What is the correct way to remove a large switch from my jsp file

What is the correct way to delete from my JSP file? I have a factory that can return multiple types of objects. Each of them has its own presentation logic, so I need something like this:

//From controller
@RequestMapping(value = "/source", method = RequestMethod.POST)
public ModelAndView doMainJob(@RequestParam("text") String text) {
    ResultState state = new ResultStateFactory().fromString(text);
    ModelAndView model = new ModelAndView("result/view");
    model.addObject("state", state);
    model.addObject("stateType", state.getClass());
    return model;
}

//from jsp/result/view.jsp
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@taglib prefix="main"  tagdir="/WEB-INF/tags" %>
<%@taglib prefix="r"  tagdir="/WEB-INF/tags/result" %>
<main:basic_layout>
  <jsp:body>
    <c:choose>
      <c:when test="${stateType == StateA}"><r:stateA param=${state} /></c:when>
      <c:when test="${stateType == StateB}"><r:stateB param=${state} /></c:when>
      <c:when test="${stateType == StateC}"><r:stateC param=${state} /></c:when>
      .
      .
      .
      <c:when test="${stateType == StateX}"><r:stateX param=${state} /></c:when>
      <c:when test="${stateType == StateY}"><r:stateY param=${state} /></c:when>
    </c:choose>
  </jsp:body>
</main:basic_layout>

My factory is based on annotations, so I can easily add state with the correct annotation. I want to do something similar to tags. It would be ideal if I added only 2 files: 1 state with the correct annotation and 1 tag file.

+5
source share
1 answer

Put sateTypein the tag result:

<r:state param=${state} type="${stateType}" />
+1
source

All Articles