Java EE - Spring MVC 数据绑定实例
来源:互联网 发布:app gps数据 运营商 编辑:程序博客网 时间:2024/06/05 05:01
问题导读:
1. 如何使用jstl标签?
2. Spring MVC表单标签库是什么?
3. Jsp 页面注释应注意什么?
4. Sping MVC 实例 实现
解决方案:
Jsp 标准标签库(JSTL)
配置
下载地址:
- 官方下载
- 云盘分享(提取码:77b4)
Jar:
下载并解压,将jakarta-taglibs-standard-1.1.2/lib/下的两个jar文件:standard.jar和jstl.jar文件拷贝到/WEB-INF/lib/下。
使用核心标签库
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
注:菜鸟教程JSTL文档
Spring MVC 表单标签库
概念
从2.0版本开始,如果使用JSP和Spring Web MVC的话,Spring提供了一套支持数据绑定的标签集合,用于处理表单元素。 每个标签所支持的属性集合和与其对应的HTML标签
相同,这就让这些标签看起来很熟悉,而且用起来很直观。 由这些标签库生成的HTML页面符合HTML 4.01/XHTML 1.0标准。与其它表单/输入标签库不同,Spring的表单标签库与Spring Web MVC集成在一起, 使标签可以使用命令对象(command object)和你的控制器处理的参考数据(reference data)。 就像下面这些例子展示的一样,表单标签让JSP更易于开发、阅读和维护。让我们进入表单标签的领域,并通过一个例子研究每个标签如何使用。 当某些标签需要更进一步的解释时,我们已经把所生成的HTML片段也一起列了进来。
配置
<%@ taglib uri="http://www.springframework.org/tags/form" prefix="form" %>
理解
“form”标签,为内部标签的绑定暴露了一个绑定路径(binding path)。 它把命令对象(command object)放在PageContext中,这样内部的标签就可以访问这个命令对象了。 这个库中的其他标签都是form标签的嵌套标签。
JSP注释
示例: <%-- 这是JSP注释 --%>
处理:翻译时忽略
Java注释
分类: JSP脚本元素,Java Scriptlet
示例:<% /* 这是Java注释 */ %>
处理:翻译时包含,编译时忽略,不包含在响应中
HTML注释
分类:静态HTML内容
示例:<!-- 这是html注释 -->
处理:包含在响应中
Demo 实现
参考:Spring MVC 开发入门
controller
package controller;import java.util.List;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.stereotype.Controller;import org.springframework.ui.Model;import org.springframework.web.bind.annotation.ModelAttribute;import org.springframework.web.bind.annotation.PathVariable;import org.springframework.web.bind.annotation.RequestMapping;import bean.Book;import bean.Category;import service.BookService;@Controllerpublic class BookController {/* * 使用@Autowired 和 @Service 对service进行依赖注入 */@Autowiredprivate BookService bService;@RequestMapping(value = "/book_input")public String inputBook(Model model) {List<Category> categories = bService.getAllCategories();/* * 控制器 创建 book,传入到view输入,在回到controller保存 */model.addAttribute("categories", categories);model.addAttribute("book", new Book());return "BookAddForm";}/* * 路径变量: * 1. 路径变量类似请求参数:/product_view/productID * 2.1 使用时,首先在RequestMapping 中定义一个路径变量 * 2.2 然后,在方法参数列表中添加 @PathVariable 注解标识的变量 */@RequestMapping(value = "/book_edit/{id}")public String editBook(Model model, @PathVariable long id) {List<Category> categories = bService.getAllCategories();model.addAttribute("categories", categories);Book book = bService.getBookById(id);model.addAttribute("book",book);return "BookEditForm";}@RequestMapping(value = "/book_save")/* * @ModelAttribute * 1. 该注解会将输入/创建的参数对象添加到Model 对象中 * 2. 标注非请求的处理方法 * 2.1 其他请求处理方法每次被调用之前,这个方法会被调用 * 2.2 这个被标记的方法的返回值是void/Object(对象会被直接加入Model中) */public String saveBook(@ModelAttribute Book book) {Category category = bService.getCategory(book.getCategory().getId());book.setCategory(category);bService.save(book);/* * 重定向 */return "redirect:/book_list";}@RequestMapping(value = "/book_update")public String updateBook(@ModelAttribute Book book) {Category category = bService.getCategory(book.getCategory().getId());book.setCategory(category);bService.update(book);return "redirect:/book_list";}@RequestMapping(value = "/book_list") public String listBooks(Model model) {List<Book> books = bService.getAllBooks();model.addAttribute("books", books);return "BookList";}}
jsp
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %><%@ taglib uri="http://www.springframework.org/tags/form" prefix="form" %><%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%><!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><html><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8"><title>添加书籍</title></head><body><div><!-- spring mvc 表单标签库 --><!-- form标签 1. commandName 为表单对应bean的名称 2. form中的标签一定要先绑定属性 3. 根据具体情况添加属性--><form:form commandName="book" action="/SpringMVCDemo2/book_save" method="post"><fieldset><legend>添加书籍</legend><p><label>书籍种类:</label><form:select id="category" path="category.id" items="${categories }" itemLabel="name" itemValue="id"/></p><p><tabel>书名:</tabel><form:input path="title" id="title"/></p><p><label>作者:</label><form:input path="author" id="author"/></p><p><label>ISBN:</label><form:input path="isbn" id="isbn"/></p><p><input id="reset" type="reset" tabindex="4"><input id="submit" type="submit" taindex="5" value="添加"></p></fieldset></form:form></div></body></html>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %> <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <html> <head> <title>书籍列表</title> </head> <body> <div> <h1>书单</h1> <%-- <c:url>标签将URL格式化为一个字符串,然后存储在一个变量中 --%> <a href="<c:url value='book_input'/>">添加</a> <table> <tr> <th>书单目录</th> <th>书名</th> <th>ISBN</th> <th>作者</th> <th> </th> </tr> <%-- <c:forEach>标签迭代一个集合中的对象。--%> <c:forEach items="${books}" var="book" > <tr> <td>${book.category.name}</td> <td>${book.title}</td> <td>${book.isbn}</td> <td>${book.author}</td> <td><a href="book_edit/${book.id}">编辑</a></td> </tr> </c:forEach> </table> </div> </body> </html>
%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %> <%@ taglib uri="http://www.springframework.org/tags/form" prefix="form" %> <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>编辑书籍</title> </head> <body> <div> <form:form commandName="book" action="/SpringMVCDemo2/book_update" method="post"> <fieldset> <legend>编辑书籍</legend> <!-- 隐藏域 隐藏 id --> <form:hidden path="id"/> <p> <label>书籍种类:</label> <form:select id="category" path="category.id" items="${categories}" itemLabel="name" itemValue="id"/> </p> <p> <tabel>书名:</tabel> <form:input path="title" id="title"/> </p> <p> <label>作者:</label> <form:input path="author" id="author"/> </p> <p> <label>ISBN:</label> <form:input path="isbn" id="isbn"/> </p> <p> <input id="reset" type="reset" tabindex="4"> <input id="submit" type="submit" taindex="5" value="修改"> </p> </fieldset> </form:form> </div> </body> </html>
测试
http://localhost:8080/SpringMVCDemo2/book_list
添加
编辑
0 0
- Java EE - Spring MVC 数据绑定实例
- spring mvc数据绑定
- spring mvc数据绑定
- spring mvc数据绑定
- Spring MVC 数据绑定
- Spring MVC-数据绑定
- Spring MVC数据绑定大全
- Spring MVC数据绑定大全
- Spring MVC数据绑定大全
- Spring MVC数据绑定大全
- Spring MVC数据绑定大全
- Spring MVC数据绑定大全 .
- Spring MVC数据绑定大全 .
- Spring MVC数据绑定大全
- Spring MVC数据绑定大全
- Spring MVC数据绑定大全
- Spring MVC表单数据绑定
- Spring MVC 复杂数据绑定
- 关于父类子类继承关系
- the best path(欧拉回路,欧拉路径)
- 快速排序
- 关于父类子类继承关系
- Python安装
- Java EE - Spring MVC 数据绑定实例
- poj1094 Sorting it all out(拓扑排序)
- 160923基础小结
- 07:可变长数组、多维数组
- NSDate分类-日期格式化以及日期常用判断
- 求字符串中连续数字的最长字符串
- [翻译]Why you don’t want to shard
- 设计模式_工厂方法模式(Java)
- 第一次查阅国外网站