| This Chapter | |
| - | Chapter 1: Model 2 and Struts |
| - | Transfer Objects |
| - | A Primitive Model 2 Application |
| - | Employing View Manager and Action Manager |
| - | Enter Struts |
| - | A Struts Example |
| - | The MVC-ness of Struts |
| - | Summary |
It is easy to see how the action servlet in the previous application can easily become bloated as the application grows in complexity. This is due to the fact that the code resides inside the controller servlet’s process method. To avoid this, you can make the application more modular by introducing two modules: action manager and view manager.
The following example (app01b) adds a view manager and an action manager to the previous sample application. The ActionManager class, which represents the action manager, is given in Listing 1.7 and the ViewManager class, a representation of the view manager, in Listing 1.8
Listing 1.7: The ActionManager class
package app01b;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class ActionManager {
public void execute(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException {
String action = request.getParameter("action");
// execute an action if necessary
if (action.equals("displayAddProductForm")) {
// do nothing
}
else if (action.equals("saveProduct")) {
// read and save the product into the database
ProductTO product = new ProductTO();
product.setProductName(
request.getParameter("productName"));
product.setDescription(
request.getParameter("description"));
product.setPrice(request.getParameter("price"));
// code for persisting the Product object goes here
request.setAttribute("product", product);
}
}
}
ActionManager has one method, execute, which replaces the first if block in the process method in the controller servlet in the previous application.
Listing 1.6:
package app01b;
import java.io.IOException;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class ViewManager {
public void execute(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException {
String action = request.getParameter("action");
// forward to a view
String dispatchUrl = null;
if (action.equals("displayAddProductForm")) {
dispatchUrl = "/jsp/displayAddProductForm.jsp";
}
else if (action.equals("saveProduct")) {
dispatchUrl = "/jsp/displaySavedProduct.jsp";
}
if (dispatchUrl != null) {
RequestDispatcher rd = request.getRequestDispatcher(dispatchUrl);
rd.forward(request, response);
}
}
}
The execute method in ViewManager takes over the task of managing views in the process method of the controller servlet in the previous application.
Listing 1.9 shows the much simpler process method in the controller servlet in the app01b application.
Listing 1.9: The process method in the controller servlet
private void process(HttpServletRequest request,
HttpServletResponse response)
throws IOException, ServletException {
ActionManager actionManager = new ActionManager();
actionManager.execute(request, response);
ViewManager viewManager = new ViewManager();
viewManager.execute(request, response);
}
The JSPs and the ProductTO class remain the same.
The app01b application is similar in functionality to app01a. You can start the application by using the following URL:
http://localhost:8080/app01b/Controller?action=displayAddProductForm
Note
Certain actions require no code to run in the action manager.