Struts2框架05 result标签的类型、拦截器
1 result标签是干什么的
就是结果,服务器处理完返回给浏览器的结果;是一个输出结果数据的组件
2 什么时候需要指定result标签的类型
把要输出的结果数据按照我们指定的数据类型进行处理
3 常见的类型(在struts的默认配置文件120行有这些类型的列表)
3.1 dispatcher 转发(默认类型)
3.2 redirect 重定向URL
格式一
<result type="redirect">
网址(例如:http://www.baidu.com)
</result>
格式二(注意:param标签的name属性是固定值location)
<result type="redirect">
<param name="location">
http://www.baidu.com
</param>
</result>
1 <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/xsd/maven-4.0.0.xsd"> 2 <modelVersion>4.0.0</modelVersion> 3 <groupId>cn.xiangxu</groupId> 4 <artifactId>ssh02</artifactId> 5 <version>0.0.1-SNAPSHOT</version> 6 <packaging>war</packaging> 7 <dependencies> 8 <dependency> 9 <groupId>org.apache.struts</groupId> 10 <artifactId>struts2-core</artifactId> 11 <version>2.3.8</version> 12 </dependency> 13 <dependency> 14 <groupId>org.apache.struts</groupId> 15 <artifactId>struts2-spring-plugin</artifactId> 16 <version>2.3.8</version> 17 </dependency> 18 </dependencies> 19 </project>
1 <?xml version="1.0" encoding="UTF-8"?> 2 <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" version="2.5"> 3 <display-name>ssh02s</display-name> 4 <welcome-file-list> 5 <welcome-file>index.html</welcome-file> 6 <welcome-file>index.htm</welcome-file> 7 <welcome-file>index.jsp</welcome-file> 8 <welcome-file>default.html</welcome-file> 9 <welcome-file>default.htm</welcome-file> 10 <welcome-file>default.jsp</welcome-file> 11 </welcome-file-list> 12 13 <!-- 配置spring监听listener 14 用于初始化Spring容器 --> 15 <listener> 16 <listener-class> 17 org.springframework.web.context.ContextLoaderListener 18 </listener-class> 19 </listener> 20 21 <!-- 配置Spring配置文件的位置 --> 22 <context-param> 23 <param-name>contextConfigLocation</param-name> 24 <param-value>classpath:spring-*.xml</param-value> 25 </context-param> 26 27 <!-- 配置主控制器和过滤条件 --> 28 <filter> 29 <filter-name>mvc</filter-name> 30 <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class> 31 </filter> 32 <filter-mapping> 33 <filter-name>mvc</filter-name> 34 <url-pattern>/*</url-pattern> 35 </filter-mapping> 36 37 </web-app>
1 <?xml version="1.0" encoding="UTF-8"?> 2 <beans xmlns="http://www.springframework.org/schema/beans" 3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" 4 xmlns:jdbc="http://www.springframework.org/schema/jdbc" xmlns:jee="http://www.springframework.org/schema/jee" 5 xmlns:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop" 6 xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:util="http://www.springframework.org/schema/util" 7 xmlns:jpa="http://www.springframework.org/schema/data/jpa" 8 xsi:schemaLocation=" 9 http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd 10 http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd 11 http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.0.xsd 12 http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.0.xsd 13 http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd 14 http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa-1.3.xsd 15 http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd 16 http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd 17 http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.0.xsd"> 18 19 <!-- 配置组件扫描 --> 20 <context:component-scan base-package="cn.xiangxu" /> 21 22 </beans>
1 <?xml version="1.0" encoding="UTF-8"?> 2 <!DOCTYPE struts PUBLIC 3 "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN" 4 "http://struts.apache.org/dtds/struts-2.3.dtd"> 5 <struts> 6 7 <package name="type" namespace="/type" extends="struts-default"> 8 <action name="redirect" class="redirectAction"> <!-- 后台经过处理后,再进行重定向 --> 9 <result name="redirect" type="redirect"> 10 http://www.baidu.com 11 </result> 12 </action> 13 14 <action name="cq"> <!-- 直接重定向,在后台不进行任何处理 --> 15 <result type="redirect"> 16 http://cq.qq.com/ 17 </result> 18 </action> 19 20 <action name="dzsk"> <!-- 利用格式二实现 --> 21 <result type="redirect"> 22 http://www.dzshike.com/ 23 </result> 24 </action> 25 </package> 26 27 </struts>
1 package cn.xiangxu.action; 2 3 import org.springframework.context.annotation.Scope; 4 import org.springframework.stereotype.Controller; 5 6 @Controller 7 @Scope("prototype") 8 public class RedirectAction { 9 public String execute() { 10 System.out.println("测试重定向URL"); 11 return "redirect"; 12 } 13 }
项目结构
3.3 redirectAction 重定向Action
格式一:请求路径不变,仅仅改变请求名
<result name="xxx" type="redirectAction">
请求名/请求名.action
</result>
格式二:请求路径和请求名都该改变
<result name="xxx" type="redirectAction">
<param name="namespace">/路径名</param>
<param name="actionName">请求名/请求名.action</param>
</result>
1 <?xml version="1.0" encoding="UTF-8"?> 2 <!DOCTYPE struts PUBLIC 3 "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN" 4 "http://struts.apache.org/dtds/struts-2.3.dtd"> 5 <struts> 6 7 <package name="test" namespace="/test" extends="struts-default"> <!-- namespace是配置访问路径,extends是配置继承默认struts文件 --> 8 <action name="demo" class="testAction"> <!-- name是配置访问网名,class是配置action类 --> 9 <result name="success"> 10 /WEB-INF/jsp/msg.jsp 11 </result> 12 </action> 13 </package> 14 15 <package name="type" namespace="/type" extends="struts-default"> 16 17 18 <action name="demo" class="testAction"> 19 <result name="success"> 20 /WEB-INF/jsp/msg.jsp 21 </result> 22 </action> 23 24 <action name="redirectAction01" class="redirectActionAction"> <!-- 重定向1:只改变请求网名 --> 25 <result name="redirectAction" type="redirectAction"> 26 demo 27 </result> 28 <result name="redirectAction" type="redirectAction"> 29 demo 30 </result> 31 </action> 32 <action name="redirectAction02" class="redirectActionAction"> <!-- 重定向2:路径和网名都改变 --> 33 <result name="redirectAction" type="redirectAction"> 34 <param name="namespace">/test</param> 35 <param name="actionName">demo</param> 36 </result> 37 </action> 38 39 </package> 40 41 </struts>
1 package cn.xiangxu.action; 2 3 import org.springframework.context.annotation.Scope; 4 import org.springframework.stereotype.Controller; 5 6 @Controller 7 @Scope("prototype") 8 public class RedirectActionAction { 9 public String execute() { 10 System.out.println("测试重定向action"); 11 return "redirectAction"; 12 } 13 }
项目结构
3.4 stream 流,处理图片
3.4.1 格式
<result name="success" type="stream">
<param name="contentType">image/jpeg</param>
<param name="inputName">imageStream</param>
<param name="contentDisposition">attachment;filename="document.pdf"</param>
<param name="bufferSize">1024</param>
</result>
3.4.2 参数解释
contentType 定义媒体类型
inputName 必须是一个inputStream类型的流
contentDisposition 强制下载保存(可选)
bufferSize 指定缓存区域的大小(可选)
3.4.3 案例:向浏览器发送图片数据并显示
1 <?xml version="1.0" encoding="UTF-8"?> 2 <!DOCTYPE struts PUBLIC 3 "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN" 4 "http://struts.apache.org/dtds/struts-2.3.dtd"> 5 <struts> 6 7 8 <package name="type" namespace="/type" extends="struts-default"> 9 10 11 <action name="stream" class="streamAction"> 12 <result name="success" type="stream"> 13 <param name="contentType">image/png</param> 14 <param name="inputName">image</param> <!-- image是ImageAction中的一个成员变量 --> 15 </result> 16 </action> 17 18 </package> 19 20 21 22 </struts>
1 package cn.xiangxu.action; 2 3 import java.awt.Color; 4 import java.awt.Graphics2D; 5 import java.awt.image.BufferedImage; 6 import java.io.ByteArrayInputStream; 7 import java.io.ByteArrayOutputStream; 8 import java.io.IOException; 9 import java.io.InputStream; 10 11 import javax.imageio.ImageIO; 12 13 import org.springframework.context.annotation.Scope; 14 import org.springframework.stereotype.Controller; 15 16 @Controller 17 @Scope("prototype") 18 public class StreamAction { 19 20 private InputStream image; 21 22 public String execute() throws IOException { 23 // 设置图片 24 BufferedImage img = new BufferedImage(399, 60, BufferedImage.TYPE_3BYTE_BGR); 25 26 // 获取画笔 27 Graphics2D g = img.createGraphics(); 28 g.setColor(Color.white); 29 g.drawString("hello fury,测试stream", 10, 30); 30 31 // 将图片转成字节数组 32 ByteArrayOutputStream out = new ByteArrayOutputStream(); 33 ImageIO.write(img, "png", out); 34 out.close(); 35 36 // 将字节数组放到字节流中 37 byte [] data = out.toByteArray(); 38 image = new ByteArrayInputStream(data); 39 40 return "success"; 41 } 42 43 public InputStream getImage() { 44 return image; 45 } 46 47 public void setImage(InputStream image) { 48 this.image = image; 49 } 50 }
项目结构
3.5 json 用于处理ajax请求
导包:struts2-json-plugin
注意:package标签中的extends属性值变更为json-default(json-default继承了struts-default)
用法:两种方式
返回Action中的所有值
<result name="xxx" type="json"></result>
返回Action中的单个属性值
<result name="xxx" type="json">
<param name="root">action中的属性名</param> <!-- name的属性值是固定的 -->
</result>
1 <?xml version="1.0" encoding="UTF-8"?> 2 <!DOCTYPE struts PUBLIC 3 "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN" 4 "http://struts.apache.org/dtds/struts-2.3.dtd"> 5 <struts> 6 7 8 <package name="type" namespace="/type" extends="json-default"> 9 10 11 <action name="json" class="jsonAction"> <!-- 返回多个值 --> 12 <result name="success" type="json"></result> 13 </action> 14 15 <action name="json02" class="jsonAction"> <!-- 返回一个值 --> 16 <result name="success" type="json"> 17 <param name="root">name</param> 18 </result> 19 </action> 20 21 22 </package> 23 24 25 </struts>
1 package cn.xiangxu.action; 2 3 import org.springframework.context.annotation.Scope; 4 import org.springframework.stereotype.Controller; 5 6 @Controller 7 @Scope("prototype") 8 public class JsonAction { 9 private String name; 10 private Integer age; 11 12 public String execute() { 13 name = "wys"; 14 age = 100; 15 return "success"; 16 } 17 18 public String getName() { 19 return name; 20 } 21 public void setName(String name) { 22 this.name = name; 23 } 24 public Integer getAge() { 25 return age; 26 } 27 public void setAge(Integer age) { 28 this.age = age; 29 } 30 31 32 }
1 <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/xsd/maven-4.0.0.xsd"> 2 <modelVersion>4.0.0</modelVersion> 3 <groupId>cn.xiangxu</groupId> 4 <artifactId>ssh02</artifactId> 5 <version>0.0.1-SNAPSHOT</version> 6 <packaging>war</packaging> 7 <dependencies> 8 <dependency> 9 <groupId>org.apache.struts</groupId> 10 <artifactId>struts2-core</artifactId> 11 <version>2.3.8</version> 12 </dependency> 13 <dependency> 14 <groupId>org.apache.struts</groupId> 15 <artifactId>struts2-spring-plugin</artifactId> 16 <version>2.3.8</version> 17 </dependency> 18 <dependency> 19 <groupId>org.apache.struts</groupId> 20 <artifactId>struts2-json-plugin</artifactId> 21 <version>2.3.8</version> 22 </dependency> 23 </dependencies> 24 </project>
项目结构图
4 小案例:根据浏览器发送的不同数据,跳转到不同的页面(类似于百度搜索)
4.1 要求:
用户输入搜索关键字后点击搜索按钮,页面直接就跳转到相应页面
例如:输入“百度”,点击搜索后直接跳转到百度的官网
4.2 struts.xml配置文件中action标签的method属性的作用
如果不指定method属性,那么在相应的action类中的处理方法名必须是execute;
如果指定了method属性后,那么在相应的action类中的处理方法名就是method的属性值
4.3 效果演示
点击“搜索”按钮后就跳转到百度的首页啦
4.4 源代码
1 <%@ page language="java" contentType="text/html; charset=utf-8" 2 pageEncoding="utf-8"%> 3 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 4 <html> 5 <head> 6 <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> 7 <title>test</title> 8 </head> 9 <body> 10 <div> 11 <form action="http://localhost:8080/ssh02/case/switch" method="post"> 12 请输入搜索词条:<input type="text" name="index" /> 13 <input type="submit" value="搜索" /> 14 </form> 15 </div> 16 </body> 17 </html>
1 <?xml version="1.0" encoding="UTF-8"?> 2 <!DOCTYPE struts PUBLIC 3 "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN" 4 "http://struts.apache.org/dtds/struts-2.3.dtd"> 5 <struts> 6 7 8 9 <package name="case" namespace="/case" extends="struts-default"> 10 <action name="toChoose"> 11 <result> 12 /WEB-INF/jsp/choose.jsp 13 </result> 14 </action> 15 <action name="demo"> 16 <result> 17 /WEB-INF/jsp/msg.jsp 18 </result> 19 </action> 20 <action name="switch" class="switchAction" method="doSwitch"> 21 <result name="百度" type="redirect"> 22 http://www.baidu.com 23 </result> 24 25 <result name="msg" type="redirectAction"> 26 demo 27 </result> 28 29 <result name="main"> 30 /WEB-INF/jsp/main.jsp 31 </result> 32 </action> 33 </package> 34 35 </struts>
1 package cn.xiangxu.action; 2 3 import org.springframework.context.annotation.Scope; 4 import org.springframework.stereotype.Controller; 5 6 @Controller 7 @Scope("prototype") 8 public class SwitchAction { 9 private String index; // 该成员变量名必须和choose.jsp中相关input标签的name属性值相同 10 11 public String doSwitch() { 12 System.out.println("你要搜索的词条是:" + index); 13 14 return index; 15 } 16 17 public String getIndex() { 18 return index; 19 } 20 21 public void setIndex(String index) { 22 this.index = index; 23 } 24 25 26 27 }
1 <%@ page language="java" contentType="text/html; charset=utf-8" 2 pageEncoding="utf-8"%> 3 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 4 <html> 5 <head> 6 <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> 7 <title>test</title> 8 </head> 9 <body> 10 <h2>欢迎来到庠序科技主页面</h2> 11 </body> 12 </html>
1 <%@ page language="java" contentType="text/html; charset=utf-8" 2 pageEncoding="utf-8"%> 3 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 4 <html> 5 <head> 6 <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> 7 <title>test</title> 8 </head> 9 <body> 10 <h2>恭喜你,struts整合spring成功!</h2> 11 <h2>hello world</h2> 12 </body> 13 </html>
4.5 项目结构
6 拦截器
6.1 什么是拦截器
java中动态拦截Action调用的对象
6.2 为什么要用拦截器
封装了一些通用处理,便于复用;通过配置的方式进行调用,比较灵活,便于扩展
6.3 什么时候使用
在不修改现有逻辑代码的情况下,为系统追加共有的功能处理
6.4 怎么使用拦截器
创建拦截器
注册拦截器
引用拦截器
6.4.1 拦截器的编写规则
编写一个类,这个类需要实现interceptor接口,在intercept方法中做共同的处理
1 package cn.xiangxu.util; 2 3 import com.opensymphony.xwork2.ActionInvocation; 4 import com.opensymphony.xwork2.interceptor.Interceptor; 5 6 public class MyInterceptor implements Interceptor { 7 8 private static final long serialVersionUID = -5324310407222552593L; 9 10 public void destroy() { 11 // TODO Auto-generated method stub 12 13 } 14 15 public void init() { 16 // TODO Auto-generated method stub 17 18 } 19 20 public String intercept(ActionInvocation in) throws Exception { 21 System.out.println("进入拦截器!"); 22 in.invoke(); // 有了这条语句才能够进入到action控制类中 23 System.out.println("退出拦截器!"); 24 return null; 25 } 26 27 }
6.4.2 拦截器注册规则 (struts.xml中完成)
在package标签中注册拦截器 利用<Interceptor>标签
<interceptors> <!-- 注册拦截器 -->
<interceptor name="demo" class="cn.xiangxu.ljq.DemoInterceptor"></interceptor>
</interceptors>
6.4.3 拦截器的引用规则(struts.xml中完成) 注意:引用默认的拦截器,请看代码
在action标签中定义引用
<action name="json" class="jsonAction">
<result name="success" type="json"></result>
<interceptor-ref name="demo"></interceptor-ref> <!-- 引用拦截器 -->
</action>
注意:自己写得拦截器可能会和默认的拦截器产生冲突,解决办法是将默认的拦截器页引用进来;
默认拦截器相关信息的位置
改进版本如下