LookUpDispatchAction详解

转自:http://hi.baidu.com/liuzhq/blog/item/d00831301130bf98a9018e23.html
例如我们做数据库的增加,修改时,如果把两个功能用两个Action来操作,这也是可行的,但两个功能很显然有相同的地方,例如都需要相同的数据验证.为了减少数据冗余度,增加可维护性.我们用LookUpDispatchAction类来实现这两种功能.
好了我们来具体的分析一下吧.
第一步:创建一个项目一个jsp页面(MyJsp.jsp
    <html:form action="login" method="post" >
        <bean:message key="label.username"/>
          <html:text property="userName" />
           <bean:message key="label.passWord"/>
          <html:password property="password" />
           <html:submit property="action">
<bean:message key="button.add"/>
</html:submit>
          <html:submit property="action">
<bean:message key="button.update"/>
</html:submit>
<html:errors><!—用于显示验证时出现的错误-->
     </html:form>
第二步:applicationResources.properties资源文件代码如下
label.username=usename
label.password=password
button.add=add
button.update=update
error.required={0} is required
error.username.length =Username must longer than 3
error.password.length =Password must longer than 3
第三步:创建一个ActionForm用于封装表单这里我以LoginForm命名代码如下:
package supermarket.dept.form;
import javax.servlet.http.HttpServletRequest;
import org.apache.struts.action.ActionError;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessage;
public class LoginForm extends ActionForm {
private String userName;
private String passWord;
          public String getUserName() {
          return username;
}
public void setUserName(String username) {
          this.userName = userName;
}
          /**
           * Method validate用于验证表单
           * @param mapping
           * @param request
           * @return ActionErrors
           */
          public ActionErrors validate(
                    ActionMapping mapping,
                    HttpServletRequest request) {
                     ActionErrors errors = new ActionErrors();
                        if(this.username==null || this.username.length()<1){
                        ActionMessage error = new ActionMessage("error.required","User Name");
                          errors.add("user",error);
                        
                        }
                        if(this.username.length()<3){
                            errors.add("userLength",new ActionMessage("error.username.length"));
                          }
                        if(this. passWrod ==null || this. passWrod.length()<1){
                        ActionMessage error = new ActionMessage("error.required","User Name");
                          errors.add("passWrod ",error);
                        
                        }
                        if(this. passWrod.length()<3){
                            errors.add("userLength",new ActionMessage("error. passWrod.length"));
                          }
                        return errors;
          }
          /**
           * Method reset
           * @param mapping
           * @param request
           */
          public void reset(ActionMapping mapping, HttpServletRequest request) {
                    // TODO Auto-generated method stub
          }
          public String getPassWord() {
                    return password;
          }
          public void setPassWord(String password) {
                    this.password = passWord;
          }
}
第四步:创建Action这里的Action继承了LookUpDispatchAction我以LoginAction命名.代码如下:
package com.lyx.struts.action;
import java.util.HashMap;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.LookupDispatchAction;
public class LoginAction extends LookupDispatchAction {
          public ActionForward add(
                             ActionMapping mapping,
                             ActionForm form,
                             HttpServletRequest request,
                             HttpServletResponse response) {
System.out.print("add");
                             // TODO Auto-generated method stub
                             return null;
                    }
          public ActionForward update(
                             ActionMapping mapping,
                             ActionForm form,
                             HttpServletRequest request,
                             HttpServletResponse response) {
System.out.println("update");
                             // TODO Auto-generated method stub
                             return null;
                    }
          protected Map getKeyMethodMap() {
                    System.out.print("hello map");
                    Map map=new HashMap();
                    map.put("button.add","add");
                    map.put("button.update"," update");
                    // TODO 自动生成方法存根
                    return map;
          }
}
注意:这里要把execute方法删掉.要不然不起作用.
   getKeyMethodMap()是一个抽象方法,必须得实现.这里用于映射.
第五步:给出Strut-config.xml配置文件的信息.
<?xml version="1.0" encoding="UTF-8"?>
<struts-config>
<data-sources />
<form-beans >
<form-bean name="loginForm" type="com.lyx.struts.form.LoginForm"/>
</form-beans>
<global-exceptions />
<global-forwards />
<action-mappings >
    <action
      input="/MyJsp.jsp"
      name="loginForm"
     parameter="action"
      path="/login"
      type="com.lyx.struts.action.LoginAction" />
</action-mappings>
<message-resources parameter="com.lyx.struts.ApplicationResources" />
</struts-config>
注意:这里Action里的Parameter属性的值是表单提交按钮属性的值.
第六步:Ok了.测试一下吧
Add在控制台上输出Add
update在控制台上输出update 
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值