初识Struts2_访问web元素

一、struts.xml

 1  <!-- 
 2          访问url:
 3              http://localhost:8080/Struts2_WebElement/web1
 4              http://localhost:8080/Struts2_WebElement/web2
 5              http://localhost:8080/Struts2_WebElement/web3
 6              http://localhost:8080/Struts2_WebElement/web4
 7       -->
 8      <package name="default" namespace="/" extends="struts-default">
 9         <action name="web*" class="cn.su.action.WebAction{1}">
10             <result>/index.jsp</result>
11         </action>
12     </package>

二、获取Map类型的reuqest、session、application

package cn.su.action;

import java.util.Map;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

public class WebAction1 extends ActionSupport {
    public String execute(){
        /*
         * 获取Map类型的Web对象,request,session,application
         * */
        Map<String,Object> request = (Map<String, Object>) ActionContext.getContext() .get("request");
        Map<String,Object> session = ActionContext.getContext().getSession();
        Map<String,Object> application = ActionContext.getContext().getApplication();
        /*放入数据*/
        request.put("r1", "r1");
        session.put("s1", "s1");
        application.put("a1", "a1");
        return SUCCESS;
    }
}

 

 1 package cn.su.action;
 2 
 3 import java.util.Map;
 4 
 5 import org.apache.struts2.interceptor.ApplicationAware;
 6 import org.apache.struts2.interceptor.RequestAware;
 7 import org.apache.struts2.interceptor.SessionAware;
 8 
 9 import com.opensymphony.xwork2.ActionContext;
10 import com.opensymphony.xwork2.ActionSupport;
11 
12 public class WebAction2 extends ActionSupport implements RequestAware,SessionAware,ApplicationAware {
13     private Map<String,Object> request;
14     private Map<String,Object> session;
15     private Map<String,Object> application;
16     public String execute(){
17         request.put("r1", "r1");
18         session.put("s1", "s1");
19         application.put("a1", "a1");
20         return SUCCESS;
21     }
22     /*
23      * 获取Map类型的request,session,application
24      * 先要实现接口RequestAware,SessionAware,ApplicationAware
25      * 这些接口实现的方法传递过来的参数就是request,session,application对象了
26      * */
27     public void setApplication(Map<String, Object> application) {
28         this.application = application;
29     }
30 
31     public void setSession(Map<String, Object> session) {
32         this.session = session;
33     }
34 
35     public void setRequest(Map<String, Object> request) {
36         this.request = request;
37     }
38 }

三、获取原生的HttpServletRequest、HttpSession、ServletContext

 1 package cn.su.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 import com.opensymphony.xwork2.ActionSupport;
10 
11 public class WebAction3 extends ActionSupport {
12     /*
13      * 获取原生的HttpServletRequest,HttpSession,ServletContext对象
14      * */
15     public String execute(){
16         /*
17          * 通过ServletActionContext对象获取
18          * */
19         HttpServletRequest request = ServletActionContext.getRequest();
20         HttpSession session = request.getSession();
21         ServletContext context = session.getServletContext();
22         request.setAttribute("r1", "r1");
23         session.setAttribute("s1", "s1");
24         context.setAttribute("a1", "a1");
25         return SUCCESS;
26     }
27 }
 1 package cn.su.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 
 9 import com.opensymphony.xwork2.ActionSupport;
10 
11 public class WebAction4 extends ActionSupport implements ServletRequestAware {
12     /*
13      * 获取原生的HttpServletRequest,HttpSession,ServletContext对象
14      * */
15     private HttpServletRequest request;
16     public String execute(){
17         /*
18          * 通过实现接口ServletRequestAware获取
19          * */
20         HttpSession session = request.getSession();
21         ServletContext context = session.getServletContext();
22         request.setAttribute("r1", "r1");
23         session.setAttribute("s1", "s1");
24         context.setAttribute("a1", "a1");
25         return SUCCESS;
26     }
27 
28     public void setServletRequest(HttpServletRequest request) {
29         this.request = request;
30     }
31 }

四、获取放置在request、session、applicat中的数据

 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()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
 6 %>
 7 
 8 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
 9 <html>
10   <head>
11     <base href="<%=basePath%>">
12     
13     <title>My JSP ‘index.jsp‘ starting page</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">
21     -->
22   </head>
23   
24   <body>
25   <!-- 获取request,session,application中的数据 -->
26       <!-- 第一种方式 -->
27       <%= request.getAttribute("r1") %><br/>
28       <%= session.getAttribute("s1") %><br/>
29       <%= application.getAttribute("a1") %><br/>
30       <hr/>
31       <!-- 第二种方式 -->
32       <s:property value="#request.r1"/><br/>
33       <s:property value="#session.s1"/><br/>
34       <s:property value="#application.a1"/><br/>
35       <hr/>
36       <!-- 第三种方式 -->
37       <s:property value="#attr.r1"/><br/>
38       <s:property value="#attr.s1"/><br/>
39       <s:property value="#attr.a1"/><br/>
40     <s:debug></s:debug>
41   </body>
42 </html>

 

初识Struts2_访问web元素,古老的榕树,5-wow.com

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。