码迷,mamicode.com
首页 > 其他好文 > 详细

struts2 的跳转方式

时间:2017-05-18 19:59:59      阅读:197      评论:0      收藏:0      [点我收藏+]

标签:struts2 的跳转方式

在struts2中有4中跳转方式

1. dispatcher        请求转发(默认),只跳到jsp页面
2. redirect         重定向到jsp
3. redirectAction        重定向到action
4. chain           转发到action

在说这之前,让我们说下请求转发和重定向的概念,如果你已经了解转发和重定向,可以跳过,直接看下面。

转发和重定向

 1. 转发是服务器内部之间进行的

   重定向是用户重新向服务器发送新的请求

 2. 转发地址栏不会改变

   重定向由于是重新发送的新请求,地址栏会改变

 3. 转发共用一个作用域对象,相当于A.jsp页面的内容添加到B.jsp页面中。

   重定向,会得到一个信的作用对象

 4. 转发的速度比较重定向快(因为重定向重新发送请求)

什么时候用转发和重定向?

   前后两个页面,有数据传递,用请求转发,没有则用重定向。

   比如servlet查询了数据需要在页面显示,就用请求转发。

   比如servlet做了update操作跳转到其他页面。就用重定向。

下面让我们步入正题,先看看页面效果和项目的结构

  1.项目结构

    技术分享

2.看看页面的效果图,先页面枯燥,就加了个背景图片。记得自己修改。

 技术分享

让我们看下具体的实现细节,不要被神秘遮住了眼,其实它并不神秘。

  1. userIndex.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>

<%
	/* 项目名称 */
	String path = request.getContextPath();
	// 获取传输协议  
	String http = request.getScheme();
	// 获取服务器名称
	String serverName = request.getServerName();
	// 获取服务器端口号
	Integer serverPort = request.getServerPort();
	// 拼接起来的项目URL
	String bath = http + "://" + serverName + ":" + serverPort + path;
	System.out.println(bath);
%>   
<!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>Insert title here</title>
</head>
<body style="background-image: url(‘image/bg1.jpg‘);">
<!-- 跳转方式 -->
<a href="<%=bath %>/jump/jumpDipatcher?param=1">dispatcher 默认方式,请求转发</a><br/>
<a href="<%=bath %>/jump/jumpDipatcher?param=2">redirect 重定向到jsp页面</a><br/>
<a href="<%=bath %>/jump/jumpDipatcher?param=3">redirectAction 重定向到Action</a><br/>
<a href="<%=bath %>/jump/jumpDipatcher?param=4">chain 转发到Action</a><br/>
</body>
</html>

2.对于 login.jsp 和 error.jsp 和success.jsp。其实没有什么内容,只是一个页面,在这里不写了。

 不过你测试的时候,要写,当然名字你也可以自己写(毕竟是来测试,不是正规开发).

3.TestJump

package com.struts2.control;

/**
 * @author admin
 * 测试跳转方式
 */
public class TestJump {
	
	private String param; 
	
	public String getParam() {
		return param;
	}

	public void setParam(String param) {
		this.param = param;
	}

	public String testDispatcher(){
		if("1".equals(param)){
			System.out.println("-----Dispatcher--------");
			return "dispatcher";
		}else if("2".equals(param)){
			System.out.println("-----Redirect--------");
			return "redirect";
		}else if("3".equals(param)){
			System.out.println("-----RedirectAction--------");
			return "redirectAction";
		}else if("4".equals(param)){
		        System.out.println("-----Chain--------");
			return "chain";
		}
		return null;
	}
	public String returnUserIndex(){
		return "userIndex";
	}
	public String returnChain(){
		return "chain";
	}
}


4.看下struts.xml 是如何配置的,面纱即将揭晓。如果你对配置中的属性不知道,请参考上章

 http://11144439.blog.51cto.com/11134439/1926477 


<?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 name="jumps" extends="struts-default" namespace="/jump">
		<action name="jumpDipatcher" class="com.struts2.control.TestJump" method="testDispatcher">
			<!-- 请求转发,跳转到jsp页面 -->
			<result name="dispatcher" type="dispatcher">/success.jsp</result>
			<!-- 重定向到jsp页面 -->
			<result name="redirect" type="redirect">/success.jsp</result>
			<!-- 重定向到Action -->
			<result name="redirectAction" type="redirectAction">
			        <!-- 这里是要跳转到Action的 package的namespace,
			           注意  / 不要忽略了。
			         -->
				<param name="namespace">/jump</param>
				<!-- 这个是action中的name 看下图 -->
				<param name="actionName">returnUserIndex</param>
			</result>
			<!-- 转发到chain -->
			<result name="chain" type="chain">
				<param name="namespace">/jump</param>
				<param name="actionName">returnChain</param>
			</result>
		</action>
		<action name="returnUserIndex" class="com.struts2.control.TestJump" method="returnUserIndex">
			<result name="userIndex" type="redirect">/login.jsp</result>
		</action>
		<action name="returnChain" class="com.struts2.control.TestJump" method="returnChain">
			<result name="chain" type="redirect">/login.jsp</result>
		</action>
	</package>
</struts>

这个是转发Action中的配置关系图(redirectAction重定向到Action也是一样的道理)

技术分享



5.上面的配置中有没有发现什么?是不是感觉有跳转同一个页面的?没错,看下图

技术分享

如果按照上面配置是不是感觉代码又减少了,没有错,下面是配置,记得修改Action中返回字符串要和全局中的一致。

<?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 name="jumps" extends="struts-default" namespace="/jump">
		<!-- 全局跳转,抽取重复的result,简化配置 -->
		<global-results>
			<result name="success">/success.jsp</result>
			<result name="login">/login.jsp</result>
		</global-results>
		
		<action name="jumpDipatcher" class="com.struts2.control.TestJump" method="testDispatcher">
			<!-- 重定向到Action -->
			<result name="redirectAction" type="redirectAction">
				<param name="namespace">/jump</param>
				<param name="actionName">returnUserIndex</param>
			</result>
			<!-- 转发到chain -->
			<result name="chain" type="chain">
				<param name="namespace">/jump</param>
				<param name="actionName">returnChain</param>
			</result>
		</action>
		<action name="returnUserIndex" class="com.struts2.control.TestJump" method="returnUserIndex">
		</action>
		<action name="returnChain" class="com.struts2.control.TestJump" method="returnChain">
		</action>
	</package>
</struts>


6.说了这么多,让我们测试下结果如何

  1. dispacher 请求转发跳转.测试如图

    技术分享

  2. redirect 重定向 jsp,效果如下图

    技术分享

  3. redirectAction 重定向到Action,效果如下图

    技术分享


4. chain 转发到Action ,效果如图

技术分享

到这里告一段落,重要的是要亲自动手,亲自动手,亲自动手。好了重要的事情,已经说了三遍。收工,打造回府搂媳妇去了。


本文出自 “11134439” 博客,转载请与作者联系!

struts2 的跳转方式

标签:struts2 的跳转方式

原文地址:http://11144439.blog.51cto.com/11134439/1927211

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!