I do not think a ServletWrappingController is appropriate for this situation because all of the code in the Servlet needs to be completely refactored and the plan is to retire the old Servlet when the refactoring is completed.
Why not. That way you can first fix your configuration (basically put the servlet behind the DispatcherServlet) and then slowly remove all the if/else elements from the old servlet to Controllers. That way you have a migration path and you can first fix your configuration.
The other 10% do some processing and dispatch to a jsp. My plan is to create a class for each command and inject a command factory into the controller or HttpRequestHandler that delivers the appropriate class for the incoming command. All URLs are the same up to the parameter list so a single Controller could be mapped to handle the 'front end' for all requests.
So you are basically planning on rewriting Spring MVC on top of Spring MVC. Why? Simply creaete a controller per command and let spring do the mapping... The PrintWriter can be injected into the request handling method (see sample below).
Code:
@Controller
public class LoggedInController {
@RequestMapping(params={"command=VALIDATELOGIN"}
public void checkLoggedIn(@RequestParam("id") String id, PrintWriter writer) {
// Check if logged in
if (loggedIn ) {
writer.println("LOGGED IN") ;
} else {
witer.println("NOT LOGGED IN");
}
}
}
However instead of using the writer I suggest writing a custom View which renders the view that way you have a nice decoupling.
Code:
public class TextView extends AbstractView {
public static final String PARAMETER = "output-text";
@Override
protected void renderMergedOutputModel(Map<String, Object> model, HttpServletRequest request, HttpServletResponse response) throws Exception {
Object result = model.get(PARAMETER);
if (result != null && result instanceof String) {
ByteArrayOutputStream baos = createTemporaryOutputStream();
byte[] toWrite = ((String) result).getBytes();
baos.write(toWrite);
writeToResponse(response, baos);
}
}
}
Then simply configure it and let a viewresolver do the work.
Code:
<bean id="textView" class="TextView" />
<bean class="org.springframework.web.servlet.view.BeanNameViewResolver" >
<property name="order" value="1" />
</bean>
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="order" value="1" />
<property name="prefix" value=".jsp" />
<property name="suffix" value="/WEB-INF/views/" />
</bean>
You could then rewirte the controller to something like this.
Code:
@Controller
public class LoggedInController {
@RequestMapping(params={"command=VALIDATELOGIN"}
public String checkLoggedIn(@RequestParam("id") String id, Model model) {
// Check if logged in
if (loggedIn ) {
model.put(PARAMETER, "LOGGED IN");
} else {
model.put(PARAMETER, "NOT LOGGED IN");
}
return "textView"; // The name of the view.
}
}
This way you have a decoupling, controllers only process the request, prepare the model and select the view.