struts1——动态formbean

下面是请求的html代码:

  <body>
    
    <form action="${pageContext.request.contextPath }/register.do" method="post">
        用户名:<input type="text" name="username"><br/>
        密码:<input type="text" name="password"><br/>
        电子邮箱:<input type="text" name="email"><br/>
        <input type="submit" value="注册">
    </form>
    
  </body>
</html>

下面是struts-config.xml配置文件:

<?xml version="1.0" encoding="UTF-8" ?>

<!DOCTYPE struts-config PUBLIC
          "-//Apache Software Foundation//DTD Struts Configuration 1.3//EN"
          "http://struts.apache.org/dtds/struts-config_1_3.dtd">

<struts-config>

    <form-beans>
        <form-bean name="UserFormBean" type="org.apache.struts.action.DynaActionForm">
            <form-property name="username" type="java.lang.String"></form-property>
            <form-property name="password" type="java.lang.String"></form-property>
            <form-property name="email" type="java.lang.String"></form-property>
        </form-bean>
    </form-beans>


    <action-mappings>
    
        <action path="/register" 
                name="UserFormBean" 
                type="cn.itcast.web.action.RegiserAction"
                input="/register.jsp">
                
        </action>
    </action-mappings>

</struts-config>

下面是java代码:

public class RegiserAction extends Action {

    @Override
    public ActionForward execute(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
    
        DynaActionForm daf = (DynaActionForm) form;  //username  password
        
        String username = daf.getString("username");
        String password = daf.getString("password");
        String email = daf.getString("email");
        System.out.println(username);
        /*
            虽然动态的formBean没有username、password等这些属性,但是BeanUtils框架还是可以将动态Bean中的数据整到User中
            因为BeanUtils从动态Bean中的Map集合中通过username、password等这些属性得到对应的值,然后将这些值整到User中
        */
        
        User user = new User();
        BeanUtils.copyProperties(user, daf);  //可以的
        
        return super.execute(mapping, form, request, response);
    }

    
    
}


/*如果想要对表单数据进行校验,那么需要指定一个formBean,然后继承DynaActionForm*/

public class UserFormBean extends DynaActionForm {
/*formBean继承DynaActionForm了,就可以让struts自动生成formBean的属性:这些属性可以通过配置文件进行配置,
    <form-beans>
        <form-bean name="UserFormBean" type="cn.itcast.web.formbean.UserFormBean">
            <form-property name="username" type="java.lang.String"></form-property>
            <form-property name="password" type="java.lang.String"></form-property>
            <form-property name="email" type="java.lang.String"></form-property>
        </form-bean>
    </form-beans>
    
但是校验的方法还是要我们自己的写*/
    
    
    @Override
    public ActionErrors validate(ActionMapping mapping,
            HttpServletRequest request) {
        
        
        String username = this.getString("username");
        if(username==null || username.trim().equals("")){
            ActionErrors errros = new ActionErrors();
            errros.add("username",new ActionMessage("用户名不能为空",false));
            return errros;
        }
        
        return null;
    }

    
    
}

而对应校验的struts-config.xml如下:

<struts-config>

    <form-beans>
        <form-bean name="UserFormBean" type="cn.itcast.web.formbean.UserFormBean">
            <form-property name="username" type="java.lang.String"></form-property>
            <form-property name="password" type="java.lang.String"></form-property>
            <form-property name="email" type="java.lang.String"></form-property>
        </form-bean>
    </form-beans>


    <action-mappings>
    
        <action path="/register" 
                name="UserFormBean" 
                type="cn.itcast.web.action.RegiserAction"
                input="/register.jsp">
                
        </action>
    </action-mappings>

</struts-config>


你可能感兴趣的:(struts1——动态formbean)