Accessing Web Resources in action

Source: Internet
Author: User

1. What is a Web resource:

Native servlet APIs such as Httpservletrequest,httpsession,servletcontext.

2. Why do you want to access Web resources?

b/S application controller must access the Web resources: such as the domain object read-write properties, reading cookies, get Realpath ...

3, how to access?

1, and the servlet API decoupling way: only limited access to the Servlet API object, and only access to a limited number of methods (read the parameters of the request, read and write the properties of the domain object, so that the session object invalidation, etc.)

--using Actioncontext: To avoid coupling with the servlet API, it is convenient for action to do unit testing, Struts2 to HttpServletRequest, HttpSession and ServletContext are encapsulated, three map objects are constructed to replace three objects, and httpservletrequest,httpservletsession can be used directly in the action. ServletContext the corresponding Map object to save and read data.

Actioncontext is the context object that the action executes, and all the objects needed to execute the action are saved in Actionconetxt, including Parameters,request,session,application, and so on

Get httpsession Map object:--public map getsession ()

Get ServletContext Map object:--public map getapplication ()

Gets the map object that corresponds to the request parameter:--public map getparameters ()

Get httpservletrequest corresponding Map object:--publiic Object Get (Object Key): Actioncontext class does not provide similar getrequest () This method is used to obtain the map object for HttpServletRequest. To get the map object for HttpServletRequest, you can pass the parameter request method through the Get () method. Key: The name of the request parameter, the value: the string array that corresponds to the value of the request parameter

It is worth noting that the return value of the getparameters is 1, and the MAP<STRING,OBJECT> is not map<string,string[]>

2, parameters This map can only read, but not write. If it's written, it's not going to go wrong, but it doesn't work.

--Implement the Xxxaware interface: access to the corresponding API is achieved through dependency injection. After implementing the Appcationaware interface, and then implementing the method of the interface, you can use the Appcation map object in the action. Other aware interfaces are Parametersaware, Requestaware, Sessionaware, and so on.

---Recommendation: If there are multiple action methods in the action class that use the map or parameter of the domain object, it is recommended to use the method of implementing the Xxxaware interface

The map for the session is actually of type Sessionmap. If you call its invalidate () after a strong turn, you can invalidate it

2. The way the Servlet API is coupled: More Servlet API objects can be accessed and their native methods can be called. Direct access to the Serlvet API will cause the action to be coupled to the servlet environment, and a Serlvet container is required for testing. Not easy for unit tests on action

-Using Servletactioncontext:

1. Get HttpServletRequest Object directly:--servletactioncontext.getrquest ()

2, directly get Servletsession object:--Servletactioncontext.getrequest (). Getseesion ()

3. Get ServletContext Object directly:--servletactioncontext.getservletcontext ()

-To implement the Servletxxxaware interface: By implementing interfaces such as Servletrequestaware,servletcontextaware

3, since there can be a non-coupling way to access the Servlet API, why also need a coupled way to access the Serlvet API? --because the non-coupled approach only accesses the map and sometimes requires access to the native API, such as Getrealpath (), this method can only be used by accessing the native Servlet API.

4, about the STRUTS2 request extension problem

1, org.apache.struts2 package under the default.properties configured in the STRUTS2 application of some constants;

2. sturts.action.extension defines the extension that the current STRUTS2 application can accept the request.

3. The constants configured by Default.properties can be modified in a constant configuration in the Strust.xml file. Such as:

<constant name= "struts.action.extension" value= "Action,do," ></constant> such a configuration, You can modify the extension of the current Strust2 app to accept all requests with. Action,.do and no suffixes.

5, in the manual completion of the field validation, display error information, internationalization, etc., the recommendation of inheritance Actionsupport

  

Accessing Web Resources in action

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.