© 版权声明:本文为博主原创文章,转载请注明出处

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.效果预览

参考:http://www.imooc.com/video/8997

Struts2学习二----------访问Servlet API的更多相关文章

  1. struts2的action访问servlet API的三种方法

    学IT技术,就是要学习... 今天无聊看看struts2,发现struts2的action访问servlet API的三种方法: 1.Struts2提供的ActionContext类 Object g ...

  2. Struts2笔记--Action访问Servlet API

    Web应用中通常需要访问的Servlet API就是HttpServletRequest.HttpSession和ServletContext,这三个接口分别代表JSP内置对象中的request.se ...

  3. struts2 笔记01 登录、常用配置参数、Action访问Servlet API 和设置Action中对象的值、命名空间和乱码处理、Action中包含多个方法如何调用

    Struts2登录 1. 需要注意:Struts2需要运行在JRE1.5及以上版本 2. 在web.xml配置文件中,配置StrutsPrepareAndExecuteFilter或FilterDis ...

  4. 笔记01 登录、常用配置参数、Action访问Servlet API 和设置Action中对象的值、命名空间和乱码处理、Action中包含多个方法如何调用

    Struts2登录 1. 需要注意:Struts2需要运行在JRE1.5及以上版本 2. 在web.xml配置文件中,配置StrutsPrepareAndExecuteFilter或FilterDis ...

  5. Struts2学习笔记(五)——Action访问Servlet API

    在Strut2中访问Servlet API有三种方式: 1.通过ActionContext访问Servlet API,推荐使用这种,但是这种方案它获取的不是真正的事Servlet API. 步骤: 1 ...

  6. 关于Struts2自动装配和访问Servlet API

    自动装配 1.根据属性的getter和setter获取值  index.jsp <s:form action="hello" method="POST"& ...

  7. Struts2(八)访问Servlet API

    一.Struts2中的Servlet API 1.1.struts2的Action实现了MVC中C层的作用 针对请求用户显示不同的信息 登录后段保存用户信息 ----session 保存当前在线人数等 ...

  8. Struts2访问Servlet API的三种方式

    有时我们需要用到Request, Response, Session,Page, ServletContext这些我们以前常用的对象,那么在Struts2中怎么样使用到这些对象呢,通常有三种方式. * ...

  9. struts2访问servlet API

    搭建环境: 引入jar包,src下建立struts.xml文件 项目配置文件web.xml. web.xml: <?xml version="1.0" encoding=&q ...

随机推荐

  1. Hibernate逆向工程生成代码

    编辑此文章,其目的是方便以后根据表生成相应的代码,然而并非所有的代码都是如此.这里的Hibernate 即响应题目的ssh框架中的“h”. 如图所示,点击右上角,在myeclipse之中.再点击Mye ...

  2. Python之文件操作:经验总结

    1.怎么判断读出来的文件是gbk还是utf-8编码 if content == u'中国'.encode('gbk'):     return 'gbk' elif content == u'中国'. ...

  3. Python之数据结构:字典

    key值需要是不可变对象,字典没有顺序 1.声明一个字典 dictA={ } 2.字典添加元素 dictA['name']='jack' dictA['age']=19 dictA['sex']='m ...

  4. 一个javascript继承和使用的例子

    继承可以帮助我们实现代码的重用,把对象的属性写入构造函数,对象的方法写入原型后,以下例子演示继承的使用: 示例的css和js在后 父实例,得到一个间隔1s的轮播: <!DOCTYPE html& ...

  5. 80人环游世界(bzoj 2055)

    Description     想必大家都看过成龙大哥的<80天环游世界>,里面的紧张刺激的打斗场面一定给你留下了深刻的印象.现在就有这么     一个80人的团伙,也想来一次环游世界. ...

  6. 【转】Resharper上手指南

    原文发布时间为:2011-02-16 -- 来源于本人的百度文章 [由搬家工具导入] 我是visual studio的忠实用户,从visual studio 6一直用到了visual studio 2 ...

  7. Codeforces Gym101063 J.The Keys (2016 USP-ICMC)

    J.The Keys Out of all science labs constructed by the GEMA mission on Mars, the DSL - Dangerous Spec ...

  8. 配置之MySQL5Dialect

    报错: org.hibernate.service.spi.ServiceException: Unable to create requested service [org.hibernate.en ...

  9. Codeforces 429D Tricky Function(平面最近点对)

    题目链接  Tricky Function $f(i, j) = (i - j)^{2} + (s[i] - s[j])^{2}$ 把$(i, s[i])$塞到平面直角坐标系里,于是转化成了平面最近点 ...

  10. 实现一个Java五子棋

    五子棋手把手教你写: 写在前面的话: 回想起从前初学代码的五子棋简直写的不像样子.今天闲来无事就写了个五子棋的小程序. 一来呢回忆一下很久以前写代码时的感觉. 二来呢顺便帮下诸位有需求的学生,顺利的C ...