STRUTS2 Learning lesson Three accessing Web resources

Source: Internet
Author: User

1. What is a Web resource?

Native servlet APIs such as Httpservletrequest,httpsession,servletcontext.

2. Why do I access Web resources?

b/S application of the controller must access the Web resources, for example, to the domain object read and write properties, read and write cookies, get Realpath and so on.

3. How do I access it?

In action, you can access resources such as the httpsession,httpservletrequest,httpservletresponse of the web in a few ways

Access to the Servlet API is decoupled: only limited servlet API objects can be accessed, and only limited methods can be accessed (GET request parameters, read and write properties of domain objects, invalidate session)

To avoid coupling with the servlet API, it is convenient for the action to do unit testing, struts2 the httpservletrequest,httpsession and ServletContext, 3 Map objects were constructed to replace the 3 objects, and the httpservletrequest,httpservletsession,servletcontext corresponding map object can be used to save and read the data directly in the action.

by Com.opensymphony.xwork2.ActionContext

The following interfaces are implemented via action:

Org.apache.struts2.interceptor.ApplicationAware;

Org.apache.struts2.interceptor.RequestAware;

Org.apache.struts2.interceptor.SessionAware;

Access mode coupled to the Servlet API: (more servlet API objects can be accessed, i.e. their native methods can be called)

by Org.apache.struts2.ServletActionContext

By implementing the corresponding Servletxxxaware interface

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

Get httpsession maps, etc.:

Public Map getsession ()

Gets the map object for the ServletContext:

Public Map getapplication ()

Gets the map object that corresponds to the request parameter:

Public Map getparameters ()

Gets the map object for the HttpServletRequest:

public object get (Object key): The Actioncontext class does not provide a method like Getrequest () to get httpservletrequest corresponding map object. To get the map object for HttpServletRequest, you can pass the request parameter implementation for the Get () method.

Look at the code:

 PackageLogan.struts2.study;ImportJava.util.Map;ImportOrg.apache.struts2.dispatcher.Parameter;ImportCom.opensymphony.xwork2.ActionContext; Public classTestactioncontext { PublicString Execute () {//0. Get the Actioncontext object//Actioncontext is the context object of the action, from which you can get all the information you need for the current actionActioncontext Actioncontext =Actioncontext.getcontext (); //1. Get the map for application and add a property to it//gets the map object of the Application object by calling the Getapplication () method of the Actioncontext objectmap<string, object> applicationmap =actioncontext.getapplication (); //Setting PropertiesApplicationmap.put ("Applicationkey", "Applicationvalue"); //Get PropertiesObject date = Applicationmap.get ("date");                SYSTEM.OUT.PRINTLN (date); //2.sessionMap<string,object> Sessionmap =actioncontext.getsession (); Sessionmap.put ("SessionKey", "Sessionvalue"); //3.request//The Actioncontext does not provide a Getrequest method to obtain the map of the request.//you need to call the Get () method manually, passing in the request string to get it. Map<string,object> Requestmap = (map<string, object>) actioncontext.get ("Request"); Requestmap.put ("Requestkey", "Requestvalue"); //4. Get the map for the request parameter and get the specified parameter value//parameters This map can only be read and cannot be written. If written, it will not be an error, but it will not work. map<string,parameter> parameters =actioncontext.getparameters (); System.out.println (Parameters.get ("Name")); return"Success"; }}
<?XML version= "1.0" encoding= "UTF-8"?><!DOCTYPE struts Public "-//apache software foundation//dtd struts Configuration 2.3//en" "Http://struts.apache.or G/dtds/struts-2.3.dtd "><Struts>    <!--Action VS Action class action: Represents a STRUTS2 Request action class: A class capable of handling STRUTS2 requests -    < Packagename= "Default"namespace="/"extends= "Struts-default">            <Actionname= "Testactioncontext"class= "Logan.struts2.study.TestActionContext">            <result>/test-actioncontext.jsp</result>        </Action>        </ Package>    </Struts>

index.jsp

<%@page Import="java.util.Date"%><%@ Page Language="Java"ContentType="text/html; charset=iso-8859-1"pageencoding="iso-8859-1"%><!DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" "Http://www.w3.org/TR/html4/loose.dtd "><HTML><Head><Metahttp-equiv= "Content-type"content= "text/html; charset=iso-8859-1"><title>Insert Title here</title></Head><Body>    <ahref= "Testactioncontext.action?name=logan&name=logan2">Test Actioncontext</a>        <%        if(Application.getattribute ("Date") == NULL) {Application.setattribute ("Date", New Date()); }    %>    </Body></HTML>

test-actioncontext.jsp

<%@ Page Language="Java"ContentType="text/html; charset=iso-8859-1"pageencoding="iso-8859-1"%><!DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" "Http://www.w3.org/TR/html4/loose.dtd "><HTML><Head><Metahttp-equiv= "Content-type"content= "text/html; charset=iso-8859-1"><title>Insert Title here</title></Head><Body>    <h4>Test Actioncontext Page</h4>Application:${applicationscope.applicationkey}<BR><BR>Session:${sessionscope.sessionkey}<BR><BR>Request:${requestscope.requestkey}<BR><BR>        <BR><BR></Body></HTML>

Xml

<?XML version= "1.0" encoding= "UTF-8"?><Web-appXmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance"xmlns= "Http://xmlns.jcp.org/xml/ns/javaee"xsi:schemalocation= "Http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"ID= "webapp_id"version= "3.1">  <Display-name>Struts2-2</Display-name>    <Filter>      <Filter-name>Struts2</Filter-name>      <Filter-class>Org.apache.struts2.dispatcher.filter.StrutsPrepareAndExecuteFilter</Filter-class>  </Filter>    <filter-mapping>      <Filter-name>Struts2</Filter-name>      <Url-pattern>/*</Url-pattern>  </filter-mapping>    <welcome-file-list>      <Welcome-file>Index.html</Welcome-file>  </welcome-file-list></Web-app>

STRUTS2 Learning lesson Three accessing Web resources

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.