标签:
Struts2中访问和添加request、session、application属性
public String execute() {// 如果仅仅往这三个范围放入属性,建议使用这种方法 ActionContext actionContext = ActionContext.getContext(); // 往ServletContext里放application actionContext.getApplication().put("application", "application应用范围"); actionContext.getSession().put("session", "session应用范围"); actionContext.put("request", "request应用范围"); actionContext.put("hobbies", Arrays.asList("羽毛球", "篮球", "足球", "兵乓球")); return "success"; }打印输出
${applicationScope.application }<br> ${sessionScope.session }<br> ${requestScope.request }<br>
如果相应的属性参数是对象呢?
获取HttpServletRequest/HttpSession/ServletContext/HttpServletResponse对象有两种方法:
方法一、通过ServletActionContext类直接获取:
public String rsa() throws Exception{ HttpServletRequest request=ServletActionContext.getRequest(); ServletContext servletContext=ServletActionContext.getServletContext(); request.getSession(); HttpServletResponse response=ServletActionContext.getResponse(); return "scope"; }方法二、实现指定接口,由struts框架运行时注入:
public class HelloWorldAction implements ServletRequestAware,ServletResponseAware,ServletContextAware{ private HttpServletRequest request; private ServletContext servletContext; private HttpServletResponse response; public void setServletRequest(HttpServletRequest req){ this.request=req;//在运行期由框架注入,不是由自己设置 } public void setServletResponse(HttpServletResponse res){ this.response=res; } public void setServletContext(ServletContext ser){ this.servletContext=ser; } }一般建议第一种方法,比较简单
完整代码
<?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="csdn" namespace="/ask" extends="struts-default"> <action name="msg" class="struts2.example.action.HelloWorldAction" method="execute"> <!-- 定义处理结果与视图资源之间的关系 --> <result name="success">/WEB-INF/page/showInfo.jsp</result> </action> </package> </struts>
package struts2.example.action; import java.util.Arrays; import javax.servlet.ServletContext; import javax.servlet.http.HttpServletRequest; import org.apache.struts2.ServletActionContext; import com.opensymphony.xwork2.ActionContext; public class HelloWorldAction { public String execute() {// 如果仅仅往这三个范围放入属性,建议使用这种方法 ActionContext actionContext = ActionContext.getContext(); // 往ServletContext里放application actionContext.getApplication().put("application", "application应用范围"); actionContext.getSession().put("session", "session应用范围"); actionContext.put("request", "request应用范围"); actionContext.put("hobbies", Arrays.asList("羽毛球", "篮球", "足球", "兵乓球")); return "success"; } public String rsa() throws Exception { HttpServletRequest request = ServletActionContext.getRequest();// 得到原始的request对象 // request.getRealPath(path); 得到站点目录上某个文件的绝对路径 ServletContext servletContext = ServletActionContext .getServletContext(); request.setAttribute("request", "*********request应用范围******"); request.getSession().setAttribute("session", "*********session应用范围********"); // HttpServletResponse response = ServletActionContext.getResponse(); servletContext.setAttribute("application", "********application应用范围********"); return "success"; } }
<pre name="code" class="html"><%@ page language="java" import="java.util.*" pageEncoding="utf-8"%> <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%> <html> <head> <title>My JSP 'showInfo.jsp' starting page</title> </head> <body> ${applicationScope.application }<br> ${sessionScope.session }<br> ${requestScope.request }<br> =============================<br> <c:forEach items="${hobbies }" var="hobby"> ${hobby}<br> </c:forEach> </body> </html>
My Eclipse在Java EE5以后把jstl集成进来,不用在导入jstl相应的jar包
Struts2之访问和添加request、session、application属性
标签:
原文地址:http://blog.csdn.net/lindonglian/article/details/46559769