日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 前端技术 > javascript >内容正文

javascript

使用Spring Form标签探索Spring Controller

發布時間:2023/12/3 javascript 23 豆豆
生活随笔 收集整理的這篇文章主要介紹了 使用Spring Form标签探索Spring Controller 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

在上一篇文章中 ,我向您展示了如何使用Spring控制器處理純HTML表單。 但是處理表單的更強大的方法是使用Spring的@ModelAttribute及其spring:form標簽。 我將向您展示如何通過修改上一篇文章的項目設置從這里開始。 我們將簡單地修改Comment表單和控制器以使用此功能。

在同一項目中,將src/webapp/comment.jsp視圖文件修改為:

<%@ taglib prefix="spring" uri="http://www.springframework.org/tags/form" %> <spring:form modelAttribute="comment"><table><tr><td><spring:textarea path="text" rows="20" cols="80"/></td></tr><tr><td colspan="2"><input type="submit" value="Post"/></td></tr></table> </spring:form>

現在,該視圖使用spring:form標記而不是純HTML來呈現注釋表單。 我在這里僅向您顯示了一個元素,但是spring:form標記庫還附帶了所有匹配HTML表單元素,可幫助您快速綁定數據并呈現表單。 提交時,這將自動觸發CommentController 。 我們將需要對其進行修改以捕獲表單。

package springweb.controller;import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.validation.BindingResult; import org.springframework.web.bind.annotation.ModelAttribute; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.servlet.ModelAndView; import springweb.data.Comment; import springweb.data.CommentService;import javax.servlet.http.HttpServletRequest; import java.util.List;@Controller public class CommentController {@Autowiredprivate CommentService commentService;@RequestMapping(value="/comments")public ModelAndView comments() {List<Comment> comments = commentService.findComments();ModelAndView result = new ModelAndView("/comments");result.addObject("comments", comments);return result;}@ModelAttribute("comment")public Comment createFormModelAttribute() {return Comment.create("");}@RequestMapping(value="/comment")public String comment() {return "comment";}@RequestMapping(value="/comment", method = RequestMethod.POST)public ModelAndView postComment(HttpServletRequest req,@ModelAttribute("comment") Comment comment) {String fromUrl = req.getRequestURI();String user = req.getRemoteUser();String userIp = req.getRemoteAddr();comment.setFromUserIp(userIp);comment.setFromUser(user);comment.setFromUrl(fromUrl);commentService.insert(comment);ModelAndView result = new ModelAndView("comment-posted");result.addObject("comment", comment);return result;} }

與舊控制器相比,該控制器的不同之處在于我們將@ModelAttribute與一個form對象一起使用(或Spring稱為command對象)。我們可以為其命名,在這里我將其稱為comment 。 它只是一個Java POJO類,沒什么特別的。 但是它用于捕獲所有表單輸入,然后傳遞給Controller,這稱為數據綁定。 請注意,當您首先請求表單視圖時,它將通過createFormModelAttribute()方法進行實例化。 如果您用文本預先填充了pojo,它將自動以表格形式顯示! 當用戶提交時,控制器將使用postComment()方法進行處理,并且再次使用新的表單輸入來填充表單對象以進行處理。 這使您可以使用純對象樣式的表單,并且在許多方面,與純HTML表單相比,它更短,更簡潔。

Spring MVC表單處理有很多。 一種強大的功能是它可以幫助您組織form對象驗證并收集錯誤消息。 Spring還可以幫助您本地化錯誤消息文本等。您可以閱讀有關其參考文檔的更多信息。

參考: A Programmer's Journal博客上的JCG合作伙伴 Zemian Deng 探索了帶有Spring Form Tag的Spring Controller 。

翻譯自: https://www.javacodegeeks.com/2013/11/exploring-spring-controller-with-spring-form-tag.html

總結

以上是生活随笔為你收集整理的使用Spring Form标签探索Spring Controller的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。