首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何在struts2中从url接收参数

如何在struts2中从url接收参数
EN

Stack Overflow用户
提问于 2012-05-26 17:52:45
回答 2查看 3.5K关注 0票数 1

我使用的是struts2 & configured,因此url看起来像www.myweb.com/index,而不是www.myweb.com/index.action。

但是现在我面临的问题是如何在struts.xml文件中映射&获取请求参数,就像在struts1中一样,我可以通过mapping.getParameters()接收请求参数,但是在struts2中有什么可用的呢?

代码语言:javascript
运行
复制
<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是参数/

谢谢

EN

回答 2

Stack Overflow用户

发布于 2016-06-14 06:59:52

可以使用通配符模式传递参数。

代码语言:javascript
运行
复制
<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>

还可以使用“命名参数”

代码语言:javascript
运行
复制
<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中使用参数,在操作名称之后,请确保设置如下:

代码语言:javascript
运行
复制
 <constant name="struts.enable.SlashesInActionNames" value="true"/>
 <constant name="struts.mapper.alwaysSelectFullNamespace" value="false"/>

然后,操作映射将类似于:

代码语言:javascript
运行
复制
 <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

票数 2
EN

Stack Overflow用户

发布于 2012-05-26 17:57:52

使用patternMatcher,通配符是用来做其他事情的。

http://struts.apache.org/2.x/docs/wildcard-mappings.html

使用REST插件是另一种选择,或regex匹配器,视您的需要而定。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/10768530

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档