Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

What is the right way to remove switch from my JSP file? I have a factory which can return multiple types of objects. Each of them has its own representation 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 tag views. It would be ideal if I add just 2 files: 1 state with correct annotation and 1 tag file.

like image 358
user2050786 Avatar asked Nov 12 '22 10:11

user2050786


1 Answers

Put the sateType into the tag result:

<r:state param=${state} type="${stateType}" />
like image 66
Foredoomed Avatar answered Nov 15 '22 07:11

Foredoomed