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

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

  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.     }  


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

  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.     } 
    1.   /*  
    2.      * 实现RequestAware中的方法 
    3.      */  
    4.     @Override  
    5.     public void setRequest(Map<String, Object> request) {  
    6.         this.request = request;  
    7.     }  
    8.     /*  
    9.      * 实现ApplicationAware中的方法 
    10.      */  
    11.     @Override  
    12.     public void setApplication(Map<String, Object> application) {  
    13.         this.application = application;  
    14.     }  
    15.     /*  
    16.      * 实现SessionAware中的方法 
    17.      */  
    18.     @Override  
    19.     public void setSession(Map<String, Object> session) {  
    20.         this.session = session;  
    21.     }  



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

  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.     }  


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

  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.     } 
  1. /*  
  2.      * 实现ServletRequestAware接口中的方法 
  3.      */  
  4.     @Override  
  5.     public void setServletRequest(HttpServletRequest request) {  
  6.         this.request = request;  
  7.         this.session = request.getSession();  
  8.         this.application = session.getServletContext();  
  9.     }  

action配置的struts.xml

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> 

jsp页面

  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.             } 
  1.  </head>  
  2.   
  3.     <body>  
  4.         <form name="form1">  
  5.             <div>  
  6.                 Struts2中访问web元素的四种方式<br>  
  7.                 方式一:<input type="button" value="submit1" onclick="sub('<%=basePath%>login/login1')"><br>    
  8.                 方式二:<input type="button" value="submit2" onclick="sub('<%=basePath%>login/login2')"><br>  
  9.                 方式三:<input type="button" value="submit3" onclick="sub('<%=basePath%>login/login3')"><br>  
  10.                 方式四:<input type="button" value="submit4" onclick="sub('<%=basePath%>login/login4')"><br>  
  11.             </div>  
  12.         </form>  
  13.     </body>  
  14. </html> 
success.jsp

  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> 


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值