首页 > 代码库 > Struts2框架05 result标签的类型

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>
maven依赖文件
技术分享
 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>
web.xml
技术分享
 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>
spring_context.xml
技术分享
 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>
struts.xml
技术分享
 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 }
RedirectAction

项目结构

  技术分享

 

 

未完待续...

2017年7月12日22:37:18

 

Struts2框架05 result标签的类型