struts1.1中的Lazy ActionForm

以前我转载过一篇“介绍struts的新特性:Lazy ActionForm“
http://www.blogjava.net/terry-zj/archive/2005/12/09/23090.html
这篇文章里面介绍的方法大大较少了ActionForm中的编码工作,但是这个Lazy ActionForm在struts1.1的时候还没有。如果既要使用struts1.1又想Lazy一下那?(实际上struts1.1还是目前使用比较多的版本)
其实少量的代码就可以让普通的Form接近Lazy ActionForm的效果。
 jsp中写:
        <table align="center" border="0" width="300">
                <tr>
                    <td align="right" width="35%">
                        用户ID:
                    </td>
                    <td width="65%">
                        <html:text property="data(username)" size="20" maxlength="20" />
                    </td>
                </tr>
                <tr>
                    <td align="right">
                        密码:
                    </td>
                    <td>
                        <html:text property="data(password)" size="20" maxlength="10" />
                    </td>
                </tr>
                <tr>
                    <td>
                        &nbsp;&nbsp;&nbsp;
                    </td>
                    <td>
                        <html:button property="login" value="确认" onclick="submitForm();" />
                        &nbsp;&nbsp;&nbsp;
                        <html:reset value="清空" />
                    </td>
                </tr>
            </table>

LoginAction中写:
public ActionForward doAction(ActionMapping mapping,
                                  RequestContext req) {
        String forward = "menu";
        BaseForm frm = (BaseForm)req.getForm();
        System.out.println(frm.get("username"));
        System.out.println(frm.get("password"));
        // ......        
        return mapping.findForward(forward);
}

BaseForm中写:
public class BaseForm extends ActionForm {
    private HashMap data = new HashMap();

    public HashMap getData() {
         return data;
    }

    public void setData(HashMap data) {
        this.data = data;
    }

    public String get(String key) {
        return (String) data.get(key);
    }

    public void set(String key, Object value) {
        data.put(key, value);
    }
}

struts-config.xml中写:
 <form-beans>
    <form-bean       name="baseform"     type="com.web.system.base.BaseForm"/>
  </form-beans>

你可能感兴趣的:(struts1.1中的Lazy ActionForm)