struts2访问Web元素

struts2访问Web元素
struts2访问Web元素:

    1.使用ActionContext
    2.使用ServletActionContext
    3.实现RequestAware,SessionAware,ApplicationAware(最常用)

具体代码如下:
VisitWebElement.java 在该类中使用第一和第二种方法
package com.sinwee.visitWebElement;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Arrays;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.struts2.ServletActionContext;

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

public class VisitWebElement extends ActionSupport {

	/**
	 * 
	 */
	private static final long serialVersionUID = -2022249357432920534L;
	
	
	
	public String byActionContext() {
		
		ActionContext actionContext = ActionContext.getContext();
		actionContext.put("request", "request应用范围");
		actionContext.put("names", Arrays.asList("wangxinwe,sinwee,zhangxueyou,liudehua"));
		actionContext.getSession().put("session", "session应用范围");
		actionContext.getApplication().put("application", "application应用范围");
		return "actionContext";
	}
	
	public String byServletActionContext() throws IOException {
		
		HttpServletRequest request = ServletActionContext.getRequest();
		request.setAttribute("request", "request域");
		HttpSession session = request.getSession();
		session.setAttribute("session", "session域");
		ServletContext application = ServletActionContext.getServletContext();
		application.setAttribute("application", "application域");
		//HttpServletResponse response = ServletActionContext.getResponse();
		//PrintWriter writer = response.getWriter();
		//writer.write("通过ServletActionContext得到的HttpServletResponse");
		
		return "servletActionContext";
	}

}



ImplementAware.java 在该类中使用第三种方法

package com.sinwee.visitWebElement;

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;

public class ImplementAware extends ActionSupport implements RequestAware,SessionAware,ApplicationAware{

	/**
	 * 
	 */
	private static final long serialVersionUID = 2534117735365564103L;
	
	private Map<String, Object> request;
	private Map<String, Object> session;
	private Map<String, Object> application;

	public String byImplAware() {
		
		this.request.put("request", "request域_aware");
		this.session.put("session", "session域_aware");
		this.application.put("application", "application域_aware");
		return "byImplAware";
	}
	
	@Override
	public void setRequest(Map<String, Object> request) {
		this.request = request;
	}

	@Override
	public void setSession(Map<String, Object> session) {
		this.session = session;
	}

	@Override
	public void setApplication(Map<String, Object> application) {
		this.application = application;
	}

}



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

<struts>
	<constant name="struts.devMode" value="true" />
    <package name="vistWebElement" namespace="" extends="struts-default">
        <action name="byActionContext_*" class="com.sinwee.visitWebElement.VisitWebElement" method="{1}">
        	<result name="actionContext">
        		/showValueByActionContext.jsp
        	</result>
        </action>
        
        <action name="byServletActionContext_*" class="com.sinwee.visitWebElement.VisitWebElement" method="{1}">
        	<result name="servletActionContext">
        		/showValueByServletActionContext.jsp
        	</result>
        </action>
        
        <action name="byImplAware_*" class="com.sinwee.visitWebElement.ImplementAware" method="{1}">
        	<result name="byImplAware">
        		/showValueByImplAware.jsp
        	</result>
        </action>
    </package>
</struts>



web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" 
	xmlns="http://java.sun.com/xml/ns/javaee" 
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
	xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
	http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
	 <filter>
		<filter-name>struts2</filter-name>
		<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
	 </filter>
	 <filter-mapping>
		<filter-name>struts2</filter-name>
		<url-pattern>/*</url-pattern>
	 </filter-mapping>
</web-app>



visitWebElement.jsp 访问action的jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <title>测试struts2访问Web元素</title>
	<!--
	<link rel="stylesheet" type="text/css" href="styles.css">
	-->

  </head>
  
  <body>
    <form name="f" method="post">
    	<input type="button" value="通过ActionContext访问Web元素" 
    		onclick="javascript:document.f.action='${pageContext.request.contextPath}/byActionContext_byActionContext';document.f.submit();"/>
    	<input type="button" value="通过ServletActionContext访问Web元素"
    		onclick="javascript:document.f.action='${pageContext.request.contextPath}/byServletActionContext_byServletActionContext';document.f.submit();"/>
    		
    	<input type="button" value="通过实现RequestAware,SessoinAware,ApplicationAware访问Web元素"
    		onclick="javascript:document.f.action='${pageContext.request.contextPath}/byImplAware_byImplAware';document.f.submit();"/>	
    </form>
  </body>
</html>



showValueByActionContext.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <title>My JSP 'showValueByActionContext.jsp' starting page</title>


  </head>
  
  <body>
   	显示通过ActionContext对象访问Web元素设置的值<br/>
   	request域中的元素:${requestScope.request}<br/>
   	request域中的list元素:
   	<c:forEach var="name" items="${requestScope.names}">
   		${name }
   	</c:forEach>
   	<br/>
   	session域中的元素:${sessionScope.session }<br/>
   	application域中的元素:${applicationScope.application }
  </body>
</html>



showValueByServletActionContext.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <title>My JSP 'showValueByServletActionContext.jsp' starting page</title>
    
  </head>
  
  <body>
           显示通过ServletActionContext对象访问Web元素设置的值<br/>
   	request域中的元素:${requestScope.request}<br/>
   	session域中的元素:${sessionScope.session }<br/>
   	application域中的元素:${applicationScope.application }
   	
  </body>
</html>



showValueByImplAware.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <title>My JSP 'showValueByImplAware.jsp' starting page</title>
    
  </head>
  
  <body>
	显示通过实现AwareRequest,AwareSession,AwareApplication访问Web元素设置的值<br/>
	request域中的元素:${requestScope.request}<br/>
   	session域中的元素:${sessionScope.session }<br/>
   	application域中的元素:${applicationScope.application }
  </body>
</html>

你可能感兴趣的:(struts2)