zoukankan      html  css  js  c++  java
  • Spring中的DataBinding(二)


    @Controller
    @RequestMapping(value = "/custom/register")
    public class RegistrationController {
       // Set the data binding per controller
        @InitBinder
        public void initBinder(WebDataBinder binder){
            binder.setDisallowedFields("id");
    
        // 此处指定在绑定的时候存在两个必须字段 binder.setRequiredFields("userName","password"); }
    // POST请求的时候将BindingResult的对象置于参数列表中。 BindingResult是<form: error> 这个tag的子类。 @RequestMapping(method = {RequestMethod.POST,RequestMethod.PUT}) public String handleRegistration(Account account, BindingResult result){
         // 校验过程中如果存在error返回到本页,而且本页中有一个DataModel为Result if(result.hasErrors()) return "custom/register"; accountService.SaveAccount(account); return "redirect:custom/account/"+account.getId(); }
    }

     

    JSP文件中,在相应的控件处添加Error 标签,这样如果BindingResult不为空。这些Error 标签就会将提示信息显示出来

    <%--
      Created by IntelliJ IDEA.
      User: Administrator
      Date: 2015/4/6
      Time: 22:16
      To change this template use File | Settings | File Templates.
    --%>
    <%@ page contentType="text/html;charset=UTF-8" language="java" %>
    <%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
    <%@taglib prefix="spring" uri="http://www.springframework.org/tags" %>
    <%@taglib prefix="form" uri="http://www.springframework.org/tags/form" %>
    <html>
    <head>
        <title>Registration Page</title>
    </head>
    <body>
    <form:form modelAttribute="account" method="post">
        <fieldset>
            <legend>
                Account Personal Information
            </legend>
            <table>
                <tr>
                    <td>FirstName</td>
                    <td>
                        <form:input path="firstName"></form:input>
                    </td>
    
                </tr>
                <tr>
                    <td>LastName</td>
                    <td>
                        <form:input path="lastName"></form:input>
                    </td>
                </tr>
    
            </table>
        </fieldset>
        <fieldset>
            <legend>Account information</legend>
    
            <table>
                <tr>
                    <td>User Name</td>
                    <td><form:input path="userName"></form:input></td>
                    <td><form:errors path="userName"></form:errors> </td>
                </tr>
                <tr>
                    <td><form:label path="password">Password</form:label></td>
                    <td><form:input path="password"></form:input></td>
                    <td><form:errors path="password"></form:errors> </td>
                </tr>
    
            </table>
        </fieldset>
    
        <form:button value="Submit">提交 </form:button>
    </form:form>
    </body>
    </html>
    

      

     

  • 相关阅读:
    GitHub上受欢迎的Android UI Library
    推荐的优秀博客--多关注,多看看
    Android SDK在线更新镜像服务器大全
    SqlServer查看各个表所占空间大小的sql
    演化理解 Android 异步加载图片
    tsp、rtmp测试地址
    eclipse离线安装Properties Editor插件查看配置文件中Unicode内容
    亲测成功关闭谷歌浏览器自动更新方法分享 取消chrome自动更新
    SQL Server 中用While循环替代游标(Cursor的解决方案
    【转】service和serviceImpl的选择
  • 原文地址:https://www.cnblogs.com/ygshen/p/4422777.html
Copyright © 2011-2022 走看看