1.与serlvet API无耦合的方式
在一个action中的获得方式:
//ServletContext context = ServletActionContext.getServletContext();
ActionContext context = ActionContext.getContext();
Map request = (Map) context.get("request");
Map session = context.getSession();
Map application = context.getApplication();
设置值
request.put(obj,obj);
session.put(obj,obj);
application.put(obj,obj);
以上三个分别对request,session,application作用域传输对象
在jsp中获得这三个作用域的对象
<s:property value="#request.obj"/>,如:<s:property value="#request.user.userName"/>
<s:property value="#session.obj"/>,如:<s:property value="#request.session.userName"/>
<s:property value="#application.obj"/>,如:<s:property value="#application.session.userName"/>
在struts2中也可以对action作用域设置值,可以用请求来获得,如:
ActionContext.getContext().put(obj,obj);
在jsp中可以用通过以下两种方式都可以获得:
1) ${reqeustScope.obj} //相当于EL表示式
如:${requestScope.user.userNmae}
2) request.getAttribute("obj")
如:(User) request.getAttribute("obj").getUserName();
2.与serlvet API耦合的方式
在一个action中的获得方式:
HttpServletRequest request = ServletActionContext().getRequest();
HttpSession session = ServletActionContext().getSession();
ServletContext application = ServletActionContext().getSerlvetContext();
设置值与serlvet或struts1.X都是一样的了
在jsp中获得这三个作用域的对象时,要按struts2.X的方式来取,方式同上
3.通过实现org.apache.struts2.interceptor.ServletResponseAware,
org.apache.struts2.interceptor.ServletResponseAware
接口来获得
如:
public class UserAction extends ActionSupport implements ServletRequestAware,ServletResponseAware{
private HttpServletResponse response;
private HttpServletRequest request;
public void setServletResponse(HttpServletResponse response) {
response.setCharacterEncoding("utf-8");
this.response = response;
}
public void setServletRequest(HttpServletRequest request) {
this.request = request;
}
}
在一个action中的获得方式:
//ServletContext context = ServletActionContext.getServletContext();
ActionContext context = ActionContext.getContext();
Map request = (Map) context.get("request");
Map session = context.getSession();
Map application = context.getApplication();
设置值
request.put(obj,obj);
session.put(obj,obj);
application.put(obj,obj);
以上三个分别对request,session,application作用域传输对象
在jsp中获得这三个作用域的对象
<s:property value="#request.obj"/>,如:<s:property value="#request.user.userName"/>
<s:property value="#session.obj"/>,如:<s:property value="#request.session.userName"/>
<s:property value="#application.obj"/>,如:<s:property value="#application.session.userName"/>
在struts2中也可以对action作用域设置值,可以用请求来获得,如:
ActionContext.getContext().put(obj,obj);
在jsp中可以用通过以下两种方式都可以获得:
1) ${reqeustScope.obj} //相当于EL表示式
如:${requestScope.user.userNmae}
2) request.getAttribute("obj")
如:(User) request.getAttribute("obj").getUserName();
2.与serlvet API耦合的方式
在一个action中的获得方式:
HttpServletRequest request = ServletActionContext().getRequest();
HttpSession session = ServletActionContext().getSession();
ServletContext application = ServletActionContext().getSerlvetContext();
设置值与serlvet或struts1.X都是一样的了
在jsp中获得这三个作用域的对象时,要按struts2.X的方式来取,方式同上
3.通过实现org.apache.struts2.interceptor.ServletResponseAware,
org.apache.struts2.interceptor.ServletResponseAware
接口来获得
如:
public class UserAction extends ActionSupport implements ServletRequestAware,ServletResponseAware{
private HttpServletResponse response;
private HttpServletRequest request;
public void setServletResponse(HttpServletResponse response) {
response.setCharacterEncoding("utf-8");
this.response = response;
}
public void setServletRequest(HttpServletRequest request) {
this.request = request;
}
}