struts-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Jeff" <>
Subject How-- html::form whose action is whatever action caused its display?
Date Mon, 25 Feb 2002 19:30:11 GMT
What's the best/proper way to create a HTML:form whose "action" property is
set to whatever action caused the jsp template containing it to be displayed
in the first place?

For example, suppose a form submission to / triggers an
action class (say, "AdminAction") which determines that the user needs to
log in before doing whatever it was that his original form submission was
supposed to achieve. As luck would have it, the form bean just submitted was
a subclass of the login form bean (in fact, ALL of the application's form
beans are subclasses of the login form bean so they'll be capable of doing
double-duty as a login form bean while preserving the state of the remaining
formvars during a login sidetrip should the need arise). Anyway, the action
class returns mapping.findForward("login"), which is mapped to
/templates/login.jsp. No sweat.

Here's where things get sticky. The action class above ("AdminAction") is
not the only one that might plausibly return mapping.findForward("login").
Actually, every single action class (including "AdminAction" above) is a
subclass of the abstract LoggedInUserAction class, which verifies that the
user is logged in (and/or tries to log them in, if a username and password
happens to have been submitted) before calling its own
performLoggedInAction() abstract class, and returns
mapping.findForward("login") if the user needs to log in.

In other words, form submissions to /, /,
/, and numerous other URLs map to Action classes that
are subclasses of LoggedInUserAction, any of which might plausibly result in
the return of mapping.findForward("login") if LoggedInUserAction realizes
that the user hasn't logged in.

The challenge is making sure that when login.jsp gets displayed because
/ fell through to login.jsp, the submitted login form gets
sent to / If login.jsp gets displayed because
/ fell through to login.jsp, the submitted login form should
get sent to / I know I -could- create a separate login form
for every possible action, but I'm pretty sure that there's GOT to be a
better way to do it. Like maybe a scripting variable whose value is whatever
URL resulted in the display of the current page?


sample struts.xml excerpt:

<form-beans type="org.apache.struts.action.ActionFormBean">
<!-- all three form beans are subclasses of LoginFormBean -->
 <form-bean name="createForm" type="pkg.CreateFormBean">
 <form-bean name="previewForm" type="pkg.PreviewFormBean">
 <form-bean name="updateForm" type="pkg.UpdateFormBean">

<global-forwards type="org.apache.struts.action.ActionForward>
 <forward name="peon" path="/not_allowed.jsp">
 <!-- when the "login" mapping is called, it needs to use whatever bean -->
 <!-- was associated with the original action in the action-mappings
below -->
 <forward name="login" path="/login.jsp">

<action-mappings type="org.apache.struts.action.ActionMapping">
 <action path="/create" type="pkg.CreateAction" name="createForm">
  <forward name="create" path="/create.jsp">
 <action path="/preview" type="pkg.PreviewAction" name="previewForm">
  <forward name="preview" path="/preview.jsp">
 <action path="/update" type="pkg.UpdateAction" name="updateForm">
  <forward name="update" path="/update.jsp">

sample login.jsp excerpt:

<html:form action="/">
<!-- assuming this jsp was displayed as a result of a form submission
to / If the form submission resulting in this page's display
were to /, the action needs to be "/", and so on.
Perhaps something like... -->

To unsubscribe, e-mail:   <>
For additional commands, e-mail: <>

View raw message