Struts2 several ways to get request

Source: Internet
Author: User

Struts2 three ways to get request

Struts2 There are three ways to get the request, preferably using the Servletrequestaware interface to inject the request object through the IOC mechanism.
Get the request method one from the action:

Code in Action:
Map request = (map) actioncontext.getcontext (). Get ("request");
list<task> tasks = Taskmanager.findall ();
Request.put ("Tasks", tasks);

Get the values in the JSP page:
<s:iterator id= "task" value= "#request. Tasks" >
<tr class= "Table_header" >
<td><s:property value= "#task. Tname"/></td>
<td><s:property value= "#task. Tuid"/></td>
<td><s:property value= "#task. Tstarttime"/></td>
<td><s:property value= "#task. Tendtime"/></td>
<td><s:property value= "#task. Tstate"/></td>
<td><input type= "Radio" id= "Choose" name= "Choose" onclick= "GetId (this.value)" value= "<s:property value=" #task. Tid '/> '/></td>
</tr>
</s:iterator>
--------------------------------------------------------------------------------------------
Method Two: Through the Servletactioncontext class to obtain, the use of struts2 experience if the handle get pass is Chinese, you can only use this method to deal with garbled problems

Action in Code:
HttpServletRequest request = Servletactioncontext.getrequest ();
Request.setattribute ("username", "Zhangsan");

Get the values in the JSP
<s:property value= "#request. Username" > or ${requestscope.req}
--------------------------------------------------------------------------------------------
Method Three: Inject the request object through the IOC mechanism through the Servletrequestaware interface
Code in Action:
The action realizes the Servletrequestaware interface, realizes the method in the interface
private HttpServletRequest request;
Implementing methods in an interface
public void Setservletrequest (HttpServletRequest request) {
This.request = Request;
}
Then in the Execute () method, you can use the
Public String execute () {
Request.setattribute ("username", "Zhangsan");
Request.getsession (). Getservletcontext (). Getapplication (); Get application
}
The method must be implemented, and the method is automatically invoked
This method, when invoked, passes the created Request object to you in the form of a parameter, which you can assign to the variables in this class, and then the request can use the
Note: the Setservletrequest () method must be executed before the Execute () method is invoked

Get the values in the JSP page
<s:property value= "#request. Task.tname"/>
/This article from the Java Show, the original source: http://www.java.sh/article/jsp/1353.html

Struts2 Hyperlink Pass Value: <s:a href= "info.action?id=%{#list. ID}" > <s:property value= "#list. Title"/></s:a>


The first way, non-IOC (control reversal in spring) means:
Java code

/**
* File Name:BaseAction.java * Version: * date:2010-1-27 * Copyright belongs to Musoon Corporation 2010
*/

Package com.action;

Import Java.util.Map;

Import Javax.servlet.http.HttpServletRequest;
Import Javax.servlet.http.HttpServletResponse;

Import Org.apache.struts2.ServletActionContext;

Import Com.opensymphony.xwork2.ActionContext;
Import Com.opensymphony.xwork2.ActionSupport;

/**
* Project name:zhiming * * Class name:baseaction
* Author:musoon * * Created time:2010-1-27 06:45:35
* Changed By:musoon * * Changed time:2010-1-27 06:45:35
* Changed Memo:
* @version
* Class Description:
*/

public class Baseaction extends Actionsupport {

Private static final long serialversionuid = 7620009925942346125L;

Actioncontext context = Actioncontext.getcontext ();
HttpServletRequest request = (httpservletrequest) context.get (servletactioncontext.http_request);
HttpServletResponse response = (httpservletresponse) context.get (servletactioncontext.http_response);
Map session = Context.getsession ();
Sessionmap session = (Sessionmap) context.get (actioncontext.session);

}


/**
* File Name:BaseAction.java * Version: * date:2010-1-27 * Copyright belongs to Musoon Corporation 2010
*/

Package com.action;

Import Java.util.Map;

Import Javax.servlet.http.HttpServletRequest;
Import Javax.servlet.http.HttpServletResponse;

Import Org.apache.struts2.ServletActionContext;

Import Com.opensymphony.xwork2.ActionContext;
Import Com.opensymphony.xwork2.ActionSupport;

/**
* Project name:zhiming * * Class name:baseaction
* Author:musoon * * Created time:2010-1-27 06:45:35
* Changed By:musoon * * Changed time:2010-1-27 06:45:35
* Changed Memo:
* @version
* Class Description:
*/

public class Baseaction extends Actionsupport {

Private static final long serialversionuid = 7620009925942346125L;

Actioncontext context = Actioncontext.getcontext ();
HttpServletRequest request = (httpservletrequest) context.get (servletactioncontext.http_request);
HttpServletResponse response = (httpservletresponse) context.get (servletactioncontext.http_response);
Map session = Context.getsession ();
Sessionmap session = (Sessionmap) context.get (actioncontext.session);

}

Our usual session is usually httpsession, but it is encapsulated in the STRUTS2 as a map type.

The second way, the IOC way:
Java code

/**
* File Name:BaseAction.java * Version: * date:2010-1-27 * Copyright belongs to Musoon Corporation 2010
*/

Package com.action;

Import Java.util.Map;

Import Javax.servlet.http.HttpServletRequest;
Import Javax.servlet.http.HttpServletResponse;

Import Org.apache.struts2.dispatcher.SessionMap;
Import Org.apache.struts2.interceptor.ServletRequestAware;
Import Org.apache.struts2.interceptor.ServletResponseAware;
Import Org.apache.struts2.interceptor.SessionAware;

Import Com.opensymphony.xwork2.ActionContext;
Import Com.opensymphony.xwork2.ActionSupport;



/**
* Project name:zhiming * * Class name:baseaction
* Author:musoon * * Created time:2010-1-27 06:45:35
* Changed By:musoon * * Changed time:2010-1-27 06:45:35
* Changed Memo:
* @version
* Class Description:
*/

public class Baseaction extends Actionsupport implements Sessionaware, Servletrequestaware, Servletresponseaware {

Private static final long serialversionuid = 7620009925942346125L;

Actioncontext context = Actioncontext.getcontext ();
HttpServletRequest request;
HttpServletResponse response;
Sessionmap session;

Get request,response,session mode One, non-IOC way, without realizing Sessionaware, Servletrequestaware, Servletresponseaware
Actioncontext context = Actioncontext.getcontext ();
HttpServletRequest request = (httpservletrequest) context.get (servletactioncontext.http_request);
HttpServletResponse response = (httpservletresponse) context.get (servletactioncontext.http_response);
Map session = Context.getsession ();
Sessionmap session = (Sessionmap) context.get (actioncontext.session);

Get Request,response,session way One, IOC way, must realize Sessionaware, Servletrequestaware, Servletresponseaware
public void Setsession (map map) {
This.session = (sessionmap) map;
}
public void Setservletrequest (HttpServletRequest request) {
This.request = Request;
}
public void Setservletresponse (HttpServletResponse response) {
This.response = response;
}

}


/**
* File Name:BaseAction.java * Version: * date:2010-1-27 * Copyright belongs to Musoon Corporation 2010
*/

Package com.action;

Import Java.util.Map;

Import Javax.servlet.http.HttpServletRequest;
Import Javax.servlet.http.HttpServletResponse;

Import Org.apache.struts2.dispatcher.SessionMap;
Import Org.apache.struts2.interceptor.ServletRequestAware;
Import Org.apache.struts2.interceptor.ServletResponseAware;
Import Org.apache.struts2.interceptor.SessionAware;

Import Com.opensymphony.xwork2.ActionContext;
Import Com.opensymphony.xwork2.ActionSupport;

/**
* Project name:zhiming * * Class name:baseaction
* Author:musoon * * Created time:2010-1-27 06:45:35
* Changed By:musoon * * Changed time:2010-1-27 06:45:35
* Changed Memo:
* @version
* Class Description:
*/

public class Baseaction extends Actionsupport implements Sessionaware, Servletrequestaware, Servletresponseaware {

Private static final long serialversionuid = 7620009925942346125L;

Actioncontext context = Actioncontext.getcontext ();
HttpServletRequest request;
HttpServletResponse response;
Sessionmap session;

Get request,response,session mode One, non-IOC way, without realizing Sessionaware, Servletrequestaware, Servletresponseaware
Actioncontext context = Actioncontext.getcontext ();
HttpServletRequest request = (httpservletrequest) context.get (servletactioncontext.http_request);
HttpServletResponse response = (httpservletresponse) context.get (servletactioncontext.http_response);
Map session = Context.getsession ();
Sessionmap session = (Sessionmap) context.get (actioncontext.session);

Get Request,response,session way One, IOC way, must realize Sessionaware, Servletrequestaware, Servletresponseaware
public void Setsession (map map) {
This.session = (sessionmap) map;
}
public void Setservletrequest (HttpServletRequest request) {
This.request = Request;
}
public void Setservletresponse (HttpServletResponse response) {
This.response = response;
}

}



In this way we will directly inherit this baseaction when we write the action, the request, response, session, and so on can be used normally, good. When I have time in the afternoon to decompile the baseaction of others, to see if this is done, haha.

PS:
Normally we have to set the value into the session and then go to the JSP page in the action (STRUTS2):
Java code  
Public String findallusers () Throws Exception {  
      
    list<user> UserList = Userservice.findallusers ();  
      
    HttpSession se = request.getsession ();  
    se.setattribute ("UserList", userlist);   
      
    session.put ("UserList", userlist);   
   //session.put ("AAA", "BBB");  
   //session.put ( Key, value);  
      
   //request.setattribute (" UserList ", userlist);  
      
    return SUCCESS;   

 public String findallusers () throws Exception {
  
  List<User> userlist = Userservice.findallusers ();
  
  httpsession se = request.getsession ();
  se.setattribute ("UserList", UserList);
  
  session.put ("UserList", userlist),
  //session.put ("AAA", "BBB");
  //session.put (key, value);
  
  //request.setattribute ("UserList", userlist);
  
   return SUCCESS;
 }


In Struts2, the session should become so, because the session is a map type:
Java code
Public String Findallusers () throws Exception {

List<user> userlist = Userservice.findallusers ();
Session.put ("UserList", userlist);
Request.setattribute ("UserList", userlist);

return SUCCESS;
}

Public String Findallusers () throws Exception {

List<user> userlist = Userservice.findallusers ();
Session.put ("UserList", userlist);
Request.setattribute ("UserList", userlist);

return SUCCESS;
}

It is said that if directly to the JSP page, the general recommendation to use the request instead of session, a number of single machine at the same time to operate the insurance, although a browser at the same time only a session.

Value in JSP page:
HTML code
<table class= "Table_report" >
<tr>
<th> User id</th>
<th> User name </th>
<th> User Sex </th>
<th> User Age </th>
<th> User Address </th>
<th> User Tel </th>
<th> User Email </th>
</tr>
<!--struts2 The most formal method of value-->
<%--<s:iterator id= "user" value= "%{#session. userlist}" >--%>

<s:iterator id= "user" value= "#session. UserList" >
<%--<s:iterator id= "user" value= "#request. UserList" >--%>
<tr>
<td>${user.id}</td>
<td>${user.name}</td>
<td>${user.sex}</td>
<td>${user.age}</td>
<td>${user.address}</td>
<td>${user.phone}</td>
<td>${user.email}</td>
</tr>
</s:iterator>
</table>
<%--use up to empty--%>
<%request.removeattribute ("UserList");%>
<%--<%session.removeattribute ("UserList");%>--%>

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.