4.Struts2转向类型详解

时间:2022-09-21 22:24:32
struts2中提供了多种视图转向类型,类型由type属性指定,如:
dispatcher:请求转发(默认值)
redirect:重定向到页面
redirectAction:重定向到Action
plainText:原样显示要转向资源的源代码,而非显示代码的执行结果(火狐、IE8下可测试出效果,IE6下不行)。不常用。
注意,
对于请求转发的页面,可以是WEB-INF中页面;
而重定向的页面,是不能为WEB-INF中页的。
因为重定向相当于用户再次发出一次请求,而用户是不能直接访问WEB-INF中资源的。

服务器内部跳转

4.Struts2转向类型详解


服务器外部跳转

4.Struts2转向类型详解

先看下请求转发到的情况:

dispatcher:请求转发

4.Struts2转向类型详解

HelloWordAction.java源码如下:

package actions;

public class HelloWordAction {

    public String some(){

        System.out.println("I am HelloWordAction.java");
return "other";
} }

新建UserAction.java源码如下:

package actions;

public class UserAction {
private String username; public String getUsername() {
return username;
} public void setUsername(String username) {
this.username = username;
} public String execute(){
username="xingyun";
System.out.println("username="+username);
return "success";
} }

web.xml配置如下:

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"> <filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter> <filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping> <welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>

index.jsp源码如下:

<%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>"> <title>My JSP 'index.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head> <body>
This is my JSP page. <br>
</body>
</html>

welcome.jsp源码如下:

Welcome to Study Struts2!

show.jsp源码如下:

<%@ page isELIgnored="false" pageEncoding="ISO-8859-1"%>
<html>
<head> <title>show page</title> </head> <body>
show_username---------------------${username}
</body>
</html>

请求转发到页面struts.xml配置如下:

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd"> <struts>
<package name="aaaa" namespace="/test" extends="struts-default"> <action name="bbbb" class="actions.HelloWordAction" method="some">
<result name="other">/welcome.jsp</result>
</action> <action name="user" class="actions.UserAction">
<result type="dispatcher">/show.jsp</result>
</action> </package> </struts>

原理图:

4.Struts2转向类型详解


dispatcher:请求转发

发布项目,启动tomcat,地址栏输入:

http://pc2014092716pel:8080/Study_Struts2/test/user

4.Struts2转向类型详解


现在看看重定向到页面是什么情况!

redirect:重定向到页面

重定向到页面原理图:

4.Struts2转向类型详解


redirect重定向引出的问题?

上面都不变,只修改struts.xml文件成重定向配置如下:

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd"> <struts>
<package name="aaaa" namespace="/test" extends="struts-default"> <action name="bbbb" class="actions.HelloWordAction" method="some">
<result name="other">/welcome.jsp</result>
</action> <action name="user" class="actions.UserAction">
<result type="redirect">/show.jsp</result>
</action> </package> </struts>

redirect:重定向到页面

重新部署,启动tomcat,地址栏输入:

http://pc2014092716pel:8080/Study_Struts2/test/user

4.Struts2转向类型详解

解决重定向携带参数问题!

由上可以知道如果重定向的话,那么重定向时就无法携带参数。

要解决这个问题的话需要如此修改struts.xml配置为:

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd"> <struts>
<package name="aaaa" namespace="/test" extends="struts-default"> <action name="bbbb" class="actions.HelloWordAction" method="some">
<result name="other">/welcome.jsp</result>
</action> <action name="user" class="actions.UserAction"> <result type="redirect">/show.jsp?username=${username}</result>
</action> </package> </struts>

修改show.jsp页面如下:

<%@ page isELIgnored="false" pageEncoding="ISO-8859-1"%>
<html>
<head> <title>show page</title> </head> <body>
show_username---------------------${paramt.username}
</body>
</html>

运行结果如下:

4.Struts2转向类型详解

中文参数重定向问题?

但是如果变量字符串"xingyun"变成"星云"就会出现乱码。

UserAction.java代码修改如下:

package actions;

public class UserAction {
private String username; public String getUsername() {
return username;
} public void setUsername(String username) {
this.username = username;
} public String execute(){
username="星云";
System.out.println("username="+username);
return "success";
} }

show.jsp修改如下:

<%@ page isELIgnored="false" pageEncoding="gb2312"%>
<html>
<head> <title>show page</title> </head> <body>
show_username=${param.username}
</body>
</html>

重新部署,发布后启动后效果图如下:

4.Struts2转向类型详解


中文参数重定向解决方法:

修改UserAction.java

package actions;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder; public class UserAction {
private String username; public String getUsername() {
return username;
} public void setUsername(String username) {
this.username = username;
} public String execute() throws UnsupportedEncodingException{
//将中文内容按指定字符集进行重编码
username=URLEncoder.encode("星云","gb2312");
System.out.println("username="+username);
return "success";
} }

修改show.jsp如下:

<%@ page isELIgnored="false" pageEncoding="gb2312" import="java.net.*"%>
<html>
<head> <title>show page</title> </head> <body>
<%
//1.获取请求参数中的中文经编码后的字符串
String encoderStr=request.getParameter("username");
//2.将获取的编码后的字符串存放到字节数组中
byte[] requestBytes=encoderStr.getBytes("ISO8859-1");
//3.将该字符串按指定的字符集进行解码
String requestString=new String(requestBytes);
//4.该字符串按指定字符集进行解码
String username=URLDecoder.decode(requestString,"gb2312");
%>
username===========<%=username%>
</body>
</html>

解决方法原理图:

4.Struts2转向类型详解

重新部署发布成功效果如下:

4.Struts2转向类型详解


重定向到servletaction类

redirectAction:重定向到Action

原理图:

4.Struts2转向类型详解

先访问test/two对应的HelloWordAction.java,完成后找user对应的UserAction.java,最后重定向到show.jsp。

修改HelloWordAction.java源码如下:

package actions;

public class HelloWordAction {

    public String some(){

        System.out.println("----------------do some()");
return "some";
}
public String other(){ System.out.println("----------------do other()");
return "other";
} }

重定向到servlet_action类需要修改struts.xml配置如下:

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd"> <struts>
<package name="aaaa" namespace="/test" extends="struts-default"> <action name="one" class="actions.HelloWordAction" method="some">
<result name="some">/welcome.jsp</result>
</action>
<action name="two" class="actions.HelloWordAction" method="other">
<result name="other" type="redirectAction">user</result>
</action>
<action name="user" class="actions.UserAction"> <result type="redirect">/show.jsp?username=${username}</result>
</action> </package> </struts>

重新部署发布,启动tomcat,输入地址:

http://127.0.0.1:8080/Study_Struts2/test/two

运行截图如下:

4.Struts2转向类型详解

跨包重定向问题?

redirectAction:重定向到Action

原理图:

4.Struts2转向类型详解

修改struts.xml配置如下:

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd"> <struts>
<package name="aaaa" namespace="/one" extends="struts-default"> <action name="two" class="actions.HelloWordAction" method="other"> <result name="other" type="redirectAction"> <param name="namespace">/test2</param>
<param name="actionName">user</param>
</result>
</action> </package> <!--**************分开标记注释*********************--> <package name="test2" namespace="/test2" extends="struts-default"> <action name="user" class="actions.UserAction"> <result type="redirect">/show.jsp?username=${username}</result>
</action> </package> </struts>

redirectAction:重定向到Action

重新部署发布,开启tomcat,输入地址:

http://127.0.0.1:8080/Study_Struts2/one/two

运行截图:

4.Struts2转向类型详解

plainText:原样显示要转向资源的源代码,而非显示代码的执行结果(火狐、IE8下可测试出效果,IE6下不行)。不常用。

4.Struts2转向类型详解

修改struts.xml配置如下:

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd"> <struts>
<package name="aaaa" namespace="/one" extends="struts-default"> <action name="two" class="actions.HelloWordAction" method="other"> <result name="other" type="plainText">index.jsp</result>
</action> </package> </struts>

部署发布,开启tomcat,输入地址:

http://127.0.0.1:8080/Study_Struts2/one/two

运行截图:

4.Struts2转向类型详解