Accessing web elements in Struts2

Source: Internet
Author: User

Get reference of Map type request, session, application, actual type HttpServletRequest, HttpSession, and ServletContext:

First three: dependent on Containers

First three: IOC (only this type)

The following three: dependent on Containers

Third: IOC

I. Method 1: ActionContext

It is generally obtained in the Action class constructor or execute () method.

[Java]
Public class LoginAction1 extends ActionSupport {

Private Map request;
Private Map session;
Private Map application;

Public LoginAction1 (){
Request = (Map) ActionContext. getContext (). get ("request ");
Session = ActionContext. getContext (). getSession ();
Application = ActionContext. getContext (). getApplication ();
}

Public String execute (){
Request. put ("r1", "r1 ");
Session. put ("s1", "s1 ");
Application. put ("a1", "a1 ");
Return SUCCESS;
}
}

Public class LoginAction1 extends ActionSupport {
 
Private Map request;
Private Map session;
Private Map application;
 
Public LoginAction1 (){
Request = (Map) ActionContext. getContext (). get ("request ");
Session = ActionContext. getContext (). getSession ();
Application = ActionContext. getContext (). getApplication ();
}
 
Public String execute (){
Request. put ("r1", "r1 ");
Session. put ("s1", "s1 ");
Application. put ("a1", "a1 ");
Return SUCCESS;
}
}

Then, obtain the relevant web elements on the Jsp page.

[Html]
<Body>
User Login Success!
<Br/>
<S: property value = "# request. r1"/> | <% = request. getAttribute ("r1") %> <br/>
<S: property value = "# session. s1"/> | <% = session. getAttribute ("s1") %> <br/>
<S: property value = "# application. a1"/> | <% = application. getAttribute ("a1") %> <br/>
<S: property value = "# attr. a1"/> <br/>
<S: property value = "# attr. s1"/> <br/>
<S: property value = "# attr. r1"/> <br/>
<S: debug> </s: debug>
<Br/>
</Body>

<Body>
User Login Success!
<Br/>
<S: property value = "# request. r1"/> | <% = request. getAttribute ("r1") %> <br/>
<S: property value = "# session. s1"/> | <% = session. getAttribute ("s1") %> <br/>
<S: property value = "# application. a1"/> | <% = application. getAttribute ("a1") %> <br/>
<S: property value = "# attr. a1"/> <br/>
<S: property value = "# attr. s1"/> <br/>
<S: property value = "# attr. r1"/> <br/>
<S: debug> </s: debug>
<Br/>
</Body>
Note: The request, session, and application Object Struts2 will be placed in the Action Context,

Therefore, you need to use # key to access objects.

The following describes how to access the java script code.

I. Method 2: Ioc (control reversal)-recommended

Enable the Action class to implement the RequestAware, SessionAware, and ApplicationAware interfaces, and then rewrite their set methods (setRequest, setSession, and setApplication) through dependency injection and control inversion (originally controlled by myself, it is now controlled by others .)

[Java]
Import org. apache. struts2.interceptor. ApplicationAware;
Import org. apache. struts2.interceptor. RequestAware;
Import org. apache. struts2.interceptor. SessionAware;
Import com. opensymphony. xwork2.ActionSupport;
Public class LoginAction2 extends ActionSupport implements RequestAware, SessionAware, ApplicationAware {
Private Map <String, Object> request;
Private Map <String, Object> session;
Private Map <String, Object> application;

// DI dependency injection
// IoC inverse of control Inversion
Public String execute (){
Request. put ("r1", "r1 ");
Session. put ("s1", "s1 ");
Application. put ("a1", "a1 ");
Return SUCCESS;
}
@ Override
Public void setRequest (Map <String, Object> request ){
This. request = request;
}
@ Override
Public void setSession (Map <String, Object> session ){
This. session = session;
}
@ Override
Public void setApplication (Map <String, Object> application ){
This. application = application;
}
}

Import org. apache. struts2.interceptor. ApplicationAware;
Import org. apache. struts2.interceptor. RequestAware;
Import org. apache. struts2.interceptor. SessionAware;
Import com. opensymphony. xwork2.ActionSupport;
Public class LoginAction2 extends ActionSupport implements RequestAware, SessionAware, ApplicationAware {
Private Map <String, Object> request;
Private Map <String, Object> session;
Private Map <String, Object> application;
 
// DI dependency injection
// IoC inverse of control Inversion
Public String execute (){
Request. put ("r1", "r1 ");
Session. put ("s1", "s1 ");
Application. put ("a1", "a1 ");
Return SUCCESS;
}
@ Override
Public void setRequest (Map <String, Object> request ){
This. request = request;
}
@ Override
Public void setSession (Map <String, Object> session ){
This. session = session;
}
@ Override
Public void setApplication (Map <String, Object> application ){
This. application = application;
}
}
Obtain related objects on the view (JSP) page, in the same way.

I. Method 3: obtain the original type

HttpServletRequest/HttpSession/ServletContext

[Java]
Public class LoginAction3 extends ActionSupport {

Private HttpServletRequest request;
Private HttpSession session;
Private ServletContext application;
Public LoginAction3 (){
Request = ServletActionContext. getRequest ();
Session = request. getSession ();
Application = session. getServletContext ();
}
Public String execute (){
Request. setAttribute ("r1", "r1 ");
Session. setAttribute ("s1", "s1 ");
Application. setAttribute ("a1", "a1 ");
Return SUCCESS;
}
}

Public class LoginAction3 extends ActionSupport {
 
Private HttpServletRequest request;
Private HttpSession session;
Private ServletContext application;
Public LoginAction3 (){
Request = ServletActionContext. getRequest ();
Session = request. getSession ();
Application = session. getServletContext ();
}
Public String execute (){
Request. setAttribute ("r1", "r1 ");
Session. setAttribute ("s1", "s1 ");
Application. setAttribute ("a1", "a1 ");
Return SUCCESS;
}
}


I. Method 4: obtain the original type-control reversal

First, you need Action to implement the org. apache. struts2.interceptor. ServletRequestAware interface, then rewrite the setServletRequest () method to obtain the HttpServletRequest object, and then retrieve the jsonhttpsession and ServletContext objects through the HttpServletRequest object.


[Java]
Import javax. servlet. ServletContext;
Import javax. servlet. http. HttpServletRequest;
Import javax. servlet. http. HttpSession;
 
Import org. apache. struts2.interceptor. ServletRequestAware;
Import com. opensymphony. xwork2.ActionSupport;
Public class LoginAction4 extends ActionSupport implements ServletRequestAware {
Private HttpServletRequest request;
Private HttpSession session;
Private ServletContext application;
Public String execute (){
Request. setAttribute ("r1", "r1 ");
Session. setAttribute ("s1", "s1 ");
Application. setAttribute ("a1", "a1 ");
Return SUCCESS;
}
@ Override
Public void setServletRequest (HttpServletRequest request ){
This. request = request;
This. session = request. getSession ();
This. application = session. getServletContext ();
}
}

Import javax. servlet. ServletContext;
Import javax. servlet. http. HttpServletRequest;
Import javax. servlet. http. HttpSession;

Import org. apache. struts2.interceptor. ServletRequestAware;
Import com. opensymphony. xwork2.ActionSupport;
Public class LoginAction4 extends ActionSupport implements ServletRequestAware {
Private HttpServletRequest request;
Private HttpSession session;
Private ServletContext application;
Public String execute (){
Request. setAttribute ("r1", "r1 ");
Session. setAttribute ("s1", "s1 ");
Application. setAttribute ("a1", "a1 ");
Return SUCCESS;
}
@ Override
Public void setServletRequest (HttpServletRequest request ){
This. request = request;
This. session = request. getSession ();
This. application = session. getServletContext ();
}
}

 

 

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.