Struts2 Learning Path (v)--access to web elements

Source: Internet
Author: User

The following is an example of accessing a Web element in the action class, in total four ways. One of the first and third dependent containers, the second and fourth use of IOC thought. The previous two request, session, and application for the map type, followed by two references to the real type HttpServletRequest, Httpservletsession, ServletContext.

1. The first method:

1  Packagecom.user.action;2 3 ImportJava.util.Map;4 5 ImportCom.opensymphony.xwork2.ActionContext;6 ImportCom.opensymphony.xwork2.ActionSupport;7 8  Public classLoginAction1extendsactionsupport{9     PrivateMAP request;Ten     PrivateMap session; One     PrivateMAP Application; A      -      PublicLoginAction1 () { -Request = (map<string, string>) Actioncontext.getcontext (). Get ("Request"); theSession =Actioncontext.getcontext (). getsession (); -Application =Actioncontext.getcontext (). Getapplication (); -     } -      +      PublicString Execute () { -Request.put ("R1", "R1"); +Session.put ("S1", "S1"); AApplication.put ("A1", "A1"); at         returnSUCCESS; -     } -}

2. The second method: (recommended)

1  Packagecom.user.action;2 3 ImportJava.util.Map;4 5 ImportOrg.apache.struts2.interceptor.ApplicationAware;6 ImportOrg.apache.struts2.interceptor.RequestAware;7 ImportOrg.apache.struts2.interceptor.SessionAware; 8 9 ImportCom.opensymphony.xwork2.ActionSupport;Ten  One //The idea of IOC program design A  -  Public classLoginAction2extendsActionsupportImplementsRequestaware,sessionaware,applicationaware { -     PrivateMap<string,object>request; the     PrivateMap<string,object>session; -     PrivateMap<string,object>Application; -      -      PublicString Execute () { +Request.put ("R1", "R1"); -Session.put ("S1", "S1"); +Application.put ("A1", "A1"); A         returnSUCCESS; at     } -      - @Override -      Public voidSetrequest (map<string, object>request) { -          This. Request =request; -     } in      -  to @Override +      Public voidSetsession (map<string, object>session) { -          This. session =session; the          *     } $ Panax Notoginseng @Override -      Public voidSetapplication (map<string, object>application) { the          This. Application =Application; +          A     } the}

3. The third method:

1  Packagecom.user.action;2 3 ImportJavax.servlet.ServletContext;4 Importjavax.servlet.http.HttpServletRequest;5 Importjavax.servlet.http.HttpSession;6 7 ImportOrg.apache.struts2.ServletActionContext;8 9 ImportCom.opensymphony.xwork2.ActionSupport;Ten  One  Public classLoginAction3extendsActionsupport { A     Privatehttpservletrequest request; -     PrivateHttpSession session; -     PrivateServletContext Application; the      -      PublicLoginAction3 () { -Request =servletactioncontext.getrequest (); -Session =request.getsession (); +Application =Session.getservletcontext (); -     } +      PublicString Execute () { ARequest.setattribute ("R1", "R1"); atSession.setattribute ("S1", "S1"); -Application.setattribute ("A1", "A1"); -         returnSUCCESS; -     } -}

4. The fourth method:

1  Packagecom.user.action;2 3 ImportJavax.servlet.ServletContext;4 Importjavax.servlet.http.HttpServletRequest;5 Importjavax.servlet.http.HttpSession;6 7 ImportOrg.apache.struts2.interceptor.ServletRequestAware;8 9 ImportCom.opensymphony.xwork2.ActionSupport;Ten  One  Public classLoginAction4extendsActionsupportImplementsServletrequestaware { A     Privatehttpservletrequest request; -     PrivateHttpSession session; -     PrivateServletContext Application; the      -      PublicString Execute () { -Request.setattribute ("R1", "R1"); -Session.setattribute ("S1", "S1"); +Application.setattribute ("A1", "A1"); -         returnSUCCESS; +     } A      at      Public voidsetservletrequest (HttpServletRequest request) { -          This. Request =request; -          This. session =session; -          This. Application =Application; -     } -}

Struts2 Learning Path (v)--access to web elements

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.