如何使用同一个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>