Action的搭建及applicationrequestSession的运用 多种方法

Posted 丶疏影横斜

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Action的搭建及applicationrequestSession的运用 多种方法相关的知识,希望对你有一定的参考价值。

 1 <%@ page language="java" contentType="text/html; charset=UTF-8"
 2     pageEncoding="UTF-8"%>
 3 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
 4 <html>
 5 <head>
 6 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
 7 <title>Insert title here</title>
 8 </head>
 9 <body>
10 添加测试的web资源
11 <%
12 application.setAttribute("app1", "测试的application")  ;
13 application.setAttribute("app12", "测试的application2")  ;
14 session.setAttribute("ses1", "测试的Session") ;
15 
16 request.setAttribute("req1", "测试的request") ;
17 
18 
19 %>
20 <br>
21 <a href="testAction.action?user=tom" >测试Action类访问web资源</a>
22 <br><br><br>
23 <a href="testActionAware.action?user=tom&user=rose" >以Aware方式测试Action类访问web资源</a>
24 <br><br><br>
25 <a href="testServletAction.action?user=tom" >访问原生web资源</a>
26 <br><br><br>
27 <a href="testServletAware.action?user=tom" >以接口方式访问原生web资源</a>
28 </body>
29 </html>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!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=UTF-8">
<title>Insert title here</title>
</head>
<body>
显示application的属性
<br>
app1=${app1 }
<br>
app12=${app12 }
<br>
appx=${applicationScope.appx }

<br><br>

显示Session的属性
<br><br>

ses1=${ses1 }
<br><br>
ses2=${ses2 }

<br><br>
req1=${req1 }
<br><br>
req2=${req2 }
<br><br>
<% 
String user = request.getParameter("user") ;
Object haha = session.getAttribute("ses1") ;
out.print("user = "+user) ;
out.print("<br><br> ") ;
out.print("haha = "+haha.toString()) ;
String ww = request.getParameter("ww") ;
%>
<br>
<%out.print("ww= "+ww) ; %>
</body>
</html>

Struts

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
    "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
    "http://struts.apache.org/dtds/struts-2.3.dtd">

<struts>

   <!-- 设置过滤的扩展名 -->
   <constant name="struts.action.extension" value="do,action,,"></constant>

    <!-- 定义包 -->
    <package name="index" extends="struts-default" namespace="">
    
            <action name="testAction" class="com.hanqi.action.TestAction" method="testWeb">
            
                    <result type="redirect">test1.jsp</result>
            
            </action>
            
            <!-- 解耦的接口方式 -->
            <action name="testActionAware" class="com.hanqi.TestActionAware" method="testAware">
            
                    <result >test1.jsp</result>
            
            </action>
    
            <!-- 以ServletActionContext方式 -->
            <action name="testServletAction" class="com.hanqi.action.TestServletAction" 
                            method="testServletAction">
            
                    <result >test1.jsp</result>
            
            </action>
    
            <action name="testServletAware" class="com.hanqi.action.TestServletActionAware"
                        method="testServletAware">
                        
                        
                        <result >test1.jsp</result>
                        
                        </action>
    
    </package>
    
</struts>
 1 package com.hanqi.action;
 2 
 3 import javax.servlet.ServletContext;
 4 import javax.servlet.http.HttpServletRequest;
 5 import javax.servlet.http.HttpSession;
 6 
 7 import org.apache.struts2.interceptor.ServletRequestAware;
 8 import org.apache.struts2.util.ServletContextAware;
 9 
10 public class TestServletActionAware
11         implements ServletRequestAware, ServletContextAware {
12 
13     private ServletContext sc ;
14     private HttpServletRequest hsr ;
15     private HttpSession hs ;
16     
17     @Override
18     public void setServletContext(ServletContext arg0) {
19         // TODO 自动生成的方法存根
20         sc = arg0 ;
21     }
22 
23     @Override
24     public void setServletRequest(HttpServletRequest arg0) {
25         // TODO 自动生成的方法存根
26         
27         hsr = arg0 ;
28         
29         hs = arg0.getSession() ;
30         
31     }
32 
33     public String testServletAware()
34     {
35         System.out.println("app1 = " + sc.getAttribute("app1"));
36         
37         sc.setAttribute("appx", "新的app");
38         
39         System.out.println("ses1 = " + hs.getAttribute("ses1"));
40         
41         hs.setAttribute("ses2", "新的ses2");
42         
43         hsr.setAttribute("req2", "新的req2");
44         
45         System.out.println("user = " +hsr.getParameter("user"));
46         
47     
48         
49         return "success" ;
50     }
51     
52     
53 }
 1 package com.hanqi.action;
 2 
 3 import javax.servlet.ServletContext;
 4 import javax.servlet.http.HttpServletRequest;
 5 import javax.servlet.http.HttpSession;
 6 
 7 import org.apache.struts2.ServletActionContext;
 8 
 9 public class TestServletAction
10 {
11             
12     public String testServletAction()
13     {
14         //访问原生的web资源
15         
16         //1得到ServletContext,相当于application 
17         ServletContext sc = ServletActionContext.getServletContext();
18         
19         System.out.println("app1 = " + sc.getAttribute("app1"));
20          
21         sc.setAttribute("appx", "原生方式放入的内容");
22             
23         //2访问Session
24         HttpServletRequest hsr = ServletActionContext.getRequest() ;
25         
26         HttpSession hs = hsr.getSession() ;
27         
28         System.out.println("ses1 = " + hs.getAttribute("ses1"));
29         
30         hs.setAttribute("ses2", "原生方式放入的Session");
31         
32         //3访问request
33         System.out.println("req1 = " + hsr.getAttribute("req1"));
34         
35         hsr.setAttribute("req2", "原生方式放入的request");
36         
37         //4访问parameter
38         System.out.println("user1 = " +hsr.getParameter("user") );
39         System.out.println("user2 = " +hsr.getParameter("user") );
40         
41         return "success" ;
42     }
43 }
package com.hanqi.action;

import java.util.Map;

import com.opensymphony.xwork2.ActionContext;

//解耦方式
public class TestAction 
{

    //
    
    public String testWeb()
    {
        System.out.println("hah");
        
        //单例
        //Action实例的上下文对象
        ActionContext ac = ActionContext.getContext() ;
        
        //1获取application资源
        Map<String, Object> mapApp = ac.getApplication() ;
        
        System.out.println("app1 = " + mapApp.get("app1"));
        
        System.out.println("app12 = " + mapApp.get("app12"));
        
        //放入新的键值对
        mapApp.put("appx", "新放入的application") ;
        
        
        //2.获取Session的资源
        Map<String, Object> mapses = ac.getSession() ;
        
        System.out.println("ses1=" + mapses.get("ses1"));
        
        //放入新的键值对
        mapses.put("ses2", "新放入的session") ;
        
        //移除
        mapses.remove("ses1") ;
        
        
        //3获取request资源
        Map<String, Object> mapreq = (Map<String, Object>) ac.get("request");
        
        System.out.println("req1 = " + mapreq.get("req1"));
        
        //放入新的键值对
        mapreq.put("req2", "新放入的request") ;
        
        
        //4直接获取传递的参数
        Map<String, Object> mappar = ac.getParameters() ;
        
        //参数值被封装成String数组
        String[ ] array = (String[ ])mappar.get("user") ;
    
        for(String s : array)
        {
            System.out.println("user = " + s);
        }
        
        //放入
        mappar.put("ww", new String[ ]{("qweqeq")}) ;
        
        return "success" ;
    }
    
}
package com.hanqi;

import java.util.Map;

import org.apache.struts2.interceptor.ApplicationAware;
import org.apache.struts2.interceptor.ParameterAware;
import org.apache.struts2.interceptor.RequestAware;
import org.apache.struts2.interceptor.SessionAware;

public class TestActionAware implements ApplicationAware , SessionAware, RequestAware, ParameterAware{

    private Map<String, Object> mapApp ;
    private Map<String, Object> mapses ;
    private Map<String, Object> mapreq ;
    private Map<String, String[ ]> mappar ;
    
    //实现ApplicationAware接口的方法
    //提供给Struts2进行调用 , 放入Application的数据集合
    @Override
    public void setApplication(Map<String, Object> arg0) {
        // TODO 自动生成的方法存根

        //接收
        mapApp = arg0 ;
        
    }

    
    public String testAware()
    {
        
        //1访问application
        System.out.println("app1 = " + mapApp.get("app1"));
        
        mapApp.put("app12", "以Aware方式放入的application") ;
        
        //2访问Session
        //System.out.println("ses1 = " + mapses.get("ses1"));
        
        mapses.put("ses2", "以Aware方式放入的Session") ;
        
        //3访问request
        System.out.println("req1 = " + mapreq.get("req1"));
        
        mapreq.put("req2", "以Aware方式放入的request") ;
        
        //4 访问Parameter
        String[ ] str = mappar.get("user") ;
        for(String t : str)
        {
                System.out.println("user = " + t);
        }
        return "success" ;
    }


    @Override
    public void setSession(Map<String, Object> arg0) {
        // TODO 自动生成的方法存根
        
        mapses = arg0 ;
        
    }


    @Override
    public void setRequest(Map<String, Object> arg0) {
        // TODO 自动生成的方法存根
        mapreq = arg0 ;
    }


    @Override
    public void setParameters(Map<String, String[]> arg0) {
        // TODO 自动生成的方法存根
        mappar = arg0 ;
    }
    
}

 

以上是关于Action的搭建及applicationrequestSession的运用 多种方法的主要内容,如果未能解决你的问题,请参考以下文章

spring mvc4.1.6 + spring4.1.6 + hibernate4.3.11 + mysql5.5.25 开发环境搭建及相关说明

struts2.3.24 + spring4.1.6 + hibernate4.3.11 + mysql5.5.25开发环境搭建及相关说明

mybatis实战教程(mybatis in action)之一:开发环境搭建

1. 开篇-springboot环境搭建

JAVAEE学习——struts2_01:简介搭建架构配置action类详解和练习:客户列表

mybatis实战教程(mybatis in action)之一:开发环境搭建