Spring中的DataBinding(二) - Validation

來源:互聯網
上載者:User

標籤:


@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>

  

 

Spring中的DataBinding(二) - Validation

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.