Recipe7.5.Forwarding Users to a Module


Recipe 7.5. Forwarding Users to a Module

Problem

You want to forward control from one module to a page in another module.

Solution

If the link is for a JSP page and not an action, create an action of type ForwardAction for that page in the target module's struts-config.xml file. Specify the path to the JSP page as the value of the parameter attribute:

<action   path="/module1Menu"           type="org.apache.struts.actions.ForwardAction"      parameter="/mod1/module1Menu.jsp"/>

Then define a global forward, in the struts-config.xml file of the source module, with the module attribute set to the target module prefix and the path attribute set to the name of the action you just created:

<forward name="goToModule1" module="/mod1" path="/module1.do"/>

If the link is for an action in the other module, then specify the path to the action, making sure to include the Struts ActionServlet prefix or suffix (for example, .do):

<html:link forward="goToModule1">Go To Module 1</html:link>

Discussion

You would think that you could create a link to a page in another module by specifying the module attribute on the global forward:

<forward name="goToModule1" module="/mod1" path="/module1.jsp"/>

Unfortunately, this will not work. The URL to the page is correctly generated as http://localhost/jsc-ch07/mod1/module1.jsp; however, internally Struts still thinks that you are in the source module. Module-specific Struts entities such as global forwards and message resources will not be available on the target page because Struts has not switched the module context. In short, you can't link between pages in a module but only between actions.

To force Struts to switch the module context, the URL must go through the Struts controller, which is the ActionServlet. To achieve this, create an action in the Struts configuration XML file of the target module that uses the ForwardAction. Specify the name of the JSP page as the value for the parameter attribute. Then define a global forward, setting the module attribute to the target module and the path attribute to the path to the forwarding action. Make sure to include the extension (for example, /module1.do).

See Also

Module support has been enhanced in Struts 1.2. Check the online user's guide for the latest updates. The Struts User Guide can be found at http://struts.apache.org/userGuide/index.html. Some other recipes that relate are Recipes Section 6.7 and Section 7.4.



    Jakarta Struts Cookbook
    Jakarta Struts Cookbook
    ISBN: 059600771X
    EAN: 2147483647
    Year: 2005
    Pages: 200

    flylib.com © 2008-2017.
    If you may any questions please contact us: flylib@qtcs.net