The DetailForm bean
This bean is actually straightforward. You may see the complete code
here. Two things may be
noted:
1. The "validate" method checks if the "id" and "title" field have been filled in:
// Check if Id and Title are present
ActionErrors errors = new ActionErrors();
if (id == null || id.equals("")) {
errors.add("id", new ActionError("error.id.missing"));
}
if (title == null || title.equals("")) {
errors.add("title", new ActionError("error.title.missing"));
}
return errors;
This is just to show an example of validation. In a real
application you'd probably also want to check if the
"id" for a "create" was already in use.
2. Since "update" and "create" both need to get the actors in an
ArrayList, I've added some code in "validate"
that does precisely this.
The Action classes
Since most of the hard work is done in the
DVDManager bean,
the Action classes are rather
short. This is something you should always strive for--to keep
your Action classes small. The Action classes
should be considered part of the controller (the "C" in "MVC")
and should therefore act as relays or
adapters to the business logic.
Let's have a look on some of the more interesting Action classes.
You'll find a link to them all in the
Resources section at the end of the article.
The ListDVDAction class
This is the first of our classes that is being called. The first
time it is called it'll build an
instance of the DVDManager bean that holds the DVDs as a List of
JDOM Elements. This bean is saved in the
user's session.
public ActionForward perform(ActionMapping mapping,
ActionForm form,
HttpServletRequest request,
HttpServletResponse response) {
HttpSession session = request.getSession();
DVDManager dvds = (DVDManager)session.getAttribute("dvds");
if (dvds == null) {
// Get the name of the xml file from the config file
String filename = mapping.getParameter();
session.setAttribute("filename",filename);
// Read the XML file and save it in a bean
dvds = new DVDManager();
dvds.buildDocument(filename);
// Save the bean in session scope
session.setAttribute("dvds",dvds);
System.out.println
("Reading dvds.xml: " + dvds.getNumberOfDvds());
}
// Forward control to the list page
return (mapping.findForward("list"));
} |
The DetailDVDAction class
This class is rather typical. First you get the index of the DVD
from the form bean. Then you retrieve
the DVDManager bean, and using the index value, you now copy the
data from this bean to the form bean. Note
that I've sketched some simple error handling. The "error.nodvds"
key is looked up in the
ApplicationResources.properties file by the error.jsp page.
public ActionForward perform(ActionMapping mapping,
ActionForm form,
HttpServletRequest request,
HttpServletResponse response) {
// Get the index of the DVD (given in the hyperlink)
DetailForm df = (DetailForm)form;
String index = df.getIndex();
int ix = Integer.parseInt(index);
// Get the bean with dvd info
HttpSession session = request.getSession();
DVDManager dvds = (DVDManager)session.getAttribute("dvds");
if (dvds == null) {
request.setAttribute("errormessage","error.nodvds");
return (mapping.findForward("error"));
}
// Transfer the data from the dvd bean to the ActionForm
dvds.setIndex(ix);
df.setId(dvds.getId());
df.setTitle(dvds.getTitle());
df.setLength(dvds.getLength());
df.setActors(dvds.getActorLines());
// Forward control to the detail page
return (mapping.findForward("detail"));
} |
The CreateDVDAction class
Again: get the data from the form bean, get the DVD bean, use
it's create method.
public ActionForward perform(ActionMapping mapping,
ActionForm form,
HttpServletRequest request,
HttpServletResponse response) {
// Get the data from the Form bean
DetailForm df = (DetailForm)form;
String id = df.getId();
String title = df.getTitle();
String length = df.getLength();
ArrayList actorsList = df.getActorsList();
// Get the bean with dvd info
HttpSession session = request.getSession();
DVDManager dvds = (DVDManager)session.getAttribute("dvds");
// Create a new DVD
dvds.createDVD(id, title, length, actorsList);
// Forward control to the next page
return (mapping.findForward("OK"));
} |
In the config file "OK" points to the Detail page, and is
therefore easily changed to the List page if you
prefer.
New on the Java Boutique:
New Review:
Time Management Made Easy with the Quartz Enterprise Job Scheduler
Why not just use the Java timer API? This open source scheduling
API boasts simplicity, ease-of-integration, a well-rounded feature
set, and it's free!
New Applet:
Reverse Complement
Reverse Complement is a simple applet that converts DNA or RNA
sequences into three useful formats.
Elsewhere on internet.com:
WebDeveloper Java
Lots of Java information on webdeveloper.com
WDVL Java
Thorough Java resource at the Web Developer's Virtual Library.
ScriptSearch Java
Hundreds of free Java code files to download.
jGuru: Your View of the Java Universe
Customizable portal with online training, FAQs, regular news updates, and tutorials.