"Go-to-organize" Struts2 the main methods and processes for passing data from the background to the foreground

Source: Internet
Author: User

two main ways:
One and Servlet API-coupled access modes
two how to access the Servlet API decoupling


********************************************************************
One and Servlet API-coupled access modes
1. Using Request (HttpServletRequest) object to pass data
(1) In the Action class file
(A) Import the Servletactioncontext class:
import Org.apache.struts2.ServletActionContext;
(B) Obtain the request object, in the following way:
httpservletrequest request = Servletactioncontext.getrequest ();
(C) Place the data object that needs to be passed into the request object through the SetAttribute () method:
Request.setattribute ("key", Object);


(2) in JSP files, there are several ways to get the data objects that are passed, such as:
(A) request.getattribute ("key") obtains the data object being passed.
(B) <s:iterator value= "#request. Key" > obtains the data object being passed.


2, using Application (ServletContext) object to pass data
(1) In the Action class file
(A) Import the Servletactioncontext class:
import Org.apache.struts2.ServletActionContext;
(B) Obtaining the Application object, in the following way:
ServletContext application = Servletactioncontext.getservletcontext ();
(C) Place the data objects that need to be passed into the Application object through the SetAttribute () method:
Application.setattribute ("key", Object);
 
(2) in JSP files, there are several ways to get the data objects that are passed, such as:
(A) application.getattribute ("key") obtains the data object being passed.
(B) <s:iterator value= "#application. Key" > obtains the data object being passed.


3, using session (HttpSession) object to pass data
(1) In the Action class file
(A) Import the Servletactioncontext class:
import Org.apache.struts2.ServletActionContext;
(B) to obtain the session object in the following way:
HttpSession session = Servletactioncontext.getrequest (). getsession ();
(C) Place the data object that needs to be passed into the session object through the SetAttribute () method:
Session.setattribute ("key", Object);


(2) in JSP files, there are several ways to get the data objects that are passed, such as:
(A) session.getattribute ("key") obtains the data object being passed.
(B) <s:iterator value= "#session. Key" > obtains the data object being passed.


*********************************************************************************
two how to access the Servlet API decoupling

1, using the request (httpservletrequest corresponding Map object) object to pass the data
(1) In the Action class file
(A) Import the Actioncontext class:
import Com.opensymphony.xwork2.ActionContext;
(B) Obtain the request object, in the following way:
actioncontext context= actioncontext.getcontext ();
Map request = (map) context.get ("request");
(C) Put the data object that needs to be passed into the request object by using the Put () method:
request.put ("key", Object);


(2) in JSP files, there are several ways to get the data objects that are passed, such as:
(A) request.getattribute ("key") obtains the data object being passed.
(B) request.get ("key") obtains the data object being passed.
(C) <s:iterator value= "#request. Key" > obtains the data object being passed.
(D) Requestscope.key obtains the data object being passed.


2, using application (servletcontext corresponding Map object) object to pass the data
(1) In the Action class file
(A) Import the Actioncontext class:
import Com.opensymphony.xwork2.ActionContext;
(B) Obtaining the Application object, in the following way:
actioncontext context= actioncontext.getcontext ();
Map application = (map) context.getapplication ();
(C) Put the data object that needs to be passed into the Application object by using the Put () method:
application.put ("key", Object);


(2) in JSP files, there are several ways to get the data objects that are passed, such as:
(A) application.getattribute ("key") obtains the data object being passed.
(B) application.get ("key") obtains the data object being passed.
(C) <s:iterator value= "#application. Key" > obtains the data object being passed.
(D) Applicationscope.key obtains the data object being passed.






3, using the session (httpsession corresponding Map object) object to pass the data
(1) In the Action class file
(A) Import the Actioncontext class:
import Com.opensymphony.xwork2.ActionContext;
(B) to obtain the session object in the following way:
actioncontext context= actioncontext.getcontext ();
Map session = (map) context.getsession ();
(C) Put the data object that needs to be passed into the session object by using the Put () method:
session.put ("key", Object);


(2) in JSP files, there are several ways to get the data objects that are passed, such as:
(A) session.getattribute ("key") obtains the data object being passed.
(B) session.get ("key") obtains the data object being passed.
(C) <s:iterator value= "#session. Key" > obtains the data object being passed.
(D) Sessionscope.key obtains the data object being passed.

"Go-to-organize" Struts2 the main methods and processes for passing 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.