Struts2 中request,session,application三种请求方法
搭建 struts2框架,需要自己来搭建,在这里我所使用的版本是struts2.2.3,版本不影响自己来完成request,session,application三种请求方法
1、在 src下建三个action类:OneRSATestAction、TwoRSATestAction、ThreeRSATestAction
2、在 WebRoot下建一个首页index.jsp和三个结果页面:testOne.jsp、testTwo.jsp、Three.jsp(也可以建一个结果页面,不过为了便于测试结果,在这里建三个页面,因为在struts.xml,本人使用的是通配符作为控制)。
Struts.xml代码:
<?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>
 
    <constant name="struts.enable.DynamicMethodInvocation" value="false" />
    <constant name="struts.devMode" value="false" />
 
    <package name="default" namespace="/" extends="struts-default">
 
        <action name="*_*" class="com.monkey.threeRSA.action.{2}Action" method="{1}">
            <result>/{1}. jsp</result>
        </action>
    </package>
3、代码填写:
a)  OneRSATestAction.java
package com.monkey.threeRSA.action;
 
import java.util.Map;
 
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
 
public class OneRSATestAction extends ActionSupport {
 
   /**
    * 第一种获取 request、session、application的方法
    */
   private static final long serialVersionUID = 1L;
   private Map request;
   private Map session;
   private Map application;
  
   public OneRSATestAction()
   {
      request = (Map)ActionContext.getContext().get("request");
      session=ActionContext.getContext().getSession();
      application=ActionContext.getContext().getApplication();
   }
  
   public String testOne()
   {
      request.put("r1", "第一种获取 request"); 
      session.put("s1", "第一种获取 session"); 
      application.put("a1", "第一种获取 application");
      return SUCCESS;
   }
}
b) TwoRSATestAction.java
package com.monkey.threeRSA.action;
 
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 TwoRSATestAction extends ActionSupport implements RequestAware,SessionAware,ApplicationAware {
 
   /**
    * 第二种获取 request、session、application的方法
    */
   private static final long serialVersionUID = 1L;
   private Map<String, Object> request;
   private Map<String, Object> session;
   private Map<String, Object> application;
  
   public String testTwo()
   {
      request.put("r2", "第二种获取 request"); 
      session.put("s2", "第二种获取 session"); 
      application.put("a2", "第二种获取 application");
      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;
   }
}
c) ThreeRSATestAction.java
package com.monkey.threeRSA.action;
 
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 ThreeRSATestAction extends ActionSupport {
 
    /**
     * 第三种获取 request、session、application的方法
     */
    private static final long serialVersionUID = 1L;
    private HttpServletRequest request; 
    private HttpSession session; 
    private ServletContext application;
   
    public ThreeRSATestAction()
    {
        request = ServletActionContext. getRequest();
        session = request.getSession();
        application = session.getServletContext() ;
    }
   
    public String testThree()
    {
        request.setAttribute("r3", "第三种获取 request"); 
        session.setAttribute("s3", "第三种获取 session"); 
        application.setAttribute("a3", "第三种获取 application");
        return SUCCESS;
    }
}
testOne.jsp
<%@ page language="java" contentType="text/html; charset=GBK"
    pageEncoding="GBK"%>
<%@ taglib uri="/struts-tags" prefix="s" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=GBK">
<title>第一种获取方法</title>
</head>
<body>
<div style="margin-top:20px; height:30px; font-size:20px;"><%=request.getAttribute("r1") %> | <s:property value="#request.r1"/></div>
<div style="margin-top:20px; height:30px; font-size:20px;"><%=session.getAttribute("s1") %> | <s:property value="#session.s1"/></div>
<div style="margin-top:20px; height:30px; font-size:20px;"><%=application.getAttribute("a1") %> | <s:property value="#application.a1"/></div>
</body>
</html>
testTwo.jsp
<%@ page language="java" contentType="text/html; charset=GBK"
    pageEncoding="GBK"%>
<%@ taglib uri="/struts-tags" prefix="s" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=GBK">
<title>第二种获取方法</title>
</head>
<body>
<div style="margin-top:20px; height:30px; font-size:20px;"><%=request.getAttribute("r2") %> | <s:property value="#request.r2"/></div>
<div style="margin-top:20px; height:30px; font-size:20px;"><%=session.getAttribute("s2") %> | <s:property value="#session.s2"/></div>
<div style="margin-top:20px; height:30px; font-size:20px;"><%=application.getAttribute("a2") %> | <s:property value="#application.a2"/></div>
</body>
</html>
testThree.jsp
<%@ page language="java" contentType="text/html; charset=GBK"
    pageEncoding="GBK"%>
<%@ taglib uri="/struts-tags" prefix="s" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=GBK">
<title>第三种获取方法</title>
</head>
<body>
<div style="margin-top:20px; height:30px; font-size:20px;"><%=request.getAttribute("r3") %> | <s:property value="#request.r3"/></div>
<div style="margin-top:20px; height:30px; font-size:20px;"><%=session.getAttribute("s3") %> | <s:property value="#session.s3"/></div>
<div style="margin-top:20px; height:30px; font-size:20px;"><%=application.getAttribute("a3") %> | <s:property value="#application.a3"/></div>
</body>
</html>
 
首页代码: index.jsp
<%@ page language="java" import="java.util.*" pageEncoding="GBK"%>
<%
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>
    <base href="<%=basePath%>">
   
    <title>My JSP 'index.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>
    <div style="margin-top:20px; height:30px; font-size:20px;"><a href="testOne_OneRSATest">第一种获取 request、session、application方法</a></div>
    <div style="margin-top:20px; height:30px; font-size:20px;"><a href="testTwo_TwoRSATest">第二种获取 request、session、application方法</a></div>
    <div style="margin-top:20px; height:30px; font-size:20px;"><a href="testThree_ThreeRSATest">第三种获取 request、session、application方法</a></div>
 </body>
</html>
 
如果要想参考整个项目下载源代码: ThreeRSA.zip