posts - 56, comments - 54, trackbacks - 0, articles - 4
   ::  ::  :: 联系 :: 聚合  :: 管理

struts1.1中的Lazy ActionForm

Posted on 2006-03-01 17:27 Terry的Blog 阅读(965) 评论(0)  编辑  收藏 所属分类: java语言web开发

以前我转载过一篇“介绍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>


只有注册用户登录后才能发表评论。


网站导航: