Struts development <parameter transfer in struts. 3>

Source: Internet
Author: User

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>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.