This problem has always puzzled me. When writing ordinary jsp programs, it is easy to pass parameters through forms, requests, links, etc. However, in struts2, I cannot see them in every place I write. When it comes to any request or response, I don’t know how to pass parameters. Today I learned the parameter passing section in struts2 and finally solved the doubt, but it is not very clear yet and needs to be explored in the future.
Let me talk here about how to pass parameters between Actions in struts2. Parameter transfer between Actions is configured in the struts.xml file. In the result element, use the param tag to specify the name and value of the passed parameter. However, this parameter name is not written casually. It must be consistent with the Action to be passed to. corresponding to the attribute name. Let’s illustrate with an example.
Example: There is a Login.jsp page with two elements: username and password. The data needs to be submitted to Action1. Action1 then requests forwarding or redirection to Action2, and Action1 requests forwarding or redirection to In the process of Action2, the two parameters of user name and password, plus a customized constant parameter, must be passed to Action2. Action2 receives the parameters and displays the parameters to a result page: result.jsp
Code:
Login.jsp: <form action="/struts2/test/action1" method="post"> 姓名:<input type="text" name="username"/><br/> 密码:<input type="password" name="password"/><br/> <input type="submit" value="提交"/> <input type="hidden" name="type" value="something"/> </form>
struts.xml: <action name="action1" class="com.suo.actions.Action1"> <result name="success" type="redictAction"><!--type指定是请求转发还是重定向--> <param name="actionName">action2</param><!--在这里指定要请求转发或是重定向到的Action--> <param name="username">${username}</param> <param name="password">${password}</param><!--action1中的属性值--> <param name="myparam">piao</param><!-- 自定义的不变参数 --> <!--在这里定义的参数,在传到的Action中,都要有相应的set/get方法,才能够得到该参数 , 并且在传递到的Action中的属性名,要和参数的name保持一致--> </result> </action> <action name="action2" class="com.suo.actions.Action2"> <span> </span><result name="success">/WEB-INF/result/action.jsp</result> </action>
Action1.java: package com.suo.actions; import com.opensymphony.xwork2.ActionSupport; public class Action1 extends ActionSupport { private String username; private String password; public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } public String execute() { System.out.println(type); return SUCCESS; } }
Action2.java: package com.suo.actions; import com.opensymphony.xwork2.ActionSupport; public class Action2 extends ActionSupport { private String username; private String password; private String myparam; public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } public String getMyparam() { return myparam; } public void setMyparam(String myparam) { this.myparam = myparam; } public String execute() { System.out.println(username); System.out.println(password); System.out.println(myparam); return SUCCESS; } }
result.jsp: <body> username:<s:property value="username"/><br> password:<s:property value="password"/><br> myparam:<s:property value="myparam"/><br> <!-- 这里可以用标签得到的属性值,必须是在Action中有对应的set/get方法才可以 --> </body>
The above is a detailed introduction to parameter passing in struts2 For more related content, please pay attention to the PHP Chinese website (www.php.cn)!