本文主要是介绍Struts2里如何取得request,session,application,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
- 一共有4种方法
第一种:
取得MAP类型的request,session,application
在java文件里写
ActionContext.getContext()是获得Action的上下文,然后可以获得MAP类型的request等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;} }
- 在输出页面可获得参数
<%@ 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+"/"; %><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html><head><title>My JSP 'MyJsp.jsp' 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">--></head><body>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></body> </html>
- 第二种:
实现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 stubthis.request=request;}public void setSession(Map<String, Object> session) {// TODO Auto-generated method stubthis.session= session;}public void setApplication(Map<String, Object> application) {// TODO Auto-generated method stubthis.application = application;} }
前面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;}}
- 第四种:
也是ioc的方式
代码如下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 stubthis.request = request;session = request.getSession();application = session.getServletContext();}}
这篇关于Struts2里如何取得request,session,application的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!