我使用的是struts2 & configured,因此url看起来像www.myweb.com/index,而不是www.myweb.com/index.action。
但是现在我面临的问题是如何在struts.xml文件中映射&获取请求参数,就像在struts1中一样,我可以通过mapping.getParameters()接收请求参数,但是在struts2中有什么可用的呢?
<action path="/profile/*"
type="net.viralpatel.struts.cleanurl.ProfileAction"
parameter="{1}">
<forward name="success" path="/profile.jsp" />
<forward name="fail" path="/profile.jsp" />
</action>
字符串参数= mapping.getParameter();
所以在struts2中,如果我点击www.myweb.com/index/p=2 www.myweb.com/index/ biz /name /here biz & name是2个参数/ www.myweb.com/index/biz/ name /23 /here biz&name& 23是参数/
谢谢
发布于 2016-06-14 06:59:52
可以使用通配符模式传递参数。
<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>
还可以使用“命名参数”
<constant name="struts.patternMatcher" value="namedVariable"/>
@Namespace{"/users/{userID}");
public class ProfileAction exends ActionSupport {
private Long userID;
public void setUserID(Long userID) {...}
}
如果请求URL为/users/10/detail
,则将执行ProfileAction,并将其userID
字段设置为10。
操作名称之后的参数。
若要在URL中使用参数,在操作名称之后,请确保设置如下:
<constant name="struts.enable.SlashesInActionNames" value="true"/>
<constant name="struts.mapper.alwaysSelectFullNamespace" value="false"/>
然后,操作映射将类似于:
<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>
当请求像/edit/profile/123
这样的URL时,将调用/edit/profile/123
,并将其"id“字段设置为123。
发布于 2012-05-26 17:57:52
使用patternMatcher,通配符是用来做其他事情的。
http://struts.apache.org/2.x/docs/wildcard-mappings.html
使用REST插件是另一种选择,或regex匹配器,视您的需要而定。
https://stackoverflow.com/questions/10768530
复制相似问题