struts2访问web元素方法

Struts2中,动作类虽然继承ActionSupport类,可以直接写我们自己定义的方法,但是却不能像在Struts1中,对reques/response/application/HttpServletRequest等等一些Web元素进行操作,所以Struts2提供了RequestAware,SessionAware,ApplicationAware/ServletRequestAware....接口.

实现这些接口就可以对其进行想要的操作了.

package actions;

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;

import com.opensymphony.xwork2.ModelDriven;

import enetitys.Student;

public class StudentAction extends ActionSupport implements RequestAware,SessionAware,ApplicationAware,ModelDriven<Student>{

    private Student student=new Student();

    @Override

    public Student getModel() {

       return student;

    }

    //实现了RequestAware,SessionAware,ApplicationAware接口的类.

    //谁调用执行这个action,谁就来初始化这些值

private Map<String,Object> request;   

    private Map<String,Object> session;

    private Map<String,Object> application;

    @Override

    public void setApplication(Map<String, Object> application) {

       this.application=application;

    }

    @Override

    public void setRequest(Map<String, Object> request) {

       this.request=request;

    }

    @Override

    public void setSession(Map<String, Object> session) {

       this.session=session;

    }

    public String delete(){

       request.put("list", "把一个集合的数据删掉");

       return "delete";

    }

Action中实现RequestAware,SessionAware,ApplicationAware接口,

实现这些接口,都会有相对应的setXXX()方法.就是说谁来执行这个action中的相应方法,

谁就对这些个对象进行初始化(Spring中的注入).也就是Struts2为我们进行了初始化,所以这三个值都不需要自己初始化.

Delete.jsp页面中通过el表达式访问request中存放的key为list的值

<body>

    ${request.list}

</body>

页面访问的时候:

Struts2访问Web元素(RequestAware,SessionAware,ApplicationAware)

还有一种方法,但是需要依赖于Struts2.也就是上一篇日志中,访问栈中的Stack Context属性值,

只需要在action中定义相应名称的Map集合,在构造函数或一个什么方法中进行取值就行,在当前action的运行环境中取值:

private Map request;

private Map session;

private Map application;

    public UserAction(){

       request=(Map)ActionContext.getContext().get("request");

       session=ActionContext.getContext().getSession();

       application=ActionContext.getContext().getApplication();

    }

******************************************************************

Struts2中访问web元素的四种方式及前台jsp页面获取后台值的方式

四种方式:

1. 通过ActionContext来访问request,session,application对象

2. 通过实现RequestAware、SessionAware、ApplicationAware接口来访问request,session,application对象

3. 通过ServletActionContext来访问request,session,application对象

4. 通过实现ServletRequestAware接口来访问request,session,application对象

演示代码:

方式一:

Java代码 复制代码
  1. /**
  2. * 通过ActionContext来访问request,session,application对象
  3. * @author 健
  4. */ 
  5. public class UserAction1 extends ActionSupport{ 
  6.     /**
  7.      * 序列化
  8.      */ 
  9.     private static final long serialVersionUID = 1L; 
  10.     /**
  11.      * request
  12.      */ 
  13.     private Map <String,Object>request; 
  14.     /**
  15.      * response
  16.      */ 
  17.     private Map <String,Object>session; 
  18.     /**
  19.      * application
  20.      */ 
  21.     private Map <String,Object>application; 
  22.     /**
  23.      * 添加用户
  24.      * @return 用户是否添加成功
  25.      */ 
  26.     @SuppressWarnings("unchecked"
  27.     @Override 
  28.     public String execute(){ 
  29.         System.out.println("通过ActionContext来访问request,session,application对象"); 
  30.         // 初始化 
  31.         request = (Map<String,Object>)ActionContext.getContext().get("request"); 
  32.         session = ActionContext.getContext().getSession(); 
  33.         application = ActionContext.getContext().getApplication(); 
  34.         // 赋值 
  35.         request.put("requestKey", "requestValue"); 
  36.         session.put("sessionKey", "sessionValue"); 
  37.         application.put("applicationKey", "applicationValue"); 
  38.         return "success"
  39.     } 

方式二:

Java代码 复制代码
  1. /**
  2. * 通过实现RequestAware、SessionAware、ApplicationAware接口来访问request,session,application对象
  3. * @author 健
  4. */ 
  5. public class UserAction2 extends ActionSupport implements RequestAware,SessionAware,ApplicationAware{ 
  6.     /**
  7.      * 序列化
  8.      */ 
  9.     private static final long serialVersionUID = 1L; 
  10.     /**
  11.      * request
  12.      */ 
  13.     private Map <String,Object>request; 
  14.     /**
  15.      * response
  16.      */ 
  17.     private Map <String,Object>session; 
  18.     /**
  19.      * application
  20.      */ 
  21.     private Map <String,Object>application; 
  22.     /**
  23.      * 控制器
  24.      */ 
  25.     @SuppressWarnings("unchecked"
  26.     @Override 
  27.     public String execute(){ 
  28.         System.out.println("通过实现RequestAware、SessionAware、ApplicationAware接口来访问request,session,application对象"); 
  29.         // 赋值 
  30.         request.put("requestKey", "requestValue"); 
  31.         session.put("sessionKey", "sessionValue"); 
  32.         application.put("applicationKey", "applicationValue"); 
  33.         return "success"
  34.     } 
  35.     /*
  36.      * 实现RequestAware中的方法
  37.      */ 
  38.     @Override 
  39.     public void setRequest(Map<String, Object> request) { 
  40.         this.request = request; 
  41.     } 
  42.     /*
  43.      * 实现ApplicationAware中的方法
  44.      */ 
  45.     @Override 
  46.     public void setApplication(Map<String, Object> application) { 
  47.         this.application = application; 
  48.     } 
  49.     /*
  50.      * 实现SessionAware中的方法
  51.      */ 
  52.     @Override 
  53.     public void setSession(Map<String, Object> session) { 
  54.         this.session = session; 
  55.     } 

方式三:

Java代码 复制代码
  1. +/**
  2. * 通过ServletActionContext来访问request,session,application对象
  3. * @author 健
  4. */ 
  5. public class UserAction3 extends ActionSupport{ 
  6.     /**
  7.      * 序列化
  8.      */ 
  9.     private static final long serialVersionUID = 1L; 
  10.     /**
  11.      * request
  12.      */ 
  13.     private HttpServletRequest request; 
  14.     /**
  15.      * response
  16.      */ 
  17.     private HttpSession session; 
  18.     /**
  19.      * application
  20.      */ 
  21.     private ServletContext application; 
  22.     /**
  23.      * 控制器
  24.      */ 
  25.     @SuppressWarnings("unchecked"
  26.     @Override 
  27.     public String execute(){ 
  28.         System.out.println("通过ServletActionContext来访问request,session,application对象"); 
  29.         // 初始化 
  30.         request = ServletActionContext.getRequest(); 
  31.         session = request.getSession(); 
  32.         application = session.getServletContext(); 
  33.         // 赋值 
  34.         request.setAttribute("requestKey", "requestValue"); 
  35.         session.setAttribute("sessionKey", "sessionValue"); 
  36.         application.setAttribute("applicationKey", "applicationValue"); 
  37.         return "success"
  38.     } 

方式四:

Java代码 复制代码
  1. /**
  2. * 通过实现ServletRequestAware接口来访问request,session,application对象
  3. * @author 健
  4. */ 
  5. public class UserAction4 extends ActionSupport implements ServletRequestAware{ 
  6.     /**
  7.      * 序列化
  8.      */ 
  9.     private static final long serialVersionUID = 1L; 
  10.     /**
  11.      * request
  12.      */ 
  13.     private HttpServletRequest request; 
  14.     /**
  15.      * response
  16.      */ 
  17.     private HttpSession session; 
  18.     /**
  19.      * application
  20.      */ 
  21.     private ServletContext application; 
  22.     /**
  23.      * 控制器
  24.      */ 
  25.     @SuppressWarnings("unchecked"
  26.     @Override 
  27.     public String execute(){ 
  28.         System.out.println("通过实现ServletRequestAware接口来访问request,session,application对象"); 
  29.         // 赋值 
  30.         request.setAttribute("requestKey", "requestValue"); 
  31.         session.setAttribute("sessionKey", "sessionValue"); 
  32.         application.setAttribute("applicationKey", "applicationValue"); 
  33.         return "success"
  34.     } 
  35.     /*
  36.      * 实现ServletRequestAware接口中的方法
  37.      */ 
  38.     @Override 
  39.     public void setServletRequest(HttpServletRequest request) { 
  40.         this.request = request; 
  41.         this.session = request.getSession(); 
  42.         this.application = session.getServletContext(); 
  43.     } 

上面的action配套的struts.xml及jsp页面

struts.xml

Xhtml代码 复制代码
  1. <struts> 
  2.     <!-- 配置开发模式:修改不用重启服务器 --> 
  3.     <constant name="struts.devMode" value="true"/> 
  4.     <package name="" namespace="/login" extends="struts-default"> 
  5.         <action name="login*" class="com.wj.struts2.action.UserAction{1}"> 
  6.             <result name="success">/success.jsp</result> 
  7.             <result name="failure">/failure.jsp</result> 
  8.         </action> 
  9.     </package> 
  10. </struts> 

index.jsp

Xhtml代码 复制代码
  1. <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> 
  2. <%@taglib uri="/struts-tags" prefix="s" %> 
  3. <
  4.     String path = request.getContextPath(); 
  5.     String basePath = request.getScheme() + "://" 
  6.             + request.getServerName() + ":" + request.getServerPort() 
  7.             + path + "/"; 
  8. %> 
  9. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> 
  10. <html> 
  11.     <head> 
  12.         <base href="<%=basePath%>"> 
  13.  
  14.         <title>Struts2_AccessWebElements</title> 
  15.         <meta http-equiv="pragma" content="no-cache"> 
  16.         <meta http-equiv="cache-control" content="no-cache"> 
  17.         <meta http-equiv="expires" content="0"> 
  18.         <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> 
  19.         <meta http-equiv="description" content="This is my page"> 
  20.         <!--
  21.         <link rel="stylesheet" type="text/css" href="styles.css" mce_href="styles.css">
  22.         --> 
  23.         <mce:script language="javascript"><!-- 
  24.             function sub(str){ 
  25.                 document.form1.action = str
  26.                 document.form1.submit(); 
  27.             } 
  28.          
  29. // --></mce:script> 
  30.     </head> 
  31.  
  32.     <body> 
  33.         <form name="form1"> 
  34.             <div> 
  35.                 Struts2中访问web元素的四种方式<br> 
  36.                 方式一:<input type="button" value="submit1" onclick="sub('<%=basePath%>login/login1')"><br>   
  37.                 方式二:<input type="button" value="submit2" onclick="sub('<%=basePath%>login/login2')"><br> 
  38.                 方式三:<input type="button" value="submit3" onclick="sub('<%=basePath%>login/login3')"><br> 
  39.                 方式四:<input type="button" value="submit4" onclick="sub('<%=basePath%>login/login4')"><br> 
  40.             </div> 
  41.         </form> 
  42.     </body> 
  43. </html> 

success.jsp

Xhtml代码 复制代码
  1. <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> 
  2. <%@taglib uri="/struts-tags" prefix="s" %> 
  3. <
  4.     String path = request.getContextPath(); 
  5.     String basePath = request.getScheme() + "://" 
  6.             + request.getServerName() + ":" + request.getServerPort() 
  7.             + path + "/"; 
  8. %> 
  9. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> 
  10. <html> 
  11.     <head> 
  12.         <base href="<%=basePath%>"> 
  13.         <title>Struts2_AccessWebElements</title> 
  14.         <meta http-equiv="pragma" content="no-cache"> 
  15.         <meta http-equiv="cache-control" content="no-cache"> 
  16.         <meta http-equiv="expires" content="0"> 
  17.         <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> 
  18.         <meta http-equiv="description" content="This is my page"> 
  19.         <!--
  20.         <link rel="stylesheet" type="text/css" href="styles.css" mce_href="styles.css">
  21.         --> 
  22.     </head> 
  23.  
  24.     <body> 
  25.         requestKey---<s:property value="#request.requestKey"/>|<%=request.getAttribute("requestKey")%><br> 
  26.         sessionKey---<s:property value="#session.sessionKey"/>|<%=session.getAttribute("sessionKey")%><br> 
  27.         applicationKey---<s:property value="#application.applicationKey"/>|<%=application.getAttribute("applicationKey")%><br> 
  28.         -------------------------------------------- 
  29.         <s:debug></s:debug> 
  30.     </body> 
  31. </html> 

前台jsp页面获取后台值的方式

<s:property value="#request.requestKey"/>|<%=request.getAttribute("requestKey")%>

<s:property value="#session.sessionKey"/>|<%=session.getAttribute("sessionKey")%>

<s:property value="#application.applicationKey"/>|<%=application.getAttribute("applicationKey")%>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值