读书人

struts1.2 LazyValidatorForm的用法

发布时间: 2012-09-10 22:20:12 作者: rapoo

struts1.2 LazyValidatorForm的用法,代替多余的烦人的actionform
struts 如何去掉那多余的 actionform
而使用LazyValidatorForm

struts-config.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts-config PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 1.2//EN" "http://struts.apache.org/dtds/struts-config_1_2.dtd">

<struts-config>
<data-sources />
<form-beans>
<form-bean name="lazyForm" type="org.apache.struts.validator.LazyValidatorForm">
<form-property name="c1" type="java.lang.String[]" />
<form-property name="user" type="test.User" />
</form-bean>

</form-beans>

<global-exceptions />
<global-forwards />
<action-mappings>
<action attribute="lazyForm" input="index.jsp" name="lazyForm" parameter="method" path="/userAction" scope="request" type="test.UserAction" validate="false">
<forward name="addUser" path="/index.jsp" />
</action>

</action-mappings>

<message-resources parameter="test.ApplicationResources" />
</struts-config>

JSP

<%@ page language="java" pageEncoding="GBK"%>
<%@ taglib uri="/tags/struts-bean" prefix="bean"%>
<%@ taglib uri="/tags/struts-html" prefix="html"%>
<%@ taglib uri="/tags/struts-logic" prefix="logic"%>
<html>
<head>
<title>JSP for lazyForm form</title>
</head>
<body>
<FORM name="form1" action="userAction.do" method="post" enctype="multipart/form-data">
用户名<INPUT type="text" name="user.name" value="testuser"><br>
年龄<INPUT type="text" name="user.id" value="1"><br>
密码<INPUT type="text" name="user.password" value="testpwd"><br>
文件<INPUT type="file" name="testfile" ><br>
选择1<INPUT type="checkbox" name="c1" value="1" checked >
选择2<INPUT type="checkbox" name="c1" value="2" checked ><br>
非用户信息<INPUT type="text" name="other" value="nonsense"><br>
<INPUT type="submit" name="method" value="addUser">
</FORM>
<logic:present name="lazyForm">
<bean:write name="lazyForm"/>
</logic:present>
</body>
</html>

Action
package test;

import javax.servlet.http.*;

import org.apache.commons.beanutils.*;
import org.apache.struts.action.*;
import org.apache.struts.actions.*;
import org.apache.struts.upload.*;

public class UserAction extends DispatchAction {

/**
* Method execute
*
* @param mapping
* @param form
* @param request
* @param response
* @return ActionForward
*/
public ActionForward addUser(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) {
DynaBean lazyForm = (DynaBean) form;
System.out.println(lazyForm.get("c1"));
System.out.println(((FormFile) lazyForm.get("testfile")).getFileSize());
System.out.println(lazyForm.get("user"));
return mapping.findForward("addUser");
}

}

读书人网 >软件架构设计

热点推荐