© 版权声明:本文为博主原创文章,转载请注明出处
Struts2提供了三种方式去访问Servlet API
-ActionContext
-实现*Aware接口
-ServletActionContext
实例:
1.项目结构

2.pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.struts</groupId>
<artifactId>Struts2-ServletAPI</artifactId>
<packaging>war</packaging>
<version>0.0.1-SNAPSHOT</version>
<name>Struts2-ServletAPI Maven Webapp</name>
<url>http://maven.apache.org</url>
<properties>
<!-- 统一Struts2开发环境 -->
<struts2.version>2.5.10</struts2.version>
</properties>
<dependencies>
<!-- junit -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
<!-- Struts2 -->
<dependency>
<groupId>org.apache.struts</groupId>
<artifactId>struts2-core</artifactId>
<version>${struts2.version}</version>
</dependency>
<!-- Servlet -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
</dependency>
</dependencies>
<build>
<finalName>Struts2-ServletAPI</finalName>
</build>
</project>
3.web.xml
<?xml version="1.0" encoding="UTF-8"?> <web-app 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_3_0.xsd" version="3.0" metadata-complete="true"> <!-- 配置Struts2过滤器 --> <filter> <filter-name>struts</filter-name> <filter-class>org.apache.struts2.dispatcher.filter.StrutsPrepareAndExecuteFilter</filter-class> </filter> <filter-mapping> <filter-name>struts</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> </web-app>
4.ServletAPIAction.java
package org.struts.action;
import javax.servlet.http.HttpServletRequest;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.interceptor.ServletRequestAware;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
public class ServletAPIAction extends ActionSupport implements ServletRequestAware {
private static final long serialVersionUID = 1L;
private HttpServletRequest req1;
private HttpServletRequest req2;
private HttpServletRequest req3;
/**
* struts2默认执行方法
*/
@Override
public String execute() throws Exception {
//第一种方式:ActionContext
req1 = (HttpServletRequest) ActionContext.getContext()
.get(org.apache.struts2.StrutsStatics.HTTP_REQUEST);
//第三种方式:ServletActionContext
req3 = ServletActionContext.getRequest();
return SUCCESS;
}
//第二种方式:实现*Aware接口
public void setServletRequest(HttpServletRequest request) {
req2 = request;
}
public HttpServletRequest getReq1() {
return req1;
}
public HttpServletRequest getReq2() {
return req2;
}
public HttpServletRequest getReq3() {
return req3;
}
}
5.struts.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.5//EN"
"http://struts.apache.org/dtds/struts-2.5.dtd">
<struts>
<package name="servletAPI" extends="struts-default" namespace="/">
<action name="servletAPI" class="org.struts.action.ServletAPIAction">
<result>/success.jsp</result>
</action>
</package>
</struts>
6.success.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
第一种方式获取request:${req1 }<br/>
第二种方式获取request:${req2 }<br/>
第三种方式获取request:${req3 }
</body>
</html>
7.效果预览
