struts2-第一章-基础用法3
一,结果类型配置
在之前servlet学习中,知道网页页面路径跳转有两种方式,内部跳转(请求转发)和外部跳转(重定向),两者的区别,内部跳转浏览器地址不会变化
可以保存上一次请求的数据
外部跳转浏览器地址会发生变化,但是不能保存上一次请求数据
struts2中也有上述两种跳转方式,但是需要我们进行指定,在struts.xml中action节点的result节点的type属性进行配置
type属性的取值:
(1)dispatcher :内部跳转,默认值,通常跳转jsp
(2)redirect:外部跳转,通常跳转jsp
(3)chain:内部跳转,通常跳转到action
(4)redirectAction:外部跳转,通常跳转到action
案例
(1)action1类
@Getter
@Setter
public class Demo1Action extends ActionSupport { private String name = "zhangsan"; //内部跳转jsp
public String dispatcher(){
System.out.println("Demo1Action.dispatcher...");
return "a";
}
//外部跳转jsp
public String redirect(){
System.out.println("Demo1Action.redirect...");
return "b";
}
//内部跳转action
public String chain(){
System.out.println("Demo1Action.chain...");
return "c";
}
//外部跳转action
public String redirectAction(){
System.out.println("Demo1Action.redirectAction...");
return "d";
} }
(2)struts.xml配置
<package name="default" namespace="/" extends="struts-default">
<action name="demo1Action" class="com.yujun.maven.action.Demo1Action">
<!-- 跳转到jsp,内部和外部 -->
<result name="a" type="dispatcher">/demo1.jsp</result>
<result name="b" type="redirect">/demo1.jsp</result> <!-- 跳转到action,内部和外部 -->
<result name="c" type="chain">
<!-- 指定跳转的action的名称 -->
<param name="actionName">demo2Action</param>
<!-- 指定action所在命名空间 -->
<param name="namespace">/</param>
<!-- 指定执行action的哪个方法 -->
<param name="method">m1</param>
</result>
<result name="d" type="redirectAction">
<!-- 指定跳转的action的名称 -->
<param name="actionName">demo2Action!m2.action</param>
<!-- 指定action所在命名空间 -->
<param name="namespace">/</param>
</result> </action> <action name="demo2Action" class="com.yujun.maven.action.Demo2Action">
<result>/demo2.jsp</result>
</action> </package>
(3)action2类
public class Demo2Action extends ActionSupport {
public String m1(){
System.out.println("Demo2Action.m1...");
return SUCCESS;
}
public String m2(){
System.out.println("Demo2Action.m2...");
return SUCCESS;
}
}
(4)demo1.jsp
<h3>
姓名:${name }
</h3>
(5)demo2.jsp
<h3>
this is demo2.jsp
</h3>
(6)index.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>
<base href="${pageContext.request.contextPath}/ "/>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<h4>
<a href="demo3Action!dispatcher.action">访问demo1Action的dispatcher方法</a>
</h4>
<h4>
<a href="demo3Action!redirect.action">访问demo1Action的redirect方法</a>
</h4>
<h4>
<a href="demo3Action!chain.action">访问demo1Action的chain方法</a>
</h4>
<h4>
<a href="demo3Action!redirectAction.action">访问demo1Action的redirectAction方法</a>
</h4> </body>
</html>
二,action中访问request,session,application
在struts2中的action有时需要访问servlet资源,比如会话,记录登录人信息,通常有三种
(1)使用ActionContext
(2)使用ServletActionContext
(3)action实现ServletRequestAware/ServletContextAware接口
1.ActionContext
表示action上下文,上下文看做一个容器(本质就是HashMap),存放的是action在执行时需要用到的对象,是一个线程安全类
(1)action类
public class Demo3Action extends ActionSupport {
public String m1(){
//都可以给当前请求中存放数据
//等效于request对象,但并不是request
ActionContext.getContext().put("name", "小明");
//真正的request对象
((Map)ActionContext.getContext().get("request")).put("name2", "小红");
//session对象
ActionContext.getContext().getSession().put("username", "admin");
//application对象
ActionContext.getContext().getApplication().put("count", 10);
return SUCCESS;
}
}
(2)struts.xml配置
<action name="demo3Action" class="com.yujun.maven.action.Demo3Action">
<result>/demo3.jsp</result>
</action>
(3)页面
<h3>
${name },${name2 },${username },${count }
</h3>
若想查看值栈的信息,可以使用struts2提供的debug标签

(4)访问
http://127.0.0.1:8080/struts2-chapter1-3/demo3Action!m1.action
2.servletActionContext
public class ServletActionContext extend ActionContext implements StrutsStatics{
ServletActionContext类继承了ActionContext,因此此类也是一个线程安全类;该类提供了直接访问servlet容器对象(request、session、application)的功能
(1)action类
public class Demo4Action extends ActionSupport {
public String m1(){
//request对象
ServletActionContext.getRequest().setAttribute("abc", "abc123");
//session对象
ServletActionContext.getRequest().getSession().setAttribute("xyz", "xyz123");
//application对象
ServletActionContext.getServletContext().setAttribute("opq", "opq123");
return SUCCESS;
}
}
(2)struts.xml配置
<action name="demo4Action" class="com.yujun.maven.action.Demo4Action">
<result>/demo4.jsp</result>
</action>
(3)页面

(4) 访问
http://127.0.0.1:8080/struts2-chapter1-3/demo4Action!m1.action
3.实现接口的方式
(1)action类
public class Demo5Action extends ActionSupport implements ServletRequestAware,ServletContextAware{
//成员变量保存servlet容器对象
private HttpServletRequest request;
private ServletContext application;
private HttpSession session;
//参数的request是struts2注入给我们的,我们可以直接保存起来
@Override
public void setServletRequest(HttpServletRequest request) {
this.request = request;
this.session = request.getSession();
}
@Override
public void setServletContext(ServletContext context) {
this.application = context;
}
public String m1(){
request.setAttribute("a", "a123");
session.setAttribute("b", "b123");
application.setAttribute("c", "c123");
return SUCCESS;
}
}
(2)struts.xml配置
<action name="demo5Action" class="com.yujun.maven.action.Demo5Action">
<result>/demo5.jsp</result>
</action>
(3)页面

(4)访问
http://127.0.0.1:8080/struts2-chapter1-3/demo5Action!m1.action
struts2-第一章-基础用法3的更多相关文章
- struts2第一章-基本用法
一.struts简介 1.回顾 MVC M-model模型层 V-view 视图层 前端界面 C-controller 控制层 struts2: Apache提供的开源的控制层框架,相当于servl ...
- .net架构设计读书笔记--第一章 基础
第一章 基础 第一节 软件架构与软件架构师 简单的说软件架构即是为客户构建一个软件系统.架构师随便软件架构应运而生,架构师是一个角色. 2000年9月ANSI和IEEE发布了<密集性软件架构建 ...
- Laxcus大数据管理系统2.0(2)- 第一章 基础概述 1.1 基于现状的一些思考
第一章 基础概述 1.1 基于现状的一些思考 在过去十几年里,随着互联网产业的普及和高速发展,各种格式的互联网数据也呈现爆炸性增长之势.与此同时,在数据应用的另一个重要领域:商业和科学计算,在各种新兴 ...
- Linux系统shell编程自学_第一章基础
第一章 基础shell的优势在于处理操作系统底层的业务,Python,php的优势在于开发运维工具,web界面的管理工具以及web业务开发.处理一键安装.优化.报警脚本shell又叫命令解释器,它能识 ...
- wpf(第一章 基础知识)
wpf第一章基础知识:通过vs2015创建wpf程序会在引用里面多出3个核心程序集PresentationCore.PresentationFramework.WindowsBase.并且会在解决方案 ...
- 《openssl编程》:第一章基础知识
第一章 基础知识 1.1 对称算法 对称算法使用一个密钥.给定一个明文和一个密钥,加密产生密文,其长度和明文大致相同.解密时,使用读密钥与加密密钥相同. 对称算法主要有四种加密模式: (1) 电子密码 ...
- Python第一章-基础知识
第一章:基础知识 1.1 安装python. 直接官网下载最新的python然后默认安装就可以了,然后开始菜单里找到pyhton *.*.* Shell.exe运行python的交互shell ...
- 【SSH三框架】Struts2第一章的基础:第一次写Struts2规划
今年八月,当已经SSH三架完成学业.然后,他感动Android开展.三个框架已经很长的时间做无用的东西.所以,如果你想花三四天的时间来复习一下,写在博客. 附带SSH整个jar包网盘下载:http:/ ...
- 《Python基础教程(第二版)》学习笔记 -> 第一章 基础知识
写笔记的原因:书也看了一遍,视频也看了,但总是感觉效果不好,一段时间忘记了,再看又觉得有心无力,都是PDF的书籍,打开了就没有心情了,上班一天了,回家看这些东西,真的没多大精力了,所以,我觉得还是把p ...
- TypeScript学习指南第一章--基础数据类型(Basic Types)
基础数据类型(Basic Types) 为了搭建应用程序,我们需要使用一些基础数据类型比如:numbers,strings,structures,boolean等等. 在TypeScript中除了Ja ...
随机推荐
- vscode的插件收集
转:https://zhuanlan.zhihu.com/p/27905838 转:https://segmentfault.com/a/1190000006697219
- docker添加阿里云专属镜像
阿里云镜像地址:https://link.zhihu.com/?target=https%3A//cr.console.aliyun.com/%23/accelerator 根据提示开启容器镜像服务, ...
- centos7服务器配置nuxt部署环境
一.安装node(默认安装在根目录下) 1.首先安装wget yum install -y wget 2.下载最新nodejs安装包 wget https://nodejs.org/dist/v10. ...
- mac下安装maven
在mac下 使用 brew安装,brew install maven 查看maven版本 mvn -version 打开Terminal,输入以下命令,设置Maven classpath 添加下列两行 ...
- mysql 在visual studio中的配置
视图-->其他窗口-->服务器资源管理器-->数据连接-->右键添加连接 servername:localhost username:root password:123456 ...
- 关于 redis 的 数据类型 和 内存模型
该文章 是在读了 公众号 : java 后端技术 之后 做的一个小记录 原文网址 : https://mp.weixin.qq.com/s/mI3nDtQdlVlLv2uUTxJegA 作者文章写的 ...
- python9--内存管理 引用计数 标记清除 分代回收
复习 文件处理 1.操作文件的三步骤 -- 打开文件:硬盘的空间被操作系统持有 | 文件对象被应用程序持续 -- 操作文件:读写操作 -- 释放文件:释放操作系统对硬盘空间的持有 2.基础的读写 ...
- Java单例模式之最优解分析【为何说是最优解】
代码如下 /** * * @ClassName: SingletionStaticInner * @Description: TODO[单例模式之最优解] * @author shundong.wu ...
- DirectX11--HR宏关于dxerr库的替代方案
DirectX11 With Windows SDK完整目录 欢迎加入QQ群: 727623616 可以一起探讨DX11,以及有什么问题也可以在这里汇报. 综述 参考文章:https://blogs. ...
- IntelliJ IDEA 2018最新版注册码激活方法
一.首先点击intellij idea 2018 二.选择激活码 三.输入以下激活码intellij idea 2018 最新版本 注册激活码 **************************** ...