Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Quick question regarding parameters in struts-config.xml

 
O. Ziggy
Ranch Hand
Posts: 430
Android Debian VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 38
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4864
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
O. Ziggy
Ranch Hand
Posts: 430
Android Debian VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 948
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4864
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 430
Android Debian VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is there a way to determine which action path (from the struts-config.xml file) triggered the Action?

Thanks
 
Merrill Higginson
Ranch Hand
Posts: 4864
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, quite easily. Call the getPath() method on the instance of ActionMapping that is passed as a parameter to your execute() method.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic