首先:在action类中给rquest设置一个属性:
例如:
String catalogid = ServletActionContext.getRequest().getParameter("catalogid");
ServletActionContext.getRequest().setAttribute("catalogid", catalogid);
接着:在action配置中配置url
<action name="saveCatalog" class="bookCatalogBean" method="saveBookCatalog">
<result name="success" type="redirect">/admin/book/loadTopCatalogs.action</result>
<result name="add" type="redirect-action">/admin/book/addSubCatalog?id=${#request.catalogid}&type=add</result>
</action>
这样就可以在addSubCatalog里面用url传递的参数了
在这个配置文件里,多个参数的连接符使用了"&",但XML的语法规范,应该使用"&"代替"&",原理和HTML中的转义相同,开始没有注意,在struts分析配置文件时,总是报出这样的错误:
Java代码
The reference to entity "id" must end with the ';'
进行上面说明的替换后,就正常了
struts2 redirect-action传递参数问题解决方案
我想在Post被Save 成功之后,直接转到 viewPost 页面,该页面显示主贴及所有跟贴,
也就是由savePost.action 转到 viewPost.action 并且要将主贴的postid作为参数传过去,配置如下:
<package name="post" namespace="/post" extends="struts-default">
<action name="savePost" method="savePost" class="net.mengfanpp.bbs.post.web.action.PostAction" >
<result type="redirect-action">viewPost?postid=${postid}</result>
<result name="input">/WEB-INF/pages/post/edit.jsp</result>
</action>
<action name="viewPost" class="net.mengfanpp.bbs.post.web.action.PostAction" method="viewPost">
<result>/WEB-INF/pages/post/view.jsp</result>
</action>
</package>
Note: 红色粗体部分 viewPost?postid=${postid} 不能写成 viewPost.action?postid=${postid}
当有多个参数需要传递时,只需在各个参数添加&符号,例如
<result type="redirect-action">viewPost?postid=${postid}&postname=${postname}</result>