首页 > 代码库 > struts2学习笔记(3)---Action中訪问ServletAPI获取真实类型的Servlet元素

struts2学习笔记(3)---Action中訪问ServletAPI获取真实类型的Servlet元素

一、源码:

struts.xml文件:

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

<struts>
	<constant name="struts.action.extension" value="http://www.mamicode.com/action,do," />
	<constant name="struts.devMode" value="http://www.mamicode.com/true" />
	<constant name="struts.enable.DynamicMethodInvocation" value="http://www.mamicode.com/true" />


	<package name="default" namespace="/" extends="struts-default">
		<default-action-ref name="default" />

		<action name="test1" class="org.Test.action.TestAction1"
			method="test">
			<result>/success.jsp</result>
		</action>
		
		<action name="test2" class="org.Test.action.TestAction2"
			method="test">
			<result>/success.jsp</result>
		</action>
		
		<action name="test3" class="org.Test.action.TestAction3"
			method="test">
			<result>/success.jsp</result>
		</action>
		
		<action name="test4" class="org.Test.action.TestAction4"
			method="test">
			<result>/success.jsp</result>
		</action>

		<action name="default">
			<result>/index.jsp</result>
		</action>
	</package>
</struts>

success.jsp页面:

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>

<title>成功</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">

</head>

<body>
	${requestScope.name}
	${sessionScope.name}
	${applicationScope.name}
	<br>
</body>
</html>


方法一:通过ServletActionContext来訪问

TestAction3.java文件:

package org.Test.action;

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

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 org.apache.struts2.interceptor.ApplicationAware;
import org.apache.struts2.interceptor.RequestAware;
import org.apache.struts2.interceptor.SessionAware;

import com.opensymphony.xwork2.ActionSupport;

public class TestAction3 extends ActionSupport {
	private HttpServletRequest request;
	private HttpServletResponse response;
	private HttpSession session;
	private ServletContext application;
	
	public TestAction3(){   //构造函数内获取四个对象
		this.request = ServletActionContext.getRequest();
		this.response = ServletActionContext.getResponse();
		this.session = this.request.getSession();
		this.application = ServletActionContext.getServletContext();
	}
	

	public String test() throws IOException {
		System.out.println("TestAction3:test");
		this.request.setAttribute("name", "Jack");
		this.session.setAttribute("name", "Rose");
		this.application.setAttribute("name", "Dean");
		
		return SUCCESS;
	}

}

执行结果:

技术分享技术分享


说明:

1.TestAction3的
类中:定义真实类型的request,response,session,application对象
构造方法中:通过ServletActionContext获取request,response和application。session可由request.getSession();得到
test()方法中:用setAttribute方法设置属性
2.success.jsp:通过EL语言查看request,session,application对象的name属性的值



方法二:IOC(控制反转),通过实现接口ServletRequestAware,ServletResponseAware, ServletContextAware来訪问
TestAction4.java文件:

package org.Test.action;

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

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 org.apache.struts2.interceptor.ApplicationAware;
import org.apache.struts2.interceptor.RequestAware;
import org.apache.struts2.interceptor.ServletRequestAware;
import org.apache.struts2.interceptor.ServletResponseAware;
import org.apache.struts2.interceptor.SessionAware;
import org.apache.struts2.util.ServletContextAware;

import com.opensymphony.xwork2.ActionSupport;

public class TestAction4 extends ActionSupport implements ServletRequestAware,
		ServletResponseAware, ServletContextAware {
	private HttpServletRequest request;
	private HttpServletResponse response;
	private HttpSession session;
	private ServletContext application;

	@Override
	public void setServletResponse(HttpServletResponse response) {   //实现接口的set方法。初始化response
		this.response = response;
	}

	@Override
	public void setServletRequest(HttpServletRequest request) {
		this.request = request;
	}
	
	@Override
	public void setServletContext(ServletContext application) {
		this.application = application;
	}


	public String test() throws IOException {
		this.session = this.request.getSession();
		
		System.out.println("TestAction4:test");
		this.request.setAttribute("name", "学渣");
		this.session.setAttribute("name", "学霸");
		this.application.setAttribute("name", "学神");

		return SUCCESS;
	}

}

执行结果:

技术分享技术分享


说明:

1.通过实现接口的set方法来实例化request,response,application对象。而TestAction4本身并没有对三个对象进行实例化。session由request.getSession();进行实例化。

struts2学习笔记(3)---Action中訪问ServletAPI获取真实类型的Servlet元素