Struts transmits data from the background to the foreground

Source: Internet
Author: User

Before writing it, The get and set methods should be provided for the instantiated objects in the action, and data can be obtained through the struts tag at the front end.

 

----------------------------------------------------------------------- Convert -------------------------------------------------------------

Instead of passing values to the JSP page by action, the JSP page obtains the values in the property values or ranges (such as request, session, and application) of the action. Therefore, there are two methods: 1. Obtain the value of the action attribute. You can use the struts2 tag and ognl to obtain the value, for example, <s: property value = "property name. property name... "/> In this Form 2, get the value in the range and directly use the El expression, for example, $ {name} is the attribute named name bound to the requestscope range, omit requestscope because this is the default range $ {sessionscope. name} is the name attribute bound to the sessionscope.

 

 

1234567 1 ) Action defines getpersons () 2 ) getname () and getage () are defined in person () 3 ): "U" value = "persons" > '# U. getname () ' /> '# U. getage () ' />
总结来说是2中方式:如下 
1、一般是在Action中定义一个成员变量,然后对这个成员变量提供get/set方法,在JSP页面就可以取到这个变量的值了。
  1)在Action中定义成员变量
//定义一个成员变量 private String message;       //提供get/set方法 public String getMessage() {     return message; } public void setMessage(String message) {     this.message = message; }
 2)在JSP页面中取值
${message} 或者 <s:property value="message"/>
2、但是定义的成员变量多了,感觉整个Action的代码就很长了。这个时候可以使用一些Servlet API进行值的存取操作:HttpServletRequest、HttpSession和ServletContext。Struts2对这个三个对象用Map进行了封装,我们就可以使用Map对象来存取数据了。
  1)在Action中存值
ActionContext actionContext = ActionContext.getContext();           //get HttpServletRequest Map<String,Object> request = (Map) actionContext.get("request"); request.put("a", "a is in request");           //get HttpSession //Map<String,Object> session = (Map) actionContext.get("session"); Map<String,Object> session = actionContext.getSession(); session.put("b", "b is in session");           //get ServletContext //Map<String,Object> application  = (Map) actionContext.get("application"); Map<String,Object> application  = actionContext.getApplication(); application.put("c", "c is in application");
 2)在JSP页面上取值
${a} ${b} ${c} or               ${requestScope.a} ${sessionScope.b} ${applicationScope.c}

struts transfers data from the background to the foreground

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.