aspose file tools*
The moose likes Struts and the fly likes Quick question regarding parameters in struts-config.xml Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "Quick question regarding parameters in struts-config.xml " Watch "Quick question regarding parameters in struts-config.xml " New topic
Author

Quick question regarding parameters in struts-config.xml

O. Ziggy
Ranch Hand

Joined: Oct 02, 2005
Posts: 430

What is the easiest way to pass a parameter to an Action.

i have looked at the configuration details for the struts-config.xml file and found two properties that i think i can use. The "parameter" property and the "set-property" property.

I am a bit confused though as to what is the difference between the two. Can someone please clarify? or can you think of a better way to pass a parameter to an action?

thanks in advance
Rohit Dhodapkar
Ranch Hand

Joined: Apr 27, 2006
Posts: 38
The work of passing parameters to the action is done my the inbuilt ActionServlet. Programmer doesnt need to worry about that. Read the Struts framework flow for more details.
Also the "parameter" attribute is a attribute of <action.../> tag in struts-config.xml. Its used in case of DispatchAction. setAttribute is used when we use dynaaction forms.
Merrill Higginson
Ranch Hand

Joined: Feb 15, 2005
Posts: 4864
Although the parameter attribute of the <action> tag is used for DispatchAction, if your Action class does not extend DispatchAction, you can use it to pass any data you want to the Action class. To retrieve the value, just use the getParameter() method of the ActionMapping object that is passed to the execute method as a parameter.


Merrill
Consultant, Sima Solutions
O. Ziggy
Ranch Hand

Joined: Oct 02, 2005
Posts: 430

Originally posted by Merrill Higginson:
Although the parameter attribute of the <action> tag is used for DispatchAction, if your Action class does not extend DispatchAction, you can use it to pass any data you want to the Action class. To retrieve the value, just use the getParameter() method of the ActionMapping object that is passed to the execute method as a parameter.


Can i specify more that one parameter to be passed to the Action?
Brent Sterling
Ranch Hand

Joined: Feb 08, 2006
Posts: 948
It is pretty easy to add custom attributes to all actions, though it may be overkill depending on what you are doing. You need to create a class that extends ActionMapping. Add get and set methods for your property. In struts-config set your class as the type on the action-mappings entry:

<action-mappings type="com.x.web.common.MyActionMapping">

You can then set this property like this:

<action path="/displayLogin" type="com.x.action.LoginAction" ... >
<set-property property="customprop" value="100" />
<forward name="success" path="login" />
</action>

In your action you have to cast the passed in mapping to your custom class, but once you do that you can get at the value of the property. Another trick it to use RequestActionMapping to change the default scope to request.

- Brent
Merrill Higginson
Ranch Hand

Joined: Feb 15, 2005
Posts: 4864
Also, don't forget that a Struts App is still a Java EE App, so all of the Java EE methods of passing information into the app from web.xml are available to you: Context parameters, available through the ServletContext's getInitParameter() method, as well as environment variables that you can look up in JNDI.
O. Ziggy
Ranch Hand

Joined: Oct 02, 2005
Posts: 430

Is there a way to determine which action path (from the struts-config.xml file) triggered the Action?

Thanks
Merrill Higginson
Ranch Hand

Joined: Feb 15, 2005
Posts: 4864
Yes, quite easily. Call the getPath() method on the instance of ActionMapping that is passed as a parameter to your execute() method.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Quick question regarding parameters in struts-config.xml