Java Reference
In-Depth Information
<jsp:useBean id="beanName" class="package.beanClass"
scope="request"></jsp:useBean>
In most of my examples, the bean name and the class name are the
same.
Identify the required attributes for each command. These will usually
be defined as database fields, transaction fields, or something similar.
You must define set methods for fields used to update the model and
get methods for fields used to display the model. Create attributes in
the commands as required and implement them.
4
Identify the core logic that will initialize and trigger the model's
update or query. Move these Java statements to the model's initial-
ize and execute methods, as in listing 3.4.
5
In the controller object, instantiate the command. This can either be
done with a basic constructor ( command = new CommandClass() ), or if
the class object needs special consideration, use the code that follows.
Assume your command's variable name is command and that your com-
mand's class is of type CommandClass :
6
CommandClass command =
(packagePath.CommandClass) java.beans.Beans.instantiate(
getClass().getClassLoader(),
"packagePath.CommandClass");
Then, initialize and execute the command:
7
command.initialize();
command.execute();
request.setAttribute("YourCommand", command);
Forward the response object to the JSP with the following call:
8
ServletContext servletContext = getServletContext ();
RequestDispatcher dispatcher =
servletContext.getRequestDispatcher("theJSPResultsPage");
dispatcher.forward(request, response);
Replace the code that prints dynamic content in the original JSP with
command bean references instead, as in the JSP in listing 3.4.
9
Using this method, we can take a monolithic JSP and refactor it to the coveted
Triangle design pattern. In general, we are stripping out the code into three
pieces: the JSP (encapsulating the return-trip view), the command (encapsulat-
ing the model), and the servlet (encapsulating the controller). For teams
Search WWH ::




Custom Search