标签:
第一种:
取得MAP类型的request,session,application
在java文件里写
package com.xjtu.st; import java.util.Map; import com.opensymphony.xwork2.ActionContext; import com.opensymphony.xwork2.ActionSupport; public class LoginAction extends ActionSupport{ private Map request; private Map session; private Map application; public LoginAction(){ request = (Map)ActionContext.getContext().get("request"); session = ActionContext.getContext().getSession(); application = ActionContext.getContext().getApplication(); } public String execute(){ request.put("rs", "rs"); session.put("ss", "ss"); application.put("as", "as"); return SUCCESS; } }
ActionContext.getContext()是获得Action的上下文,然后可以获得MAP类型的request等
在输出页面可获得参数
<%@ page language="java" import="java.util.*" pageEncoding="GBK"%> <%@ taglib uri="/struts-tags" prefix="s" %> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %> <title>My JSP ‘MyJsp.<a href="http://www.2cto.com/kf/web/jsp/" target="_blank" class="keylink">jsp</a>‘ starting page</title> <meta http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="expires" content="0"> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> <meta http-equiv="description" content="This is my page"> <!-- <link rel="stylesheet" type="text/css" href="styles.css"> --> hello <br> <s:property value="#request.rs"> <br><!-- //通过标签取值--> <s:property value="#session.ss"> <br> <s:property value="#application.as"> <br> <%=request.getAttribute("rs") %><br><!-- 用原始的方法取值--> <%=session.getAttribute("ss") %><br> <%=application.getAttribute("as") %><br> <!--html> </s:property></s:property></s:property>
第二种:
实现RequestAware,SessionAware,ApplicationAware接口,该方法成为DI(依赖注入)或者IOC(控制反转)。代码如下
package com.xjtu.st; 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.ActionContext; import com.opensymphony.xwork2.ActionSupport; public class LoginAction extends ActionSupport implements RequestAware,SessionAware,ApplicationAware{ private Map request; private Map session; private Map application; public String execute(){ request.put("rs", "rrs"); session.put("ss", "sss"); application.put("as", "aas"); return SUCCESS; } public void setRequest(Map<string, object=""> request) { // TODO Auto-generated method stub this.request=request; } public void setSession(Map<string, object=""> session) { // TODO Auto-generated method stub this.session= session; } public void setApplication(Map<string, object=""> application) { // TODO Auto-generated method stub this.application = application; } } </string,></string,></string,>
前面2种是取得Map类型的,后两种是取得HtteServletRequest,HttpSession,ServletContext类型的办法第三种:
代码如下
package com.xjtu.st; import java.util.Map; import javax.servlet.ServletContext; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpSession; import org.apache.struts2.ServletActionContext; import org.apache.struts2.interceptor.ApplicationAware; import org.apache.struts2.interceptor.RequestAware; import org.apache.struts2.interceptor.SessionAware; import com.opensymphony.xwork2.ActionContext; import com.opensymphony.xwork2.ActionSupport; public class LoginAction extends ActionSupport { private HttpServletRequest request; private HttpSession session; private ServletContext application; public LoginAction(){ request= ServletActionContext.getRequest(); session = request.getSession(); application = session.getServletContext(); } public String execute(){ request.setAttribute("rs", "rrrs"); session.setAttribute("ss", "ssss"); application.setAttribute("as", "aaas"); return SUCCESS; } }
package com.xjtu.st; import java.util.Map; import javax.servlet.ServletContext; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpSession; import org.apache.struts2.ServletActionContext; import org.apache.struts2.interceptor.ApplicationAware; import org.apache.struts2.interceptor.RequestAware; import org.apache.struts2.interceptor.ServletRequestAware; import org.apache.struts2.interceptor.SessionAware; import com.opensymphony.xwork2.ActionContext; import com.opensymphony.xwork2.ActionSupport; public class LoginAction extends ActionSupport implements ServletRequestAware{ private HttpServletRequest request; private HttpSession session; private ServletContext application; public String execute(){ request.setAttribute("rs", "rrrrs"); session.setAttribute("ss", "sssss"); application.setAttribute("as", "aaaas"); return SUCCESS; } public void setServletRequest(HttpServletRequest request) { // TODO Auto-generated method stub this.request = request; session = request.getSession(); application = session.getServletContext(); } }
Struts2里如何取得request,session,application
标签:
原文地址:http://www.cnblogs.com/sunxun/p/4257407.html