Struts1.2懒人就用LazyValidatorForm

来源:互联网 发布:电话数据采集器 编辑:程序博客网 时间:2024/05/19 12:13

懒的境界在于操作懒而脑袋不能懒
想偷懒就要多看多想
懒得废话,看配置和程序吧


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");
 }

}

 
原创粉丝点击