Result学习笔记(三)带參数的结果集
第一部分:代码
//前端 <head> <meta http-equiv="Content-Type" content="text/html; charset=GB18030" /> <title>Insert title here</title> </head> <body> <ol> <li><a href="user/user?type=1">传參数</a></li> </ol> </body> </html>
//web.xml <struts> <constant name="struts.devMode" value="true" /> <package name="user" namespace="/user" extends="struts-default"> <action name="user" class="com.struts2.user.action.UserAction"> <result type="redirect">/user_success.jsp?t=${type}</result> </action> </package> </struts>
//类包 package com.struts2.user.action; import com.opensymphony.xwork2.ActionSupport; public class UserAction extends ActionSupport { private int type; public int getType() { return type; } public void setType(int type) { this.type = type; } @Override public String execute() throws Exception { return "success"; } }
//展示 </head> <body> User Success! from valuestack: <s:property value="t"/><br/> from actioncontext: <s:property value="#parameters.t"/> <s:debug></s:debug> </body> </html>
第二部分:分析
1.<li><a href="user/user?type=1">传參数</a></li>,链接訪问user命名空间里面的user action,并对action传递參数type = 1;
2.<package name="user" namespace="/user" extends="struts-default"> <action name="user" class="com.struts2.user.action.UserAction"> <result type="redirect">/user_success.jsp?t=${type}</result> </action> </package> <result type="redirect">/user_success.jsp?t=${type}</result>
目的:
跟据配置文件构造user action对象,并进行设置參数。由client发出请求后,由web.xml的配置文件构造UserAction对象时,请求中出传递的数值会传递到构造的对象中。注意:一次request仅仅有一个值栈.
result要求<result type="redirect">/user_success.jsp?t=${type}</result>,要求重定向到到user_success.jsp,并要求去除
action中的type值。当中type="redirect"表示是client跳转,
3.from valuestack: <s:property value="t"/><br/> //从值栈取值 from actioncontext: <s:property value="#parameters.t"/> <span style="white-space:pre"> </span>//从actioncontext取值
时间: 2024-10-10 16:47:39