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.
Put the sateType
into the tag result:
<r:state param=${state} type="${stateType}" />
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With