Reputation: 533
I am using struts2 & configured so the url will look like as www.myweb.com/index instead of www.myweb.com/index.action
But now the problem i am facing is how should i map in struts.xml file & get request parameters as in struts1 i can receive it through mapping.getParameters() but what available in struts2 for this?
<action path="/profile/*"
type="net.viralpatel.struts.cleanurl.ProfileAction"
parameter="{1}">
<forward name="success" path="/profile.jsp" />
<forward name="fail" path="/profile.jsp" />
</action>
String parameter = mapping.getParameter();
So in struts2 if i hit www.myweb.com/index/p=2 www.myweb.com/index/biz/name /here biz & name are 2 parameters/ www.myweb.com/index/biz/name/23 /here biz & name & 23 are parameters/
Thanks
Upvotes: 1
Views: 3906
Reputation: 2856
You can use wildcard patterns to pass the parameter.
<action name="/profile/*" class="net.viralpatel.struts.cleanurl.ProfileAction">
<param name="id">{1}</param>
<result>
<param name="location">/profile.jsp</param>
<param name="id">{1}</param>
</result>
</action>
You can also use Named Parameter
<constant name="struts.patternMatcher" value="namedVariable"/>
@Namespace{"/users/{userID}");
public class ProfileAction exends ActionSupport {
private Long userID;
public void setUserID(Long userID) {...}
}
if the request URL is /users/10/detail
, then the ProfileAction will be executed and its userID
field will be set to 10.
To use parameters in the URL, after the action name, make sure this is set:
<constant name="struts.enable.SlashesInActionNames" value="true"/>
<constant name="struts.mapper.alwaysSelectFullNamespace" value="false"/>
Then the action mapping will look like:
<package name="edit" extends="struts-default" namespace="/edit">
<action name="/profile/*" class="net.viralpatel.struts.cleanurl.ProfileAction">
<param name="id">{1}</param>
<result>/profile.jsp</result>
</action>
</package>
When a URL like /edit/profile/123
is requested, ProfileAction will be called, and its "id" field will be set to 123.
Upvotes: 2
Reputation: 160301
Use the patternMatcher, wildcards are for doing something else.
http://struts.apache.org/2.x/docs/wildcard-mappings.html
Using the REST plugin is another option, or the regex matcher, depending on your needs.
Upvotes: 0