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>

 <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">
<modelVersion>4.0.0</modelVersion>
<groupId>cn.xiangxu</groupId>
<artifactId>ssh02</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
<dependencies>
<dependency>
<groupId>org.apache.struts</groupId>
<artifactId>struts2-core</artifactId>
<version>2.3.8</version>
</dependency>
<dependency>
<groupId>org.apache.struts</groupId>
<artifactId>struts2-spring-plugin</artifactId>
<version>2.3.8</version>
</dependency>
</dependencies>
</project>

maven依赖文件

 <?xml version="1.0" encoding="UTF-8"?>
<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">
<display-name>ssh02s</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list> <!-- 配置spring监听listener
用于初始化Spring容器 -->
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener> <!-- 配置Spring配置文件的位置 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring-*.xml</param-value>
</context-param> <!-- 配置主控制器和过滤条件 -->
<filter>
<filter-name>mvc</filter-name>
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>mvc</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping> </web-app>

web.xml

 <?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
xmlns:jdbc="http://www.springframework.org/schema/jdbc" xmlns:jee="http://www.springframework.org/schema/jee"
xmlns:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:util="http://www.springframework.org/schema/util"
xmlns:jpa="http://www.springframework.org/schema/data/jpa"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.0.xsd
http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa-1.3.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.0.xsd"> <!-- 配置组件扫描 -->
<context:component-scan base-package="cn.xiangxu" /> </beans>

spring_context.xml

 <?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
"http://struts.apache.org/dtds/struts-2.3.dtd">
<struts> <package name="type" namespace="/type" extends="struts-default">
<action name="redirect" class="redirectAction"> <!-- 后台经过处理后,再进行重定向 -->
<result name="redirect" type="redirect">
http://www.baidu.com
</result>
</action> <action name="cq"> <!-- 直接重定向,在后台不进行任何处理 -->
<result type="redirect">
http://cq.qq.com/
</result>
</action> <action name="dzsk"> <!-- 利用格式二实现 -->
<result type="redirect">
http://www.dzshike.com/
</result>
</action>
</package> </struts>

struts.xml

 package cn.xiangxu.action;

 import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller; @Controller
@Scope("prototype")
public class RedirectAction {
public String execute() {
System.out.println("测试重定向URL");
return "redirect";
}
}

RedirectAction

项目结构

  

  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>

 <?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
"http://struts.apache.org/dtds/struts-2.3.dtd">
<struts> <package name="test" namespace="/test" extends="struts-default"> <!-- namespace是配置访问路径,extends是配置继承默认struts文件 -->
<action name="demo" class="testAction"> <!-- name是配置访问网名,class是配置action类 -->
<result name="success">
/WEB-INF/jsp/msg.jsp
</result>
</action>
</package> <package name="type" namespace="/type" extends="struts-default"> <action name="demo" class="testAction">
<result name="success">
/WEB-INF/jsp/msg.jsp
</result>
</action> <action name="redirectAction01" class="redirectActionAction"> <!-- 重定向1:只改变请求网名 -->
<result name="redirectAction" type="redirectAction">
demo
</result>
<result name="redirectAction" type="redirectAction">
demo
</result>
</action>
<action name="redirectAction02" class="redirectActionAction"> <!-- 重定向2:路径和网名都改变 -->
<result name="redirectAction" type="redirectAction">
<param name="namespace">/test</param>
<param name="actionName">demo</param>
</result>
</action> </package> </struts>

struts.xml

 package cn.xiangxu.action;

 import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller; @Controller
@Scope("prototype")
public class RedirectActionAction {
public String execute() {
System.out.println("测试重定向action");
return "redirectAction";
}
}

RedirectActionAction.java

项目结构

  

  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 案例:向浏览器发送图片数据并显示

 <?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
"http://struts.apache.org/dtds/struts-2.3.dtd">
<struts> <package name="type" namespace="/type" extends="struts-default"> <action name="stream" class="streamAction">
<result name="success" type="stream">
<param name="contentType">image/png</param>
<param name="inputName">image</param> <!-- image是ImageAction中的一个成员变量 -->
</result>
</action> </package> </struts>

struts.xml

 package cn.xiangxu.action;

 import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream; import javax.imageio.ImageIO; import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller; @Controller
@Scope("prototype")
public class StreamAction { private InputStream image; public String execute() throws IOException {
// 设置图片
BufferedImage img = new BufferedImage(399, 60, BufferedImage.TYPE_3BYTE_BGR); // 获取画笔
Graphics2D g = img.createGraphics();
g.setColor(Color.white);
g.drawString("hello fury,测试stream", 10, 30); // 将图片转成字节数组
ByteArrayOutputStream out = new ByteArrayOutputStream();
ImageIO.write(img, "png", out);
out.close(); // 将字节数组放到字节流中
byte [] data = out.toByteArray();
image = new ByteArrayInputStream(data); return "success";
} public InputStream getImage() {
return image;
} public void setImage(InputStream image) {
this.image = image;
}
}

StreamAction.java

    项目结构

      

  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>

 <?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
"http://struts.apache.org/dtds/struts-2.3.dtd">
<struts> <package name="type" namespace="/type" extends="json-default"> <action name="json" class="jsonAction"> <!-- 返回多个值 -->
<result name="success" type="json"></result>
</action> <action name="json02" class="jsonAction"> <!-- 返回一个值 -->
<result name="success" type="json">
<param name="root">name</param>
</result>
</action> </package> </struts>

struts.xml

 package cn.xiangxu.action;

 import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller; @Controller
@Scope("prototype")
public class JsonAction {
private String name;
private Integer age; public String execute() {
name = "wys";
age = 100;
return "success";
} public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
} }

JsonAction.java

 <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">
<modelVersion>4.0.0</modelVersion>
<groupId>cn.xiangxu</groupId>
<artifactId>ssh02</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
<dependencies>
<dependency>
<groupId>org.apache.struts</groupId>
<artifactId>struts2-core</artifactId>
<version>2.3.8</version>
</dependency>
<dependency>
<groupId>org.apache.struts</groupId>
<artifactId>struts2-spring-plugin</artifactId>
<version>2.3.8</version>
</dependency>
<dependency>
<groupId>org.apache.struts</groupId>
<artifactId>struts2-json-plugin</artifactId>
<version>2.3.8</version>
</dependency>
</dependencies>
</project>

pom.xml

项目结构图

  

4 小案例:根据浏览器发送的不同数据,跳转到不同的页面(类似于百度搜索)

  4.1 要求:

    用户输入搜索关键字后点击搜索按钮,页面直接就跳转到相应页面

    例如:输入“百度”,点击搜索后直接跳转到百度的官网

  4.2 struts.xml配置文件中action标签的method属性的作用

    如果不指定method属性,那么在相应的action类中的处理方法名必须是execute;

    如果指定了method属性后,那么在相应的action类中的处理方法名就是method的属性值

    

  4.3 效果演示

    

    点击“搜索”按钮后就跳转到百度的首页啦

  4.4 源代码

 <%@ 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>test</title>
</head>
<body>
<div>
<form action="http://localhost:8080/ssh02/case/switch" method="post">
请输入搜索词条:<input type="text" name="index" />&nbsp;&nbsp;
<input type="submit" value="搜索" />
</form>
</div>
</body>
</html>

shoose.jsp

 <?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
"http://struts.apache.org/dtds/struts-2.3.dtd">
<struts> <package name="case" namespace="/case" extends="struts-default">
<action name="toChoose">
<result>
/WEB-INF/jsp/choose.jsp
</result>
</action>
<action name="demo">
<result>
/WEB-INF/jsp/msg.jsp
</result>
</action>
<action name="switch" class="switchAction" method="doSwitch">
<result name="百度" type="redirect">
http://www.baidu.com
</result> <result name="msg" type="redirectAction">
demo
</result> <result name="main">
/WEB-INF/jsp/main.jsp
</result>
</action>
</package> </struts>

struts.xml

 package cn.xiangxu.action;

 import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller; @Controller
@Scope("prototype")
public class SwitchAction {
private String index; // 该成员变量名必须和choose.jsp中相关input标签的name属性值相同 public String doSwitch() {
System.out.println("你要搜索的词条是:" + index); return index;
} public String getIndex() {
return index;
} public void setIndex(String index) {
this.index = index;
} }

SwitchAction.java

 <%@ 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>test</title>
</head>
<body>
<h2>欢迎来到庠序科技主页面</h2>
</body>
</html>

mian.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>test</title>
</head>
<body>
<h2>恭喜你,struts整合spring成功!</h2>
<h2>hello world</h2>
</body>
</html>

msg.jsp

  4.5 项目结构

     

6 拦截器

  6.1 什么是拦截器

    java中动态拦截Action调用的对象   

  6.2 为什么要用拦截器

    封装了一些通用处理,便于复用;通过配置的方式进行调用,比较灵活,便于扩展   

  6.3 什么时候使用

    在不修改现有逻辑代码的情况下,为系统追加共有的功能处理   

  6.4 怎么使用拦截器

    创建拦截器
    注册拦截器
    引用拦截器   

    6.4.1 拦截器的编写规则

      编写一个类,这个类需要实现interceptor接口,在intercept方法中做共同的处理

 package cn.xiangxu.util;

 import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.Interceptor; public class MyInterceptor implements Interceptor { private static final long serialVersionUID = -5324310407222552593L; public void destroy() {
// TODO Auto-generated method stub } public void init() {
// TODO Auto-generated method stub } public String intercept(ActionInvocation in) throws Exception {
System.out.println("进入拦截器!");
in.invoke(); // 有了这条语句才能够进入到action控制类中
System.out.println("退出拦截器!");
return null;
} }

MyInterceptor.java

    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>

      

      注意:自己写得拦截器可能会和默认的拦截器产生冲突,解决办法是将默认的拦截器页引用进来;

        默认拦截器相关信息的位置

  

      改进版本如下

        

Struts2框架05 result标签的类型、拦截器的更多相关文章

  1. Struts2框架05 result标签的类型

    1 result标签是干什么的 就是结果,服务器处理完返回给浏览器的结果:是一个输出结果数据的组件 2 什么时候需要指定result标签的类型 把要输出的结果数据按照我们指定的数据类型进行处理 3 常 ...

  2. AOP框架Dora.Interception 3.0 [3]: 拦截器设计

    对于所有的AOP框架来说,多个拦截器最终会应用到某个方法上.这些拦截器按照指定的顺序构成一个管道,管道的另一端就是针对目标方法的调用.从设计角度来将,拦截器和中间件本质是一样的,那么我们可以按照类似的 ...

  3. Struts2框架(5)---result结果集

    result结果集 上一篇文章主要讲Struts2框架(4)---Action类访问servlet这篇主要讲result结果集 在Struts.xml中的result元素指的是:指定动作类的动作方法执 ...

  4. Struts2之Action接收请求参数和拦截器

    技术分析之在Struts2框架中使用Servlet的API        1. 在Action类中也可以获取到Servlet一些常用的API        * 需求:提供JSP的表单页面的数据,在Ac ...

  5. Struts2学习(三)———— 输入校验和拦截器

    一.输入校验 在以前我们写一个登录页面时,并没有限制用户的输入,不管用户输入什么,我们都存入数据库中,很显然这是不行的,我们需要检测用户输入的文本是否合法,是否符合我们需要的文本格式,符合菜放行,而s ...

  6. struts2视频学习笔记 18(自定义拦截器)

    课时18 自定义拦截 因为struts2中如文件上传,数据验证,封装请求参数到action等功能都是由系统默认的defaultStack中的拦截器实现的,所以我们定义的拦截器需要引用系统默认的defa ...

  7. struts2之多文件上传与拦截器(8)

    前台jsp <s:form action="uploadAction" enctype="multipart/form-data" method=&quo ...

  8. struts2 基础3 文件上传、拦截器

    文件上传: 1.将头设置为enctype=”multipart/form-data” <form action="${pageContext.request.contextPath } ...

  9. 【struts2】自定义更强大的logger拦截器

    Struts2自带的logger拦截器只是打印出了Action所对应的URL以及执行的方法名称,这对实际开发来说是肯定不够的.实际开发中为了调试方便,要记录的信息比较多,通常需要把这次请求相关的几乎所 ...

随机推荐

  1. windows7安装PyQt5(通过pip install 安装)

    开始接触PyQt5 ,总结了一下安装的方法 默认各位已经安装好了Python环境 首先,确定一下之前没有安装过pyqt5,如果安装了,可以先卸载,避免出现意外, 之前装了几次没成功就是这种情况,卸载命 ...

  2. 浅谈Vue个性化dashBoard 布局

    dashBoard布局在管理系统使用比较多:使用自己喜欢的方式进行自定义布局 使用npm 安装 npm install vue-grid-layout 全局使用 import vueGridLayou ...

  3. 使文字出现波纹效果--第三方开源--Titanic

    下载地址:https://github.com/RomainPiel/Titanic 使用的时候直接将代码复制过来即可(注意res文件下有张波浪图也要一起复制) xml代码: <com.roma ...

  4. Leetcode 970. Powerful Integers

    Brute Force(暴力) class Solution(object): def powerfulIntegers(self, x, y, bound): """ ...

  5. Java String Split Method

    Java String.split() method 有如下几种特殊情况: 1. 分隔符出现在首尾 public static void main(String args[]) { String St ...

  6. STL理论基础、容器、迭代器、算法

    一.STL基本概念 STL(Standard Template Library,标准模板库)是惠普实验室开发的一系列软件的统称.现然主要出现在C++中,但在被引入C++之前该技术就已经存在了很长的一段 ...

  7. 利用sort对数组快速排序

    // sort内部使用快速排序,每次比较两个元素大小的时候如果没有参数,则直接判断字母表,如果有参数,则把正在比较的两个参数传入自定义方法并调用(正在比较的两个数会传给自定义方法的v1.v2),如果返 ...

  8. DIV横向排列_CSS如何让多个div盒子并排同行显示

    如何让多个div盒子并排同行div横向排列显示呢? 我们先设置3个div盒子对象,什么css样式都不设置看看效果.代码如下: 三个div盒子均独占一行显示 div盒子本身默认样式属性是独占一行,而解决 ...

  9. 洛谷【P1048】采药

    浅谈\(DP\):https://www.cnblogs.com/AKMer/p/10437525.html 题目传送门:https://www.luogu.org/problemnew/show/P ...

  10. python sentence

    1.while for 增加了循环正常结束后执行的else代码块. 2.Objects are mutable 3.import copy p1 = Point() p2=copy.copy(p1) ...