在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>
密 码:<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>
分享到:
相关推荐
资源中示例了一个struts2的配置文件中配置的action的result结果为另外一个action的配置,该跳转的过程中是带所有参赛调整的,在另外一个action中可以接受到当前action的所有参数,此配置文件是我日常开发中的经验...
struts2一个action处理多个请求
struts2入门(传参) 简单的struts2登陆实例
从jsp页面提交form表单后,在struts.xml中寻找匹配的action。在action中处理完业务后返回,在struts.xml中的result属性中转向到另外一个action。
struts2中使用ognl表达式语言传参的方法,简化操作,通过jsp页面传递到Action中
利用Struts 2框架创建一个web项目chap2_e22,实现用户登录过程。具体要求是在loginAction类中分别用login()和registered()处理用户登录和注册的过程,分别创建login.jsp和register.jsp两个页面实现登录和注册的...
NULL 博文链接:https://zhouhaitao.iteye.com/blog/1095811
struts2利用通配符调用同一个Action里面不同的方法,在struts.xml配置文件中将请求方法的不相同部分抽象成“*".
Struts2 中 Action 接收参数的方法 Struts2 框架中,Action 组件可以通过多种方式接收参数,这些方式包括使用 Action 的属性、使用 DomainModel 和使用 ModelDriven。下面将详细介绍这些方法: 使用 Action 的属性...
struts2是否可以通过超链接 struts2传参struts2是否可以通过超链接 struts2传参struts2是否可以通过超链接 struts2传参
前几天在网上下载一个struts2的helloword的例子,那个作者也真够缺德的,搞个错误的程序,害得我查了一天的程序错误。 最后发现竟然是struts.xml被写成啦sturts.xml。 碰见这样的问题先鄙视下提供例子的作者, 再...
首先,Struts的ActionServlet接收到一个请求,然后根据struts-config.xml的配置定位到相应的mapping (映射);接下来如果form的范围是request或者在定义的范围中找不到这个form,创建一个新的form实例;取得form...
Struts 2实战 struts2 in Action
NULL 博文链接:https://cdxs2.iteye.com/blog/1925245
Struts的小列子三个ActionStruts的小列子三个ActionStruts的小列子三个Action
第一部分 Strut 2:一个全新的框架 第1章 Struts 2:现代Web框架 2 1.1 Web应用程序:快速学习 2 1.1.1 构建Web应用程序 2 1.1.2 基础技术简介 3 1.1.3 深入研究 6 1.2 Web应用程序框架 7 1.2.1 什么是框架 7 1.2.2 ...
struts2如何实现弹出action返回的错误信息
Struts2 in action 中文版
Struts 2 Action对象为每一个请求产生一个实例,因此没有线程安全问题。 3 Servlet依赖方面的对比:Struts 1 Action依赖于Servlet API,因为Struts 1 Action的execute方法中有HttpServletRequest和...
Struts2中struts_xml的Action配置详解