知识点:

servlet是单例的,Action是多例的,一次请求,创建一个Action的实例

结果页面分为全局和局部两类(局部优先级更高)

result标签:
name : 默认succes
type :页面跳转类型
  dispatcher 默认值,请求转发(action转发jsp)
  redirect 重定向(action重定向jsp)
  chain 转发(action转发action)
  redirectAction 重定向(action重定向action)
  stream struts2中提供文件下载的功能

代码如下

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="demo1" extends="struts-default">
<!--全局结果页面-->
<global-results>
<result name="success">servlet1/demo2.jsp</result>
</global-results>
<action name="requestDemo1" class="com.jinke.servlet1.RequestDemo1"/>
<action name="requestDemo2" class="com.jinke.servlet1.RequestDemo2"/>
<action name="requestDemo3" class="com.jinke.servlet1.RequestDemo3"/> <!--局部结果页面-->
<action name="requestDemo1" class="com.jinke.servlet1.RequestDemo1">
<result name="success">servlet1/demo2.jsp</result>
</action>
<action name="requestDemo2" class="com.jinke.servlet1.RequestDemo2">
<result name="success">servlet1/demo2.jsp</result>
</action>
<action name="requestDemo3" class="com.jinke.servlet1.RequestDemo3">
<result name="success">servlet1/demo2.jsp</result>
</action>
</package>
</struts>

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
version="4.0">
<welcome-file-list>
<welcome-file>servlet1/demo1.jsp</welcome-file>
</welcome-file-list>
<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>

demo1.jsp

<%--
Created by IntelliJ IDEA.
User: wanglei
Date: 2019/6/18
Time: 11:03
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<h1>Struts2访问Servlet的API</h1>
<h3>方式一:完成解耦合的方式</h3>
<form action="${pageContext.request.contextPath}/requestDemo1.action" method="post">
姓名:<input type="text" name="name"><br/>
密码:<input type="password" name="password"><br/>
<input type="submit" value="提交"></form> <h3>方式二:完成原生的方式访问</h3>
<form action="${pageContext.request.contextPath}/requestDemo2.action" method="post">
姓名:<input type="text" name="name"><br/>
密码:<input type="password" name="password"><br/>
<input type="submit" value="提交"></form> <h3>方式三:接口注入的方式访问</h3>
<form action="${pageContext.request.contextPath}/requestDemo3.action" method="post">
姓名:<input type="text" name="name"><br/>
密码:<input type="password" name="password"><br/>
<input type="submit" value="提交"></form> </body>
</html>

demo2.jsp

<%--
Created by IntelliJ IDEA.
User: wanglei
Date: 2019/6/18
Time: 11:24
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<h1>显示数据</h1>
${reqName}<br/>
${sessName}<br/>
${appName}<br/>
</body>
</html>

action层

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import org.apache.struts2.dispatcher.HttpParameters; /**
* 访问Servlet的API方式一:完全解耦合的方式
*/
public class RequestDemo1 extends ActionSupport {
@Override
public String execute() throws Exception {
//接收参数
//利用struts2中的对象ActionContext对象
ActionContext context = ActionContext.getContext();
//调用ActionContext中方法
HttpParameters map = context.getParameters();
// System.out.println(map); //二.向域对象中存入数据
context.put("reqName", map);//相当于request.setAttribute()
context.getSession().put("sessName", map);//相当于session.setAttribute()
context.getApplication().put("appName", map);//相当于application.setAttribute()
return SUCCESS;
}
}
import com.opensymphony.xwork2.ActionSupport;
import org.apache.struts2.ServletActionContext; import javax.servlet.http.HttpServletRequest;
import java.util.Map; /**
* 访问Servlet的API方式二:采用原生的方式
*/
public class RequestDemo2 extends ActionSupport {
@Override
public String execute() throws Exception {
//一、接收数据
//直接获得request对象,通过ServletActionSupport
HttpServletRequest request = ServletActionContext.getRequest();
Map<String, String[]> map = request.getParameterMap();
System.out.println(map.toString()); //二、向域对象中保存数据
//向request中保存数据
request.setAttribute("reqName", map.toString());
//向session中保存数据
request.getSession().setAttribute("sessName", map.toString());
//向application中保存数据
ServletActionContext.getServletContext().setAttribute("appName", map.toString());
return SUCCESS;
}
}
import com.opensymphony.xwork2.ActionSupport;
import org.apache.struts2.interceptor.ServletRequestAware;
import org.apache.struts2.util.ServletContextAware; import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import java.util.Arrays;
import java.util.Map; /**
* 访问Servlet的API方式三:接口注入的方式
*/
public class RequestDemo3 extends ActionSupport implements ServletRequestAware, ServletContextAware {
private HttpServletRequest request;
private ServletContext context; @Override
public String execute() throws Exception {
//一、接收参数
//通过接口注入的方式获得request对象
Map<String, String[]> map = request.getParameterMap();
for (String key : map.keySet()) {
String[] value = map.get(key);
System.out.println(key + " " + Arrays.toString(value));
} //二、向域对象中保存数据
//向request域中保存数据
request.setAttribute("reqName", map.toString());
//向session中保存数据
request.getSession().setAttribute("sessName", map.toString());
//向Application中保存数据
context.setAttribute("appName", map.toString());
return super.execute();
} @Override
public void setServletRequest(HttpServletRequest httpServletRequest) {
this.request = httpServletRequest;
} @Override
public void setServletContext(ServletContext servletContext) {
this.context = servletContext;
}
}

结果

欢迎关注我的微信公众号:安卓圈

Struts2访问Servlet的更多相关文章

  1. 配置Struts2及Struts2访问servlet api的方式

    Struts2的起源与背景 在很长的一段时间内,在所有的MVC框架中,Struts1处于绝对的统治地位,无论是从市场的普及范围,还是具体的使用者数量. 其他MVC框架都无 法与其相比,作为一一款优秀的 ...

  2. (转)Struts2访问Servlet的API及......

    http://blog.csdn.net/yerenyuan_pku/article/details/67315598 Struts2访问Servlet的API 前面已经对Struts2的流程已经执行 ...

  3. 八 Struts2访问Servlet的API方式三:接口注入

    Struts2访问Servlet的API方式三:接口注入 通过实现ServletRequestAware, ServletContextAware 接口,拿到Request.ServletContex ...

  4. 七 Struts2访问Servlet的API方式二:原生方式

    Struts2访问Servlet的API方式二:原生方式 和解耦合的方式不同,原生方式既可以拿到域对象,也可以调用域对象中的方法 前端jsp: <%@ page language="j ...

  5. 六 Struts2访问Servlet的API方式一:完全解耦合的方式

    注意: 完全解耦合的方式,这种方式只能获得代表request.session.application的数据的Map集合. 不能操作这些对象的本身的方法. 1 jsp: <%@ page lang ...

  6. struts2访问servlet API

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

  7. Struts2(七) Struts2访问Servlet的API

    当接受表单参数,向页面保持数据时.要用到Struts访问Servlet 的API .下面只做参考,有错误或不同意见可以发送邮箱2440867831@qq.com  .建议大家看struts文档,源代码 ...

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

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

  9. Struts2访问Servlet API的几种方式

    struts2提供了三种方式访问servlet API:大致分为两类 1. ActionContext:  public static ActionContext getContext() :获得当前 ...

随机推荐

  1. 调试用Chrome浏览器

    今天写HTML页面调试时出了问题:一个页面在本地的工作空间文件夹内可以打得开,在HBuilder里用Edge打不开. 我还以为是工作空间路径出问题了,重建了好几次空间和项目.. 询问了小页,他建议以后 ...

  2. Django如何与ajax通信

    示例一 文件结构 假设你已经创建好了一个Django项目和一个App,部分结构如下: mysite myapp |___views.py |___models.py |___forms.py |___ ...

  3. python正则表达式(3)--match方法

    1.re.match函数 re.match 尝试从字符串的起始位置匹配一个模式,如果不是起始位置匹配成功的话,match()就返回None. (1)函数语法: re.match(pattern, st ...

  4. MoveIt简单编程

    目的:使用一些简单代码使机器人运动到指定位置.讲解代码怎么实现机器人的运动. 参考文献: 第一个博客需要下载<Mastering ROS for robotics Programming> ...

  5. LGOJP3193 [HNOI2008]GT考试

    \(f[i][j]\)表示当前摆放到第\(i\)位,然后当前的匹配长度为\(j\) \(f[i][j]=\sum {f[i][k]*g[k][j]}\) \(g[i][j]\)表示将长度为\(i\)的 ...

  6. 异常sql处理

    下面是在awr报告里面看到的有问题的sql,是9个变量的,在应用前台属于关联查询,在sqlplus里面手工执行检查实际执行情况如下: SELECT /*+ GATHER_PLAN_STATISTICS ...

  7. wordpress调用指定id的page页面的方法,适用于多id调用

    前面我们讲到wordpress如何调用指定page页面内容,现在再用另外的方法来调试一下,可以直接在single.php模板使用,同样可以调用多id,随ytkah一起来看看 <?php $arg ...

  8. 分享一个简单易用的软件定时器模块(MultiTimer)——基于keil+stm32f103zet+hal库(裸机实现)

    公众号上看到一个比较好的一个github项目:https://github.com/0x1abin/MultiTimer 今天看了看,简单的,就移植了- 且看文档的说明, ============== ...

  9. OpenGL是什么?GPU是什么?

    一.GPU与CPU CPU是处理基本算数运算的单元:它处理的数据是数:整型.浮点型.bool等等: GPU是处理图形运算的单元:它处理的数据是图形的数据矩阵:   GPU的输入是一个和多个图形,输出是 ...

  10. JS的ES6的iterator

    一.iterator 1.概念:iterator是一种接口机制,为各种不同的数据结构提供统一的访问机制. 2.作用: 为各种数据结构,提供一个统一的.简便的访问接口: 使得数据结构的成员能够按某种次序 ...