Struts2中一个Action转到另一个Action以及传参的有关问题

Struts2中一个Action转到另一个Action以及传参的问题

在WEB的开过程中,我们在使用Struts2中Action完成业务逻辑时,可能存在这样一种情况,我们需要自当前的Aciton中方法执行后,在跳到另一个Action中并动态调用其中某个方法,这我们如何实现呢?我们来看下面这个例子。

如有这样一个要求,就是从首页(index.jsp)输入用户名( uname1)和密码(pwd1)后,在经过第一个Aciton完成处理后,在传到第二个Aciton继续处理,处理完成后再到指定的页面显示。

 

 

首先看我们的index.jsp页面:

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib uri="/struts-tags"  prefix="s"%>

<html>
  <head>
  </head>
  
  <body>
   <form action="acion1" method="post">
   		用户名:<input type="text" name="uname1"/><br>
   		密&nbsp;码:<input type="text" name="pwd1"/><br>   		
   		<input type="submit" value="提交" />
   </form>
  </body>
</html>

 

 

 

因为没有指定调用哪个方法,因此会执行Aciton1中的Execute()方法

 

Action1.java:

package com.lyl.struts2;

import com.opensymphony.xwork2.ActionSupport;

public class Action1  extends ActionSupport{

	
	private String method;
	
	private String uname1;
	private String pwd1;
	private String uname1Andpwd1;
	public String getUname1() {
		return uname1;
	}
	public void setUname1(String uname1) {
		this.uname1 = uname1;
	}
	public String getPwd1() {
		return pwd1;
	}
	public void setPwd1(String pwd1) {
		this.pwd1 = pwd1;
	}
	public String getUname1Andpwd1() {
		return uname1Andpwd1;
	}
	public void setUname1Andpwd1(String uname1Andpwd1) {
		this.uname1Andpwd1 = uname1Andpwd1;
	}
	public String getMethod() {
		return method;
	}
	public void setMethod(String method) {
		this.method = method;
	}
	
	
	@Override
	public String execute() throws Exception {
		
		System.out.println("--------------------------");
		
		 this.pwd1=this.getPwd1()+"----action1";
		 this.uname1=this.getUname1()+"----action1";
		 setUname1Andpwd1(this.uname1+this.pwd1);
		return SUCCESS;
	}

	
}

 

下面看下配置文件:

 

 

<?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>

	<!-- 
		include标签:导入其它的配置文件(代码重用)
	-->
	<include file="struts-default.xml" />

	<!-- 
		package:划分项目模块
		
		name:包名
		abstract:true抽象包(不能包action标签),
				 false非抽象包
		extends:指定父包
		namespace:命名空间(必须/开头)
		
		子控制器路径=命名空间+"/"+子控制器的名字+".action"
		子控制器路径=命名空间+"/"+子控制器的名字+"!方法名.action"//动态方法调用
	-->
	
	<package name="struts2" extends="struts-default">
		
		<!-- 
			action标签:定义子控制器
			name:子控制器名字
			class:子控制器完整类名
		-->
		<action name="acion1" class="com.lyl.struts2.Action1">
			<result name="success" type="redirectAction">
				<!-- 将重定向到actionName指向的action -->
				<param name="actionName">action2</param> <!-- 通过ationName指定要转向的Action -->
				<param name="method">myMethod</param><!-- 通过method指定Action2执行的方法 -->
				<param name="uname2">${uname1}</param><!-- 取得Action1中的的uname1赋值给Action2的uname2属性 -->
				<param name="pwd2">${pwd1}</param>
				<param name="uname2Andpwd2">${uname1Andpwd1}</param>
			</result>
		</action>
		
		<action name="action2" class="com.lyl.struts2.Action2">
			<result name="success">/actionResult.jsp</result>
		</action>
		
		
	</package>

</struts>

 

 

下面看Action2.java

 

package com.lyl.struts2;

import com.opensymphony.xwork2.ActionSupport;

public class Action2 extends ActionSupport {

	
	private static final long serialVersionUID = 1L;
	
	private String uname2;
	private String pwd2;
	private String uname2Andpwd2;
	
	//指定执行的方法
	private String method;
	
	public String getMethod() {
		return method;
	}
	public void setMethod(String method) {
		this.method = method;
	}
	public String getUname2() {
		return uname2;
	}
	public void setUname2(String uname2) {
		this.uname2 = uname2;
	}
	public String getPwd2() {
		return pwd2;
	}
	public void setPwd2(String pwd2) {
		this.pwd2 = pwd2;
	}
	
	
	@Override
	public String execute() throws Exception {
		return SUCCESS;
	}
	
	public String myMethod()
	{
		System.out.println("执行了myMethod的方法");
		
		this.uname2=this.uname2+"----action2";;
		this.pwd2=this.pwd2+"----action2";
		
		return SUCCESS;
	}
	public String getUname2Andpwd2() {
		return uname2Andpwd2;
	}
	public void setUname2Andpwd2(String uname2Andpwd2) {
		this.uname2Andpwd2 = uname2Andpwd2;
	}

	
}

 

 

 

通过配置文件中

 

<param name="method">myMethod</param><!-- 通过method指定Action2执行的方法 -->

 

指定在Action1的execute()方法后将会执行Action2的myMethod()方法,执行后转发到result.jsp页面

 

如下:

 

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
  </head>
  
  <body>
  <center>
	   	uname2:<s:property value="uname2"/><br>
	   	pwd2:<s:property value="pwd2"/> <br>
	   	uname2pwd2:<s:property value="uname2Andpwd2" />	

	 
   	</center>
  </body>
</html>