Four ways to get request,session in action
In Struts2, applying an object that gets request,session from an action is a necessary step in development, so how do you get those objects from the action?
STRUTS2 offers us four ways to do this.
respectively for
Servlet unrelated non-IOC way to get request and other objects
The way that the servlet does not correlate the IoC with the request and other objects
A servlet-related non-IOC approach to acquiring objects such as request
How the servlet-related IoC obtains objects such as request
The following are respectively described.
First, look at the Struts.xml file.
File contents:
<?xml version= "1.0" encoding= "UTF-8"?>
<! DOCTYPE Struts Public
"-//apache software foundation//dtd Struts Configuration 2.0//en"
"Http://struts.apache.org/dtds/struts-2.0.dtd" >
<struts>
<package name= "Default" extends= "Struts-default" >
<action name= "Login" class= "Struts2.login.LoginAction" >
<result name= "Success" >result.jsp</result>
</action>
<action name= "login2" class= "Struts2.login.Login2Action" >
<result name= "Success" >result.jsp</result>
</action>
<action name= "login3" class= "Struts2.login.Login3Action" >
<result name= "Success" >result.jsp</result>
</action>
<action name= "Login4" class= "Struts2.login.Login4Action" >
<result name= "Success" >result.jsp</result>
</action>
</package>
</struts>
The loginaction,login2action,login3action,login4action, respectively, correspond to action in four different ways.
File name: result.jsp
File contents:
<%@ page contenttype= "text/html; charset=gb2312 "%>
<%@ taglib uri= "/struts-tags" prefix= "s"%>
The Request property is ${requestscope.attribute}<br>
The Session property is ${sessionscope.attribute}<br>
The Application property is ${applicationscope.attribute}<br>
File name: getrequest.jsp
File contents:
<%@ page contenttype= "text/html; charset=gb2312 "%>
<%@ taglib uri= "/struts-tags" prefix= "s"%>
<a href= "Login.action" >servlet an unrelated non-IOC way to get the object such as request </a><br>
<a href= "Login2.action" >servlet the way that irrelevant IoC obtains the object such as request </a><br>
<a href= "Login3.action" >servlet the relevant non-IOC way to obtain the object such as request </a><br>
<a href= "Login4.action" >servlet how the relevant IoC obtains the objects such as request </a><br>
</form>
Then we describe 4 different ways,
1,servlet irrelevant non-IOC way to get the request and other objects
This method obtains the Actioncontext first, then obtains the map type request and so on the object
Action file:
Package struts2.login;
Import Java.util.Map;
Import Com.opensymphony.xwork2.ActionContext;
Import Com.opensymphony.xwork2.ActionSupport;
public class Loginaction extends actionsupport{
Private Actioncontext context;
private MAP request;
Private MAP session;
Private MAP Application;
@Override
Public String Execute () throws Exception {
TODO auto-generated Method Stub
This.context = Actioncontext.getcontext ();
This.request = (MAP) this.context.get ("request");
This.session = This.context.getSession ();
This.application = This.context.getApplication ();
This.request.put ("attribute", "request value servlet not related to non-IOC");
This.session.put ("attribute", "Session value servlet irrelevant non-IOC");
This.application.put ("attribute", "application value servlet unrelated non-IOC");
return SUCCESS;
}
}
2,servlet the way an unrelated IoC obtains a request such as an object
This approach is to implement a specific interface, which is set by container to object such as request. Note the section of red text in the following code example.
Action file:
Package struts2.login;
Import Java.util.Map;
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 Login2action extends Actionsupport
Implements Requestaware, Sessionaware, applicationaware{
private MAP request;
Private MAP session;
Private MAP Application;
@Override
Public String Execute () throws Exception {
TODO auto-generated Method Stub
This.request.put ("attribute", "request value servlet irrelevant IoC");
This.session.put ("attribute", "Session value servlet irrelevant IoC");
This.application.put ("attribute", "Application value servlet irrelevant IoC");
return SUCCESS;
}
@Override
public void Setrequest (map<string, object> arg0) {
TODO auto-generated Method Stub
This.request = arg0;
}
@Override
public void Setsession (map<string, object> arg0) {
TODO auto-generated Method Stub
This.session = arg0;
}
@Override
public void Setapplication (map<string, object> arg0) {
TODO auto-generated Method Stub
This.application = arg0;
}
}
3,servlet related non-IOC methods for acquiring objects such as request
This method can obtain objects such as servlet-related request, which means that the request object taken out of this way is not only the function of the set attribute, but also the HTTP-related information.
To remove the method, see the Blue Word section in the code.
Action file:
Package struts2.login;
Import Javax.servlet.ServletContext;
Import Javax.servlet.http.HttpServletRequest;
Import javax.servlet.http.HttpSession;
Import Org.apache.struts2.ServletActionContext;
Import Com.opensymphony.xwork2.ActionSupport;
public class Login3action extends actionsupport{
private HttpServletRequest request;
Private HttpSession session;
Private ServletContext Application;
@Override
Public String Execute () throws Exception {
TODO auto-generated Method Stub
This.request = Servletactioncontext.getrequest ();
This.session = This.request.getSession ();
This.application = Servletactioncontext.getservletcontext ();
This.request.setAttribute ("attribute", "request value servlet-related non-IOC");
This.session.setAttribute ("attribute", "Session value servlet related non-IOC");
This.application.setAttribute ("attribute", "Application value servlet-related non-IOC");
return SUCCESS;
}
}
4,servlet how the relevant IoC obtains the object such as request
This approach can also get objects such as servlet-related request, which means that the request object taken out of this way is not only the function of the set attribute, but the HTTP-related information is available.
But the removal method is implemented via an interface, which is set by Struts2 's container, see the Red Word section in the code.
Action file:
Package struts2.login;
Import Javax.servlet.ServletContext;
Import Javax.servlet.http.HttpServletRequest;
Import javax.servlet.http.HttpSession;
Import Org.apache.struts2.interceptor.ServletRequestAware;
Import Org.apache.struts2.util.ServletContextAware;
Import Com.opensymphony.xwork2.ActionSupport;
public class Login4action extends Actionsupport
Implements Servletrequestaware, servletcontextaware{
private HttpServletRequest request;
Private HttpSession session;
Private ServletContext Application;
@Override
Public String Execute () throws Exception {
TODO auto-generated Method Stub
This.request.setAttribute ("attribute", "request value servlet-related IoC");
This.session = This.request.getSession ();
This.session.setAttribute ("attribute", "Session value Servlet related IoC");
This.application.setAttribute ("attribute", "Application value servlet-related IoC");
return SUCCESS;
}
@Override
public void Setservletrequest (HttpServletRequest arg0) {
TODO auto-generated Method Stub
This.request = arg0;
}
@Override
public void Setservletcontext (ServletContext arg0) {
TODO auto-generated Method Stub
This.application = arg0;
}
}
Well, take a look at the results of the execution, at a glance, do not know how much you know about the actions of the object such as the request of these ways?
Four ways to get request,session in action