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

转自http://blog.csdn.net/developer_java/archive/2010/09/14/5882976.aspx

Struts2中访问web元素的四种方式(访问requst、session、application)

 

四种方式:

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

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

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

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

 

演示代码:

方式一:

view plaincopy to clipboardprint?
 
public class UserAction1 extends ActionSupport{  
     
    private static final long serialVersionUID = 1L;  
     
    private Map <String,Object>request;  
     
    private Map <String,Object>session;  
     
    private Map <String,Object>application;  
     
    @SuppressWarnings("unchecked")  
    @Override 
    public String execute(){  
        System.out.println("通过ActionContext来访问request,session,application对象");  
        // 初始化  
        request = (Map<String,Object>)ActionContext.getContext().get("request");  
        session = ActionContext.getContext().getSession();  
        application = ActionContext.getContext().getApplication();  
        // 赋值  
        request.put("requestKey", "requestValue");  
        session.put("sessionKey", "sessionValue");  
        application.put("applicationKey", "applicationValue");  
        return "success";  
    }  


public class UserAction1 extends ActionSupport{
 
 private static final long serialVersionUID = 1L;
 
 private Map <String,Object>request;
 
 private Map <String,Object>session;
 
 private Map <String,Object>application;
 
 @SuppressWarnings("unchecked")
 @Override
 public String execute(){
  System.out.println("通过ActionContext来访问request,session,application对象");
  // 初始化
  request = (Map<String,Object>)ActionContext.getContext().get("request");
  session = ActionContext.getContext().getSession();
  application = ActionContext.getContext().getApplication();
  // 赋值
  request.put("requestKey", "requestValue");
  session.put("sessionKey", "sessionValue");
  application.put("applicationKey", "applicationValue");
  return "success";
 }
}

方式二:

view plaincopy to clipboardprint?
 
public class UserAction2 extends ActionSupport implements RequestAware,SessionAware,ApplicationAware{  
     
    private static final long serialVersionUID = 1L;  
     
    private Map <String,Object>request;  
     
    private Map <String,Object>session;  
     
    private Map <String,Object>application;  
     
    @SuppressWarnings("unchecked")  
    @Override 
    public String execute(){  
        System.out.println("通过实现RequestAware、SessionAware、ApplicationAware接口来访问request,session,application对象");  
        // 赋值  
        request.put("requestKey", "requestValue");  
        session.put("sessionKey", "sessionValue");  
        application.put("applicationKey", "applicationValue");  
        return "success";  
    }  
     
    @Override 
    public void setRequest(Map<String, Object> request) {  
        this.request = request;  
    }  
     
    @Override 
    public void setApplication(Map<String, Object> application) {  
        this.application = application;  
    }  
     
    @Override 
    public void setSession(Map<String, Object> session) {  
        this.session = session;  
    }  


public class UserAction2 extends ActionSupport implements RequestAware,SessionAware,ApplicationAware{
 
 private static final long serialVersionUID = 1L;
 
 private Map <String,Object>request;
 
 private Map <String,Object>session;
 
 private Map <String,Object>application;
 
 @SuppressWarnings("unchecked")
 @Override
 public String execute(){
  System.out.println("通过实现RequestAware、SessionAware、ApplicationAware接口来访问request,session,application对象");
  // 赋值
  request.put("requestKey", "requestValue");
  session.put("sessionKey", "sessionValue");
  application.put("applicationKey", "applicationValue");
  return "success";
 }
 
 @Override
 public void setRequest(Map<String, Object> request) {
  this.request = request;
 }
 
 @Override
 public void setApplication(Map<String, Object> application) {
  this.application = application;
 }
 
 @Override
 public void setSession(Map<String, Object> session) {
  this.session = session;
 }
}

方式三:

view plaincopy to clipboardprint?
 
public class UserAction3 extends ActionSupport{  
     
    private static final long serialVersionUID = 1L;  
     
    private HttpServletRequest request;  
     
    private HttpSession session;  
     
    private ServletContext application;  
     
    @SuppressWarnings("unchecked")  
    @Override 
    public String execute(){  
        System.out.println("通过ServletActionContext来访问request,session,application对象");  
        // 初始化  
        request = ServletActionContext.getRequest();  
        session = request.getSession();  
        application = session.getServletContext();  
        // 赋值  
        request.setAttribute("requestKey", "requestValue");  
        session.setAttribute("sessionKey", "sessionValue");  
        application.setAttribute("applicationKey", "applicationValue");  
        return "success";  
    }  


public class UserAction3 extends ActionSupport{
 
 private static final long serialVersionUID = 1L;
 
 private HttpServletRequest request;
 
 private HttpSession session;
 
 private ServletContext application;
 
 @SuppressWarnings("unchecked")
 @Override
 public String execute(){
  System.out.println("通过ServletActionContext来访问request,session,application对象");
  // 初始化
  request = ServletActionContext.getRequest();
  session = request.getSession();
  application = session.getServletContext();
  // 赋值
  request.setAttribute("requestKey", "requestValue");
  session.setAttribute("sessionKey", "sessionValue");
  application.setAttribute("applicationKey", "applicationValue");
  return "success";
 }
}

方式四:

view plaincopy to clipboardprint?
 
public class UserAction4 extends ActionSupport implements ServletRequestAware{  
     
    private static final long serialVersionUID = 1L;  
     
    private HttpServletRequest request;  
     
    private HttpSession session;  
     
    private ServletContext application;  
     
    @SuppressWarnings("unchecked")  
    @Override 
    public String execute(){  
        System.out.println("通过实现ServletRequestAware接口来访问request,session,application对象");  
        // 赋值  
        request.setAttribute("requestKey", "requestValue");  
        session.setAttribute("sessionKey", "sessionValue");  
        application.setAttribute("applicationKey", "applicationValue");  
        return "success";  
    }  
     
    @Override 
    public void setServletRequest(HttpServletRequest request) {  
        this.request = request;  
        this.session = request.getSession();  
        this.application = session.getServletContext();  
    }  


public class UserAction4 extends ActionSupport implements ServletRequestAware{
 
 private static final long serialVersionUID = 1L;
 
 private HttpServletRequest request;
 
 private HttpSession session;
 
 private ServletContext application;
 
 @SuppressWarnings("unchecked")
 @Override
 public String execute(){
  System.out.println("通过实现ServletRequestAware接口来访问request,session,application对象");
  // 赋值
  request.setAttribute("requestKey", "requestValue");
  session.setAttribute("sessionKey", "sessionValue");
  application.setAttribute("applicationKey", "applicationValue");
  return "success";
 }
 
 @Override
 public void setServletRequest(HttpServletRequest request) {
  this.request = request;
  this.session = request.getSession();
  this.application = session.getServletContext();
 }
}

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

struts.xml

view plaincopy to clipboardprint?
<struts> 
    <!-- 配置开发模式:修改不用重启服务器 --> 
    <constant name="struts.devMode" value="true"/> 
    <package name="" namespace="/login" extends="struts-default"> 
        <action name="login*" class="com.wj.struts2.action.UserAction{1}"> 
            <result name="success">/success.jsp</result> 
            <result name="failure">/failure.jsp</result> 
        </action> 
    </package> 
</struts> 
<struts>
 <!-- 配置开发模式:修改不用重启服务器 -->
 <constant name="struts.devMode" value="true"/>
    <package name="" namespace="/login" extends="struts-default">
        <action name="login*" class="com.wj.struts2.action.UserAction{1}">
         <result name="success">/success.jsp</result>
         <result name="failure">/failure.jsp</result>
        </action>
    </package>
</struts>

index.jsp

view plaincopy to clipboardprint?
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> 
<%@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> 
        <base href="<%=basePath%>"> 
 
        <title>Struts2_AccessWebElements</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" mce_href="styles.css"> 
        --> 
        <mce:script language="javascript"><!--  
            function sub(str){  
                document.form1.action = str;  
                document.form1.submit();  
            }  
          
// --></mce:script> 
    </head> 
 
    <body> 
        <form name="form1"> 
            <div> 
                Struts2中访问web元素的四种方式<br> 
                方式一:<input type="button" value="submit1" οnclick="sub('<%=basePath%>login/login1')"><br>    
                方式二:<input type="button" value="submit2" οnclick="sub('<%=basePath%>login/login2')"><br> 
                方式三:<input type="button" value="submit3" οnclick="sub('<%=basePath%>login/login3')"><br> 
                方式四:<input type="button" value="submit4" οnclick="sub('<%=basePath%>login/login4')"><br> 
            </div> 
        </form> 
    </body> 
</html> 
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@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>
  <base href="<%=basePath%>">

  <title>Struts2_AccessWebElements</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" mce_href="styles.css">
  -->
  <mce:script language="javascript"><!--
   function sub(str){
    document.form1.action = str;
    document.form1.submit();
   }
  
// --></mce:script>
 </head>

 <body>
  <form name="form1">
   <div>
    Struts2中访问web元素的四种方式<br>
    方式一:<input type="button" value="submit1" οnclick="sub('<%=basePath%>login/login1')"><br> 
    方式二:<input type="button" value="submit2" οnclick="sub('<%=basePath%>login/login2')"><br>
    方式三:<input type="button" value="submit3" οnclick="sub('<%=basePath%>login/login3')"><br>
    方式四:<input type="button" value="submit4" οnclick="sub('<%=basePath%>login/login4')"><br>
   </div>
  </form>
 </body>
</html>
 

success.jsp

view plaincopy to clipboardprint?
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> 
<%@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> 
        <base href="<%=basePath%>"> 
        <title>Struts2_AccessWebElements</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" mce_href="styles.css"> 
        --> 
    </head> 
 
    <body> 
        requestKey---<s:property value="#request.requestKey"/>|<%=request.getAttribute("requestKey")%><br> 
        sessionKey---<s:property value="#session.sessionKey"/>|<%=session.getAttribute("sessionKey")%><br> 
        applicationKey---<s:property value="#application.applicationKey"/>|<%=application.getAttribute("applicationKey")%><br> 
        --------------------------------------------  
        <s:debug></s:debug> 
    </body> 
</html> 
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@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>
  <base href="<%=basePath%>">
  <title>Struts2_AccessWebElements</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" mce_href="styles.css">
  -->
 </head>

 <body>
  requestKey---<s:property value="#request.requestKey"/>|<%=request.getAttribute("requestKey")%><br>
  sessionKey---<s:property value="#session.sessionKey"/>|<%=session.getAttribute("sessionKey")%><br>
  applicationKey---<s:property value="#application.applicationKey"/>|<%=application.getAttribute("applicationKey")%><br>
  --------------------------------------------
  <s:debug></s:debug>
 </body>
</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")%>


本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/developer_java/archive/2010/09/14/5882976.aspx

转载于:https://www.cnblogs.com/jianmang/articles/4947867.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值