Designing Action components
One of the limitations of Struts is that one JSP can be
associated with only one form bean at one point of time.
Similarly, one Action class can map to one form bean per
configuration. Note that you can configure the same Action class
with different path names mapping to different form beans.
Because of such limitations (Is it really a limitation?)
situations like following may become tricky to handle.
Let us think that our Student registration case study now
involves a Teacher to be associated with a Student. So a Student
is related to a Teacher and vice-versa. We declare two separate
form beans for Student and Teacher and also two separate Action
classes, StudentAction and TeacherAction, handling Student and
Teacher separately. Now you might want to display the associated
Teacher information from a Student page and also you may want to
do the same in reverse. The problem here is that the StudentAction
class maps only to Student form bean. In the JSP, where you
display the Student, information has a link to the Teacher
object. But in order to display the teacher information, you
need to forward the application to the Teacher JSP page which
maps to the Teacher form bean. However, StudentAction class has
mapping to Student form bean only and you have no way to
populate the Teacher form bean from within the StudentAction
class.
In order to tackle such situations, it is a good idea to create
Action components. For example, you might create a separate
Action class solely to display the Teacher information. This
Action class will also map to the Teacher form bean. The pre-
condition for this Action class will be that you either pass the
Teacher object to be displayed as part of the request or session
object and the display Action class then pick that up and
populate the corresponding form bean, forwarding the
application to the Teacher JSP page which can then display the
teacher information set in the form bean.
When you click the Teacher link from the Student JSP page, you
definitely call the StudentAction class (as that is what is
mapped as the form submit). Within the Student Action class you
detect that you want to display the Teacher information and then
CHAIN the action to the TeacherDisplayAction class by setting
the Teacher object as part of the request or session object. The
following diagram will make things clearer.
You can achieve the Action chaining simply through the
configuration file. Here is a sample:
<action path="/studentAction"
type="StudentAction"
name="studentForm"
parameter="methodToCall"
scope="request"
validate="true"
input="/student.jsp">
<forward name="displayTeacher" path="/displayTeacher.do"/>
</action>
<action path="/ displayTeacher "
type="DisplayTeacherAction"
name="teacherForm"
scope="request">
<forward name="success" path="/teacher.jsp"/>
</action>
Notice that the Action chaining is merely forwarding the application to a
different action.
The DisplayTeacherAction class body might look like the following:
public class DisplayTeacherAction extends Action
{
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest req, HttpServletResponse res) throws Exception
{
//get the Teacher object from the request
Teacher teacher = (Teacher)req.getAttribute(teacher);
//populate the teacher form bean with the Teacher data
//forward the application to display teacher
return mapping.findForward(success);
}
}
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.