本案例采用的是struts中的2.5.8和之前的2.3.4在配置上略有不同,但是大致的方式相同
Struts2中的重定向有两个分别为redirect和redirectAction
在struts2-core.2.5.8.jar包中有struts-default.xml文件代开查看如下代码定义了重定向
(重定向的根本就是服务器返回301和一个url,301表示返回结果为重定向,url为重定向的新地址)
第一个redirect是重定向到某一个页面或网址(包括外网)
API中如下描述:
redirect用于重定向到action
API中如下描述:
API中描述了在不同包中的重定向,还要定义一个namespace参数,本案例中,是在同一个package中进行演示的,所以不用定义namespace这个参数
该案例导包:
spring-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-4.3.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd
http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-4.3.xsd
http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-4.3.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.3.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-4.3.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.3.xsd">
<!-- spring的组件扫描 -->
<context:component-scan base-package="cn.tedu"/>
</beans>
web.xml文件如下:
<?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>struts_day03</display-name>
<filter>
<display-name>StrutsPrepareAndExecuteFilter</display-name>
<filter-name>StrutsPrepareAndExecuteFilter</filter-name>
<filter-class>org.apache.struts2.dispatcher.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>StrutsPrepareAndExecuteFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:conf/spring-*.xml</param-value>
</context-param>
</web-app>
struts.xml如下:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.5//EN"
"http://struts.apache.org/dtds/struts-2.5.dtd">
<struts>
<package namespace="/demo" name="demo" extends="struts-default,json-default">
<!-- 直接显示图片 -->
<action name="photo" class="photoAction">
<result name="success" type="stream">
<!-- 发送图片信息 -->
<param name="contentType">image/jpeg</param>
<!-- in为photoAction类中的一个bean属性,其类型必须是InputStream类型 -->
<param name="inputName">imageStream</param>
</result>
</action>
<!-- 从定向 -->
<action name="test" method="t1" class="redirectAction">
<!-- redirect用于重定向到URL 直接跳转到某个网址(外网均可) -->
<result name="doc" type="redirect">
https://www.baidu.com/
<!-- location是默认属性,可以不写param标签
<param name="location">
https://www.baidu.com/
</param>
-->
</result>
<!-- 从定向到某个页面 -->
<result name="doc1" type="redirect">
/success.jsp
</result>
<!-- redirect用于重定向到action -->
<result name="photo" type="redirectAction">
<!-- actionName 是默认属性,可以不写param标签 -->
photo
</result>
</action>
<!-- 可以自行扩展Result类型 -->
<!-- <result-types></result-types> -->
</package>
</struts>
控制器如下:
package cn.tedu.redirect;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
//用于测试从定向
@Controller
@Scope("prototype")
public class RedirectAction {
private int type;
public int getType() {
return type;
}
public void setType(int type) {
this.type = type;
}
public String t1(){
if(type == 0){ //从定向到百度
return "doc";
}else if(type == 1){ //从定向到某个jsp
return "doc1";
}else{
return "photo"; //从定向到某个action
}
}
}
当访问地址为:http://localhost:8000/struts_day03/demo/test?type=0
由于type=0,控制器返回doc,重定向到百度
当访问地址为:http://localhost:8000/struts_day03/demo/test?type=1
由于type=1,控制器返回doc1,重定success.jsp
当访问地址为:http://localhost:8000/struts_day03/demo/test?type=2
由于type=1,控制器返回photo,重定向到action为photo的antion,进入图片显示的界面