Struts2 action

Source: Internet
Author: User
Tags static class

Method 1. An ActionContext is returned through ActionContext. getActionContext ().
Obtain

The code is as follows: Copy code

ActionContext ac = ActionContext. getContext ();
Ac. get ("tip"); // The default value is to obtain the attributes in the request and the tip attributes of the built-in request object.

If you want to obtain attributes of other ranges
Ac. getApplication (). get ("tip") // Obtain the tip attribute of the application range
Ac. getApplication (). put ("tip", "abc"); // Set the tip attribute of the application range to abc.
Ac. getSession (). get ("tip"); // gets the tip attribute of the session range
Ac. getSession (). put ("tip", "abc"); // Set the tip attribute of the session range to abc.


Map application = ac. getApplication ();
Note: The above method obtains the map object, which is the real application simulated by struts and is encapsulated in the struts framework, this feature decouples action and servlet APIs.


Method 2. Implement the following interface

ServletContextAware: this interface can be used to access the ServletContext instance of the Web application.
ServletRequestAware: this interface can be used to access the HttpServletRequest instance of the Web application.
ServletResponseAware: this interface can be used to access the HttpServletResponse instance of the Web application.

Example:

The code is as follows: Copy code

Public class LoginAction extends ActionSupport implements ServletRequestAware
{
HttpServletRequest hsr = null;
@ Override
Public void setServletRequest (HttpServletRequest request ){
Hsr = request;
    }

@ Override
Public String execute () throws Exception
    {
Hsr. getAttribute ("tip"); // Obtain the tip attribute of the request range.
    }
}

Other interfaces are similar to this;

Note: the obtained result is a pure jsp built-in object, which is not encapsulated by struts. That is to say, our actions can directly use Servlet APIs, and at the same time produce coupling between them.

 

Method 3. ServletActionContext

Using this static class, you can obtain the built-in jsp objects between them.

The code is as follows: Copy code

ServletActionContext. getServletContext ();
ServletActionContext. getRequest ();
ServletActionContext. getRequest (). getSession ();

Note: I personally think this method is more convenient. If you want to use the api in servlet directly, we recommend this method.

Related Article

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.