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 ...
随机推荐
- 归并排序-JAVA实现
package com.iloveu.xxx; public class MergeSort { static final int SIZE = 15; static void mergeOne(in ...
- php Header 函数使用
<?php header('HTTP/1.1 200 OK'); // ok 正常访问 header('HTTP/1.1 404 Not Found'); //通知浏览器 页面不存在 heade ...
- mysql 修改表结构、表字段注释语句
虽然现在有各种各样的工具可以直接对表结构进行修改,但是我还是喜欢使用语句进行修改.以下语句是对表增加字段.给字段加注释的语句 alter table orders add column isupdyq ...
- Redisson实现分布式锁
转: Redisson实现分布式锁 Redisson文档参考:https://github.com/redisson/redisson/wiki/%E7%9B%AE%E5%BD%95 redis是实现 ...
- JSP中EL很常用,怎样使用大于号、小于号、等于号等
JSP中EL很常用,怎样使用大于号.小于号.等于号等 符号 在EL中使用 常规 1 等于 eq == 2 不等于 ne != 3 大于 gt > 4 小于 lt < 5 大于等于 ge ...
- redis cluster简介和配置(3)
前面我介绍了 redis sentinel,既然有了sentinel,为什么还要一个cluster呢?因为随着业务量的增加,不可避免要对redis进行扩容,扩容方式一般由2种:1. 垂直扩容 2. 水 ...
- 第四周博客作业<西北师范大学|李晓婷>
1.助教博客链接:https://home.cnblogs.com/u/lxt-/ 2.作业要求链接:www.cnblogs.com/nwnu-daizh/p/10487329.html 3.本周点评 ...
- jQuery使用(十五):when()方法的使用
这个方法再次基于callbacks和deferred对象来实现的一个延迟异步回调来实现的,再when方法内可以添加多个deferred对象作为参数,只有当所有deferred全部被触发了成功的回调函数 ...
- JS“盒子模型”
列举几个常用的属性 client系列 clientWidth - 盒子真实内容的宽度[content+padding左右],不包括边线和滚动条 clientHeight - 盒子真实内容的高度[con ...
- 全排列递归算法(元素有重复与无重复,C++实现)
元素无重复: 如:2,5,8,9. 思路:用递归的方法解决,对于2589,先输出所有以2开头的排列,然后输出5开头的排列.....(此处称为递归操作A).以2开头的排列中,第一位是2,后面的是589, ...