Springmvc數(shù)據(jù)回顯實現(xiàn)原理實例解析
數(shù)據(jù)回顯就是當用戶數(shù)據(jù)提交失敗時,自動填充好已經(jīng)輸入的數(shù)據(jù),一般來說,如果是使用Ajax來做數(shù)據(jù)提交,基本上是沒有數(shù)據(jù)回顯這個需求的,但是如果通過表單做數(shù)據(jù)提交,那么數(shù)據(jù)回顯就非常必要了。
簡單數(shù)據(jù)類型數(shù)據(jù)回顯
簡單數(shù)據(jù)類型,實際上框架在這里沒有提供任何形式的支持,就是我們自己手動配置。加入提交的 Student 數(shù)據(jù)不符合要求,那么重新回到添加 Student 頁面,并且預設之前已經(jīng)填好的數(shù)據(jù)。
首先我們先來改造一下 student.jsp 頁面:
<form action="/addstudent" method="post"> <table> <tr> <td>學生編號:</td> <td><input type="text" name="id" value="${id}"></td> </tr> <tr> <td>學生姓名:</td> <td><input type="text" name="name" value="${name}"></td> </tr> <tr> <td>學生郵箱:</td> <td><input type="text" name="email" value="${email}"></td> </tr> <tr> <td>學生年齡:</td> <td><input type="text" name="age" value="${age}"></td> </tr> <tr> <td colspan="2"> <input type="submit" value="提交"> </td> </tr> </table> </form>
在接收數(shù)據(jù)時,使用簡單數(shù)據(jù)類型去接收:
@RequestMapping("/addstudent") public String addStudent2(Integer id, String name, String email, Integer age, Model model) { model.addAttribute("id", id); model.addAttribute("name", name); model.addAttribute("email", email); model.addAttribute("age", age); return "student"; }
這種方式,相當于框架沒有做任何工作,就是我們手動做數(shù)據(jù)回顯的。此時訪問頁面,服務端會再次定位到該頁面,而且數(shù)據(jù)已經(jīng)預填好。
實體類數(shù)據(jù)回顯
簡單數(shù)據(jù)類型的回顯,實際上非常麻煩,因為需要開發(fā)者在服務端一個一個手動設置。如果使用對象的話,就沒有這么麻煩了,因為 SpringMVC 在頁面跳轉(zhuǎn)時,會自動將對象填充進返回的數(shù)據(jù)中。
<form action="/addstudent" method="post"> <table> <tr> <td>學生編號:</td> <td><input type="text" name="id" value="${student.id}"></td> </tr> <tr> <td>學生姓名:</td> <td><input type="text" name="name" value="${student.name}"></td> </tr> <tr> <td>學生郵箱:</td> <td><input type="text" name="email" value="${student.email}"></td> </tr> <tr> <td>學生年齡:</td> <td><input type="text" name="age" value="${student.age}"></td> </tr> <tr> <td colspan="2"> <input type="submit" value="提交"> </td> </tr> </table> </form>
注意,在預填數(shù)據(jù)中,多了一個 student. 前綴。這 student 就是服務端接收數(shù)據(jù)的變量名,服務端的變量名和這里的 student 要保持一直。服務端定義如下:
@RequestMapping("/addstudent") public String addStudent(@Validated(ValidationGroup2.class) Student student, BindingResult result) { if (result != null) { //校驗未通過,獲取所有的異常信息并展示出來 List<ObjectError> allErrors = result.getAllErrors(); for (ObjectError allError : allErrors) { System.out.println(allError.getObjectName()+":"+allError.getDefaultMessage()); } return "student"; } return "hello"; }
注意,服務端什么都不用做,就說要返回的頁面就行了,student 這個變量會被自動填充到返回的 Model
中。變量名就是填充時候的 key。如果想自定義這個 key,可以在參數(shù)中寫出來 Model,然后手動加入 Student 對象,就像簡單數(shù)據(jù)類型回顯那樣。
另一種定義回顯變量別名的方式,就是使用 @ModelAttribute 注解。
ModelAttribute
@ModelAttribute 這個注解,主要有兩方面的功能:
- 在數(shù)據(jù)回顯時,給變量定義別名
- 定義全局數(shù)據(jù)
定義別名
在數(shù)據(jù)回顯時,給變量定義別名,非常容易,直接加這個注解即可
@RequestMapping("/addstudent") public String addStudent(@ModelAttribute("s") @Validated(ValidationGroup2.class) Student student, BindingResult result) { if (result != null) { //校驗未通過,獲取所有的異常信息并展示出來 List<ObjectError> allErrors = result.getAllErrors(); for (ObjectError allError : allErrors) { System.out.println(allError.getObjectName()+":"+allError.getDefaultMessage()); } return "student"; } return "hello"; }
這樣定義完成后,在前端再次訪問回顯的變量時,變量名稱就不是 student 了,而是 s:
<form action="/addstudent" method="post"> <table> <tr> <td>學生編號:</td> <td><input type="text" name="id" value="${s.id}"></td> </tr> <tr> <td>學生姓名:</td> <td><input type="text" name="name" value="${s.name}"></td> </tr> <tr> <td>學生郵箱:</td> <td><input type="text" name="email" value="${s.email}"></td> </tr> <tr> <td>學生年齡:</td> <td><input type="text" name="age" value="${s.age}"></td> </tr> <tr> <td colspan="2"> <input type="submit" value="提交"> </td> </tr> </table> </form>
定義全局數(shù)據(jù)
假設有一個 Controller 中有很多方法,每個方法都會返回數(shù)據(jù)給前端,但是每個方法返回給前端的數(shù)據(jù)又不太一樣,雖然不太一樣,但是沒有方法的返回值又有一些公共的部分??梢詫⑦@些公共的部分提取出來單獨封裝成一個方法,用 @ModelAttribute 注解來標記。
例如在一個 Controller 中 ,添加如下代碼:
@ModelAttribute("info") public Map<String,Object> info() { Map<String, Object> map = new HashMap<>(); map.put("username", "javaboy"); map.put("address", "www.javaboy.org"); return map; }
當用戶訪問當前 Controller 中的任意一個方法,在返回數(shù)據(jù)時,都會將添加了 @ModelAttribute 注解的方法的返回值,一起返回給前端。@ModelAttribute 注解中的 info 表示返回數(shù)據(jù)的 key。
以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Spring Cloud Stream微服務消息框架原理及實例解析
這篇文章主要介紹了Spring Cloud Stream微服務消息框架原理及實例解析,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下2020-06-06springboot對數(shù)據(jù)庫密碼加密的實現(xiàn)
這篇文章主要介紹了springboot對數(shù)據(jù)庫密碼加密的實現(xiàn),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2020-12-12詳解JDK 5 Annotation 注解之@Target的用法介紹
這篇文章主要介紹了詳解JDK 5 Annotation 注解之@Target的用法介紹,需要的朋友可以參考下2016-02-02Spring ApplicationContext上下文核心容器深入探究
ApplicationContext是Spring應用程序中的中央接口,由于繼承了多個組件,使得ApplicationContext擁有了許多Spring的核心功能,如獲取bean組件,注冊監(jiān)聽事件,加載資源文件等2023-01-01