zoukankan      html  css  js  c++  java
  • Struts 2读书笔记使用Action的动态方法调用

                 struts 2提供了包含处理多个逻辑的Action,从而允许一个Action内包含多个控制处理逻辑。例如一个页面中存在多个按钮,用户通过不同的按钮提交同一个表单时,可以使用Action的不同的方法来处理用户的请求。如下图页面

              

               页面中有两个提交按钮,但分别提交给Action的不同方法处理,“登陆”按钮使用登陆逻辑来处理请求,而“注册”按钮则使用注册逻辑来处理请求。

               此时,可以采用动态方法调用来处理 这种请求。动态方法调用是指表单元素的action并不是直接等于某个Action的名字,而是以下面形式来指定表单的action属性

               action属性为actionName!methodName形式   其中ActionName指定提交给那个Action,而methodName指定提交给该Action的某个方法
               即:action = ”ActionName!methodName"

              上面的注册按钮是一个没有任何动作的按钮,但单击该按钮时会触发regist函数        

         <input type="submit" value="注册" onclick="regist();">

               regist函数:       

         <script type="text/javascript">
        	function regist(){
        		targetForm = document.forms[0];
        		targetForm.action = "login!regist";
        	}
         </script>

             上面的regist方法改变表单的action属性,修改action的属性为“login!regist",其实质就是将该表单提交给LoginAction的regist方法处理

             Login|Action类的代码:

            

    public class LoginAction {
    	private String username;
    	private String password;
    	public String getUsername() {
    		return username;
    	}
    	public void setUsername(String username) {
    		this.username = username;
    	}
    	public String getPassword() {
    		return password;
    	}
    	public void setPassword(String password) {
    		this.password = password;                          
    	}
    	
    	//定义处理用户请求的login方法
    	public String execute(){
    		ActionContext ctx = ActionContext.getContext();
    		Integer counter = (Integer) ctx.getApplication().get("counter");
    		if(counter==null){
    			counter = 1;
    		}
    		else {
    			counter = counter + 1;
    		}
    		
    		ctx.getApplication().put("counter", counter);
    		//通过ActionContext()设置session范围的属性
    		ctx.getSession().put("user", getUsername());
    		
    		if(getUsername().equals("chentmt")&&getPassword().equals("chenssy")){
    			//通过ActionContext()设置reque范围的属性
    			ctx.put("tip","服务器提示:您已经成功登陆.....");
    			return "success";
    		}
    		else {
    			ctx.put("tip", "服务器提示:登陆失败");
    			return "error";
    		}
    	}
    	
    	//Action包含的注册控制逻辑
    	public String regist(){
    		ActionContext.getContext().getSession().put("user",getUsername());
    		setTip("恭喜你,"+getUsername()+",您已经注册成功了....");
    		return "success";
    	}
    
    }

              上面的Action代码中定义了改Action里包含的register控制逻辑,在默认情况下,用户请求不会提交该方法。当在单击“登陆”按钮时,系统将提交给LoginAction的默认方法处理。当单击“注册”按钮时,该表单的action被修改为login!regist,系统将提交改LoginAction的regist方法处理。

             

              通过这种方式,我们可以通过在一个Action中包含多个处理逻辑,并通过为该表单元素指定不同action属性来提交给Action的不同方法。

             对于使用动态方法调用的方法,该方法的方法声明与系统默认的execute方法的方法只有方法名不同,其他部分如形参列表,返回值类型都应该完全相同。

             在试用合格动态方法调用前必须设置Struts 2允许动态方法调用。开启系统的动态方法调用时通过设置struts.enable.DynamicMethodInvocation常量完成的。设置该常量为true,将开启动态方法调用;否则则关闭动态方法调用。
     

             读李刚《轻量级java EE企业应用实战(第三版)—struts 2+Spring 3+Hibernate整合开发》

  • 相关阅读:
    jquery获取浏览器URL参数
    jquery获取、设置、删除cookie
    jquery获取当前按钮、截取字符串、字符串拼接、动态循环添加元素
    Callback函数
    window.opener和window.open的使用
    JavaScript内置对象
    SeaJS:一个适用于 Web 浏览器端的模块加载器
    hihocoder 1388 &&2016 ACM/ICPC Asia Regional Beijing Online Periodic Signal
    Codeforces Round #373 (Div. 2)
    fzu 1759Super A^B mod C 指数循环节
  • 原文地址:https://www.cnblogs.com/oversea201405/p/3752300.html
Copyright © 2011-2022 走看看