Do not talk nonsense, just pick up the goods
1. Pass parameters through set and get
Add the username and password attributes and add the set and get methods.
<span style="font-size:18px;">package fzl.user.struts.demo;import com.opensymphony.xwork2.ActionSupport;public class UserAction extends ActionSupport {<span style="white-space:pre"></span>private String username;<span style="white-space:pre"></span>private String password;public String getUsername() {<span style="white-space:pre"></span>return username;<span style="white-space:pre"></span>}<span style="white-space:pre"></span>public void setUsername(String username) {<span style="white-space:pre"></span>this.username = username;<span style="white-space:pre"></span>}<span style="white-space:pre"></span>public String getPassword() {<span style="white-space:pre"></span>return password;<span style="white-space:pre"></span>}<span style="white-space:pre"></span>public void setPassword(String password) {<span style="white-space:pre"></span>this.password = password;<span style="white-space:pre"></span>}public String list(){<span style="white-space:pre"></span><span style="white-space:pre"></span>System.out.println("list");<span style="white-space:pre"></span>return "success";}public String input(){<span style="white-space:pre"></span>System.out.println("input");<span style="white-space:pre"></span>return "success";}<span style="white-space:pre"></span>public String add(){<span style="white-space:pre"></span><span style="white-space:pre"></span>System.out.println("add");return "success";}}</span>
Use El expressions and Struts labels to call the list
<PRE name = "code" class = "html"> <span style = "font-size: 18px; "> <% @ page Language =" Java "contenttype =" text/html; charset = UTF-8 "pageencoding =" UTF-8 "%> <% @ taglib prefix =" S "uri ="/Struts-tags "%> <! Doctype HTML public "-// W3C // dtd html 4.01 transitional // en" "http://www.w3.org/TR/html4/loose.dtd"> <HTML>
Enter http: // localhost: 9000/strustdemo1/user_list in the browser? Username = fzl & Password = 123 input parameters
The second method is completed through actioncontext.
<span style="font-size:18px;">package fzl.user.struts.demo;import com.opensymphony.xwork2.ActionContext;import com.opensymphony.xwork2.ActionSupport;public class UserAction 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 list(){ActionContext.getContext().put("username", "flyou");ActionContext.getContext().put("password", "553274238");System.out.println("list");return "success";}public String input(){System.out.println("input");return "success";}public String add(){System.out.println("add");return "success";}}</span>
List files do not need to be modified
Method 3: Pass the value through servletapi
<span style="font-size:18px;">package fzl.user.struts.demo;import org.apache.struts2.ServletActionContext;import com.opensymphony.xwork2.ActionContext;import com.opensymphony.xwork2.ActionSupport;public class UserAction 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 list(){//ActionContext.getContext().put("username", "flyou");//ActionContext.getContext().put("password", "553274238");ServletActionContext.getRequest().setAttribute("username", "flyou");ServletActionContext.getRequest().setAttribute("password", "553274238");System.out.println("list");return "success";}public String input(){System.out.println("input");return "success";}public String add(){System.out.println("add");return "success";}}</span>
List file
<Span style = "font-size: 18px;"> <% @ page Language = "Java" contenttype = "text/html; charset = UTF-8 "pageencoding =" UTF-8 "%> <% @ taglib prefix =" S "uri ="/Struts-tags "%> <! Doctype HTML public "-// W3C // dtd html 4.01 transitional // en" "http://www.w3.org/TR/html4/loose.dtd"> <HTML>
Three methods
1. Accept and pass through seter and geter Methods
2. Pass parameters through actioncontext. getcontext (). Put ("username", "flyou ");
3. Pass the value through servletactioncontext. getrequest. setattribute ("", "")
Struts development <parameter transfer in struts. 3>