Struts2的简单使用(三)action跳转

在这里我要说一下action跳转的方式~

一共有4种方式:内部跳转外部跳转全局跳转,默认的跳转页面

内部跳转

1.dispatcher(action-jsp)
 从一个action里面服务器内部跳转到一个页面中.这个是type属性的默认值.
写个例子~从JumpAction跳到register.jsp
JumpAction类:
public class JumpAction extends ActionSupport{

	private static final long serialVersionUID = 1L;
	
	public String toRegister(){
		return "register";
	}	

}
register.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>注册页面</title>
</head>
<body>
     <h3>注册页面</h3>
     <hr>
            用户名:<input type="text" name="name"/><br>
            密码:<input type="password" name="password"/><br>
            确认密码:<input type="password" name="repassword"/><br>
            性别:<input type="radio" name="gender" value="true" checked="checked">男
            <input type="radio" name="gender" value="false">女
            <br>
            年龄:<select name="age">
              <option value="">请选择</option>
				<%
					for(int i=10;i<=30;i++){
				%>
				<option value="<%=i %>"><%=i %>周岁</option>
				<%
					}
				%>
            </select>
            <br>
            <input type="submit" value="注册">
     </form>  
</body>
</html>
struts.xml要配置这个Action:
		   <action name="jump"
		    class="com.xu.struts2.web.action.JumpAction">
			<result name="register" type="dispatcher">/WEB-INF/jsp/register.jsp</result>
		   </action> 
在浏览器中输入localhost:9999/Struts2/jump!toRegister.action
效果图:

2.chain(action->action)
从一个action里面服务器内部跳转到另一个action中.
还是JumpAction类:
public class JumpAction extends ActionSupport{

	private static final long serialVersionUID = 1L;
	
	public String toA(){
		System.out.println("toA");
		return "a";
	}
	public String A(){
		System.out.println("A");
		return "b";
	}

}
可以看到有俩个方法~
struts.xml:
		    <action name="jump"
		    class="com.xu.struts2.web.action.JumpAction">
		    <result name="a" type="chain">
			    <param name="namespace">/</param>
			    <param name="actionName">jump</param>
			    <param name="method">A</param>
			  </result>
			  <result name="b" type="dispatcher">
			    /WEB-INF/jsp/index.jsp
			   </result>
		   </action> 


效果图:

外部跳转

1.redirect(action->jsp)
JumpAction类:
public class JumpAction extends ActionSupport{

	private static final long serialVersionUID = 1L;
	
	public String toIndex(){
		return "index";
	}

}

struts.xml:
		    <action name="jump"
		      class="com.xu.struts2.web.action.JumpAction">
			<result name="index" type="redirect">index.jsp</result>
			</action>
效果图:


2.redirectAction(action->action)
 从一个action里面客户端重定向到另一个action里面
这个也要分情况:同一个包下的跳转和不同包的跳转~
(1)同包跳转--同一个package下面的action跳转:
代码如下:
JumpAction:
public class JumpAction extends ActionSupport{

	private static final long serialVersionUID = 1L;
	
	public String toA(){
		System.out.println("toA");
		return "a";
	}
	public String A(){
		System.out.println("A");
		return "b";
	}

}
struts.xml:
		    <action name="jump"
		    class="com.xu.struts2.web.action.JumpAction">
			<result name="a" type="redirectAction">
			    <param name="namespace">/</param>
			    <param name="actionName">jump</param>
			    <param name="method">A</param>
			 </result>
			<result name="b" >
			    /WEB-INF/jsp/index.jsp
			 </result>
		   </action>  
效果图:


(2)不同的俩个package下面的action跳转:
在JumpAction中只需要一个方法~
public class JumpAction extends ActionSupport{

	private static final long serialVersionUID = 1L;
	
	public String toA(){
		System.out.println("toA");
		return "a";
	}

}
在struts.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>
    <constant name="struts.enable.DynamicMethodInvocation" value="true" />
    <constant name="struts.devMode" value="true" />
    <package name="default" namespace="/" extends="struts-default">
        
		    <action name="jump"
		    class="com.xu.struts2.web.action.JumpAction">
			 <result name="a" type="redirectAction">
			    <param name="namespace">/test</param>
			    <param name="actionName">user</param>
			    <param name="method">toIndex</param>
			 </result>
			<result name="index">
			    /WEB-INF/jsp/index.jsp
			 </result>
		   </action>  
    </package>
    <package name="test" namespace="/test" extends="struts-default">
        <action name="user"
		    class="com.xu.struts2.web.action.UserAction">
			<result name="index">/WEB-INF/jsp/index.jsp</result>
		</action>
    </package>
    <!-- <include file="example.xml"/> -->
</struts>


UserAction类中有那个方法:
	
	public String toIndex(){
		System.out.println("跳转到首页");
		return "index";
	}
效果图如下:


注意:不是只有redirectAction可以在不同包跳转,chain也可以在不同包跳转~这里就不写代码了~
3.全局跳转
在struts.xml文件中添加如下代码:
	<global-results>
		<result name="success">/success.jsp</result>
		<result name="error">/error.jsp</result>
	</global-results> 
作用:  将来在其他任何action中,如果有需要返回success字符串跳转到success.jsp或者返回error字符串跳转到error.jsp的时候,就不需要在单独定义,因为已经把这个俩个跳转定义成了全局的跳转,对当前package里面的所有action都起作用.同时还可以在某一个action中再重新定义一下这个俩个跳转,这个时候全局跳转就不会对这个action起作用了.
效果图就不贴了~
4.默认的跳转页面也就是默认的action
在struts.xml文件中添加如下代码:
 <default-action-ref name="test"></default-action-ref>
		<!-- test.action -->
		<action name="test"
		    class="com.xu.struts2.web.action.TestAction">
		    <result name="success">/WEB-INF/jsp/index.jsp</result>
		    <result name="login">/WEB-INF/jsp/login.jsp</result>
			<result name="register">/WEB-INF/jsp/register.jsp</result>
		</action>
在TestAction中:
	public String execute(){
		// TODO Auto-generated method stub
		return "register";
	}

作用:如果地址栏中访问到了当前package下面一个不存在的action的时候,正常情况下是会直接报错的,错误信息显示这个action找不到,但是我们一旦配置了这个默认的action之后,那么再去访问一个不存在的action就不会报错了,而是直接就去访问这个默认的action.这个默认的action需要我们在当前package里面定义出来,并且在<default-action-ref>标签中引用一下.
    注意:访问某个package下面action的时候有几种错误的情况:
       1.action找不到
       2.action找到了但是action对应的类找不到.
       3.action找到了,对应的类找到了,但是在类中要执行的方法没找到。
       4.action找到了,对应的类找到了,类中要执行的方法找到了,但是方法返回的字符串在<result>标签中没有定义.
       5.action找到了,对应的类找到了,类中要执行的方法找到了,方法返回的字符串在<result>标签也定义了出来,但是要跳转的页面没有找到.
效果图如下:



好了,写到这里就完成了~

有什么疑问的可以问我啊微笑

一起交流交流



  • 2
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值