如何使用同一个Action中的不同方法

1.使用Action的DMI(Dynamic Method Invocation——动态方法调用)

(1)动态方法调用:

  表单元素的action不是直接为某个Action的名字,而是为:action="Action名!Action中的方法名"的形式,把请求指定到处理方法中去。

(2)简单的使用动态方法调用:

  1)完成以下需求:

    ①提交同一个表单,有username、password两个请求参数;

    ②有“登陆”“注册”两个按钮;

    ③提交给同一个Action,使用不同的处理逻辑;

    ④“登陆”——登陆的处理逻辑,这里直接跳转到登陆页;

    ⑤“注册”——注册的处理逻辑,这里直接跳转到注册页。

  2)定义一个处理请求的Action类,此Action类有两个处理逻辑:

  

public class WelcomeAction extends ActionSupport{
    //两个常量用于区分使用不同的处理逻辑
    public static final String LOGIN = "login";
    public static final String REGIST = "regist";
    //封装两个请求参数
    private String username;
    private String password;
    //setter、getter方法
    public void setUsername(String username){
        this.username = username;
    }
    public String getUsername(){
        return this.username;
    }
    public void setPassword(String password){
        this.password = password;
    }
    public String getPassword(){
        return this.password;
    }
    //处理逻辑1:登陆逻辑
    public String execute()throws Exception{
        if(getUsername().equals("jiagoushi")
                &&getPassword().equals("jiagoushi")){
            return LOGIN;
        }else{
            return ERROR;
        }
    }
    //处理逻辑2:注册逻辑
    public String regist()throws Exception{
        return REGIST;
    }
}

  3)写一个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>
<script type="text/javascript">
    function regist() {
        //获取表单
        thisForm = document.forms[0];
        //动态修改action属性
        thisForm.action = "welcome!regist";
    }
</script>
<body>
<form action="welcome">
    用户名:<input type="text" name="username"><br/>
    密码:<input type="password" name="password"><br/>
    <input type="submit" value="登陆">
    <input type="submit" value="注册" onclick="regist()">
</form>
</body>
</html>

   4)配置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="false" />
    <constant name="struts.devMode" value="true" />
    <package name="package_a" extends="struts-default">
        <action name="welcome" class="myAction.WelcomeAction">
            <result name="login">login.jsp</result>
            <result name="error">error.jsp</result>
            <result name="regist">regist.jsp</result>
        </action>
    </package>
</struts>

2.指定<action>标签的method属性

(1)通过指定<action>标签的method属性,可以把一个Action定义成多个逻辑Action,如下所示:

<?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="false" />
    <constant name="struts.devMode" value="true" />
    <package name="package_a" extends="struts-default">
        <!-- 默认使用WelcomeAction类的execute()方法 -->
        <action name="welcome" class="myAction.WelcomeAction">
            <result name="login">login.jsp</result>
            <result name="error">error.jsp</result>
        </action>
        <!-- 指定使用WelcomeAction类的regist()方法 -->
        <action name="regist" class="myAction.WelcomeAction" method="regist">
            <result name="regist">regist.jsp</result>
        </action>
    </package>
</struts>

(2)修改之后,我们的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>
<script type="text/javascript">
    function regist() {
        //获取表单
        thisForm = document.forms[0];
        //动态修改action属性
        thisForm.action = "regist";
    }
</script>
<body>
<form action="welcome">
    用户名:<input type="text" name="username"><br/>
    密码:<input type="password" name="password"><br/>
    <input type="submit" value="登陆">
    <input type="submit" value="注册" onclick="regist()">
</form>
</body>
</html>

如何使用同一个Action中的不同方法的更多相关文章

  1. 11SpringMvc_一个Action中,写多个类似的业务控制方法

    我们要实现这么一个功能: 编写两个表单,提交到同一个Action中的不同的处理方法中.比如注册和登录,都提交到UserAction这个控制类中.但是这两个提交由userAction这个控制类不同的方法 ...

  2. 梳理:python—同一个类中的方法调用

    为什么突然在此提到这个梳理问题呢? 因为在自己实践综合练习学过的知识时,突然觉得有些知识点的运用总是不成功,于是翻过课本进行回顾,总是觉得是对的,可是当再进一步思考“既然是对的,为什么在程序中总是不成 ...

  3. struts2的占位符*在action中的配置方法

    转自:https://blog.csdn.net/u012546338/article/details/68946633 在配置<action> 时,可以在 name,class,meth ...

  4. Struts2学习(二)运行Action中方法的三种方式

    1.运行execute()方法 一般的能够直接在action中书写execute,调用action时会自己主动运行此方法 2.配置method方法 在struts.xml中配置action时.写met ...

  5. 《同一个类中不同方法之间的调用相关问题(省略的类名或者this)》

    //同一个类中不同方法之间的调用相关问题(省略的类名或者this) class A { public void B() { System.out.println("b方法运行"); ...

  6. Extjs中给同一个GridPanel中的事件添加参数的方法

    Extjs中给同一个GridPanel中的事件添加参数的方法: this.isUse = new Ext.Action({            text:'启用',            scope ...

  7. 如何将同一个APP中的不同activity在Recent(最近任务)中显示?

    需求描述 在应用Application1中存在A.B两个activity,当在应用启动了A.B activity,点击Recent键,如何让A.B两个activity都显示在Recent界面(最近任务 ...

  8. 10.Action中的method属性

    转自:https://wenku.baidu.com/view/84fa86ae360cba1aa911da02.html 在struts1.x中我们知道通过继承DispatchAction可以实现把 ...

  9. struts2.Action中的method属性配置

    .Action中的method属性 在struts1.x中我们知道通过继承DispatchAction可以实现把多个Action进行统一操作,在struts2中实现action的统一操作也很简单.我们 ...

随机推荐

  1. 安装Eclipse并配置JacORB插件

    前人成果 •      eclipse中开发corba完整说明(jacORB版) http://blog.csdn.net/hq0927/article/details/8129534 •      ...

  2. ios pyudaren

    ed2k://|file|%E9%A1%B9%E7%9B%AE%E6%8D%95%E9%B1%BC%E8%BE%BE%E4%BA%BA01.rmvb|67044010|9e013987298d7900 ...

  3. C#委托实例

    using System; using System.Collections.Generic; using System.Linq; using System.Text;   namespace we ...

  4. linux下创建的符号链接的权限

    今天为shell脚本创建符号链接的时候突然发现的, 创建的符号链接文件的默认权限是 777, 而想要这个脚本可执行, 必须为真正的文件加上 x 权限才可以.

  5. 有关OpenGL着色语言(一)

    刚接触OpenGL着色语言...,不定期增加内容 1.OpenGL着色语言(GLSL)是什么? 用于OpenGL的面向过程的高级着色语言,是近年来图形编程领域中出现的最重要的新型开发技术,使用Open ...

  6. Python标准库之核心模块学习记录

    内建函数和异常 包括__builtin__模块和exceptions模块 操作系统接口模块 包括提供文件和进程处理功能的os模块,提供平台独立的文件名处理(分拆目录名,文件名,后缀等)的os.path ...

  7. python随机数的使用记录

    一.random模块简介 Python标准库中的random函数,可以生成随机浮点数.整数.字符串,甚至帮助你随机选择列表序列中的一个元素,打乱一组数据等. 二.random模块重要函数 1 ).ra ...

  8. db.properties

    jdbc.driverclass=oracle.jdbc.driver.OracleDriverjdbc.url=jdbc:oracle:thin:@192.168.201.192:1521:orcl ...

  9. linux设备驱动归纳总结(四):3.抢占和上下文切换【转】

    本文转载自:http://blog.chinaunix.net/uid-25014876-id-65711.html linux设备驱动归纳总结(四):3.抢占和上下文切换 xxxxxxxxxxxxx ...

  10. 3.1将AngularJS放入上下文

    本章,作者将AngularJS放在全球web app开发的上下文里,并为后面的章节设置功能.AngularJS的目标,是带来一款工具,它有服务端开发web client的能力,并易于开发,测试,富.复 ...