You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@struts.apache.org by Pierre Métras <ge...@sympatico.ca> on 2000/11/25 04:47:35 UTC

Formless actions

Hi,

I have to code a menu. Depending on the user interaction, different features
will be executed. These features are Struts actions *.do URL with associated
JSP files for the views. The menu is included on all the forms.

Should I use an Action class to manage the menu processing, knowing that no
input form will be associated with it? So the mapping will look like:
    <action     path="/menu"
                type="com.acme.MenuAction"
                name="menuForm"
                scope="request">
      <forward name="home"              path="/index.jsp" />
      <forward name="feature1"          path="/feature1.do?action=Init" />
      <forward name="feature2"          path="/feature2.do?action=Init" />
    </action>


Using the Struts design has the advantage that the foward mapping allows to
use logical names for the features. This indirection level allows to add or
modify a feature without impacting the others (just place a <a
href="menu.do?action=featureN">).
On the other side, Struts mechanic is a bit heavy for a servlet whose role
will be to forward to another feature based on a request parameter, that's
to say a big sequence of if-else.


Subsidiary question:
I have a menu option that is used to switch the language used for display,
and then return to the calling feature.
Can the menu know from which feature it is called, without having to add
another parameter in the request? Is the Referer header available in that
case?

Any opinion welcome.

Pierre Métras


Re: Formless actions

Posted by "Craig R. McClanahan" <Cr...@eng.sun.com>.
Pierre Métras wrote:

> Hi,
>
> I have to code a menu. Depending on the user interaction, different features
> will be executed. These features are Struts actions *.do URL with associated
> JSP files for the views. The menu is included on all the forms.
>
> Should I use an Action class to manage the menu processing, knowing that no
> input form will be associated with it? So the mapping will look like:
>     <action     path="/menu"
>                 type="com.acme.MenuAction"
>                 name="menuForm"
>                 scope="request">
>       <forward name="home"              path="/index.jsp" />
>       <forward name="feature1"          path="/feature1.do?action=Init" />
>       <forward name="feature2"          path="/feature2.do?action=Init" />
>     </action>
>
> Using the Struts design has the advantage that the foward mapping allows to
> use logical names for the features. This indirection level allows to add or
> modify a feature without impacting the others (just place a <a
> href="menu.do?action=featureN">).
> On the other side, Struts mechanic is a bit heavy for a servlet whose role
> will be to forward to another feature based on a request parameter, that's
> to say a big sequence of if-else.
>

You don't need to implement it with an if-else chain.  For example, the
hyperlink you submit for each link (or a hidden form field if you really do use
a form) could be the logical name of the destination of the forwarding ("home",
"feature1", and so on), and you can just call the findForward() method.  This
also means that the action itself doesn't need to be modified as you add new
options.

In general, I would go through the controller servlet if I needed to set up some
request or session attributes for the next UI page -- or if you are relying on
the other supporting things that the controller servlet does, like setting cache
headers and the like.  Otherwise, it's probably fine to go directly from a menu
page to the next display page.

>
> Subsidiary question:
> I have a menu option that is used to switch the language used for display,
> and then return to the calling feature.
> Can the menu know from which feature it is called, without having to add
> another parameter in the request? Is the Referer header available in that
> case?
>

The referer header is available, but it won't tell you what you want -- it sends
what you see in the Location bar, which will typically be the name of the last
xxx.do action that you submitted to (because the browser does not know you did
the forward).

If you need to know where (i.e. which JSP page) a request came from, you'll need
to add a parameter of some sort.

>
> Any opinion welcome.
>
> Pierre Métras

Craig