SpringMVC表單標籤(7)

1.12 textarea標籤

SpringMVC textarea標籤將被渲染爲普通HTML textarea標籤。簡單示例如下:

Jsp代碼 

  1. <form:form action="formTag/form.do" method="post" commandName="user">
  2. <table>
  3. <tr>
  4. <td>自我介紹:</td>
  5. <td>
  6. <form:textarea path="introduction" cols="20" rows="10"/>
  7. </td>
  8. </tr>
  9. <tr>
  10. <td colspan="2"><input type="submit" value="提交"/></td>
  11. </tr>
  12. </table>
  13. </form:form>

<form:form action="formTag/form.do" method="post" commandName="user">

    <table>

        <tr>

            <td>自我介紹:</td>

            <td>

               <form:textarea path="introduction" cols="20" rows="10"/>

            </td>

        </tr>

       <tr>

            <td colspan="2"><input type="submit" value="提交"/></td>

        </tr>

    </table>

</form:form>

1.13 errors標籤

SpringMVC errors標籤是對應於SpringMVCErrors對象的。它的作用就是用於展現Errors對象中包含的錯誤信息的。我們利用errors標籤來展現Errors的時候是通過errors標籤的path屬性來綁定一個錯誤信息的。我們可以通過path屬性來展現兩種類型的錯誤信息。

l 所有的錯誤信息,這個時候path的值應該置爲“*”

l 當前對象的某一個域的錯誤信息,這個時候path的值應爲所需展現的域的名稱

看下面這樣一個例子:

定義了一個UserValidator對象,專門用來對User對象進行驗證,其代碼如下:

Java代碼 

  1. import org.springframework.validation.Errors;
  2. import org.springframework.validation.ValidationUtils;
  3. import org.springframework.validation.Validator;
  4.  
  5. publicclass UserValidator implements Validator {
  6.  
  7. @Override
  8. publicboolean supports(Class<?> clazz) {
  9. // TODO Auto-generated method stub
  10. return User.class.equals(clazz);
  11. }
  12.  
  13. @Override
  14. publicvoid validate(Object target, Errors errors) {
  15. // TODO Auto-generated method stub
  16. ValidationUtils.rejectIfEmpty(errors, "name", null, "Name Is Empty");
  17. ValidationUtils.rejectIfEmpty(errors, "username", null, "Username Is Empty.");
  18. }
  19.  
  20. }

import org.springframework.validation.Errors;

import org.springframework.validation.ValidationUtils;

import org.springframework.validation.Validator;

 

public class UserValidator implements Validator {

 

    @Override

    public boolean supports(Class<?> clazz) {

       // TODO Auto-generated method stub

       return User.class.equals(clazz);

    }

 

    @Override

    public void validate(Object target, Errors errors) {

       // TODO Auto-generated method stub

       ValidationUtils.rejectIfEmpty(errors, "name", null, "Name Is Empty");

       ValidationUtils.rejectIfEmpty(errors, "username", null, "Username Is Empty.");

    }

 

}

然後我們有這樣一個控制器類:

Java代碼 

  1. @Controller
  2. @RequestMapping("formTag")
  3. publicclass FormTagController {
  4.  
  5. @RequestMapping(value="form", method=RequestMethod.GET)
  6. public String formTag(Map<String, Object> map) {
  7. User user = new User();
  8. map.put("user", user);
  9. return"formTag/form";
  10. }
  11.  
  12. @InitBinder
  13. publicvoid initBinder(DataBinder binder) {
  14. binder.setValidator(new UserValidator());
  15. }
  16.  
  17. @RequestMapping(value="form", method=RequestMethod.POST)
  18. public String form(@Valid User user, Errors errors) {
  19. if (errors.hasFieldErrors())
  20. return"formTag/form";
  21. return"formTag/submit";
  22. }
  23. }

@Controller

@RequestMapping("formTag")

public class FormTagController {

 

    @RequestMapping(value="form", method=RequestMethod.GET)

    public String formTag(Map<String, Object> map) {

       User user = new User();

       map.put("user", user);

       return "formTag/form";

    }

  

    @InitBinder

    public void initBinder(DataBinder binder) {

       binder.setValidator(new UserValidator());

    }

  

    @RequestMapping(value="form", method=RequestMethod.POST)

    public String form(@Valid User user, Errors errors) {

       if (errors.hasFieldErrors())

           return "formTag/form";

       return "formTag/submit";

    }

}

我們可以看到我們在上述控制器類中通過DataBinder對象給該類設定了一個用於驗證的UserValidator,這樣當我們請求該控制器的時候UserValidator將生效。

我們有如下這樣一段表單代碼:

Jsp代碼 

  1. <form:form action="formTag/form.do" method="post" commandName="user">
  2. <table border="1px" bordercolor="blue">
  3. <tr align="center">
  4. <td width="100">姓名:</td>
  5. <td width="150"><form:input path="name"/></td>
  6. </tr>
  7. <tr align="center">
  8. <td>用戶名:</td>
  9. <td><form:input path="username"/></td>
  10. </tr>
  11. <tr>
  12. <td>所有錯誤信息:</td>
  13. <td><form:errors path="*"/></td>
  14. </tr>
  15. <tr>
  16. <td>Name的錯誤信息:</td>
  17. <td><form:errors path="name"/></td>
  18. </tr>
  19. <tr align="center">
  20. <td colspan="2"><input type="submit" value="提交"/></td>
  21. </tr>
  22. </table>
  23. </form:form>

<form:form action="formTag/form.do" method="post" commandName="user">

    <table border="1px" bordercolor="blue">

        <tr align="center">

            <td width="100">姓名:</td>

            <td width="150"><form:input path="name"/></td>

        </tr>

        <tr align="center">

            <td>用戶名:</td>

            <td><form:input path="username"/></td>

        </tr>

        <tr>

            <td>所有錯誤信息:</td>

            <td><form:errors path="*"/></td>

        </tr>

        <tr>

            <td>Name的錯誤信息:</td>

            <td><form:errors path="name"/></td>

        </tr>

        <tr align="center">

            <td colspan="2"><input type="submit" value="提交"/></td>

        </tr>

    </table>

</form:form>

當我們提交上面的表單的時候會往Errors中注入兩個錯誤信息,展示的頁面信息將如下所示:


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章