springmvc表单标签库的使用
springmvc中可以使用表单标签库,支持数据绑定,用来将用户输入绑定到领域模型。
例子来源《Servlet、JSP和SpringMVC学习指南》
项目代码
关键代码及说明
bean对象类代码
public class Book implements Serializable {
private static final long serialVersionUID = 1520961851058396786L;
private long id;
private String isbn;
private String title;
private Category category;
private String author;
public Book() {
}
public Book(long id, String isbn, String title, Category category, String author) {
this.id = id;
this.isbn = isbn;
this.title = title;
this.category = category;
this.author = author;
}
// get and set methods not shown
}
public class Category implements Serializable {
private static final long serialVersionUID = 5658716793957904104L;
private int id;
private String name;
public Category() {
}
public Category(int id, String name) {
this.id = id;
this.name = name;
}
// get and set methods not shown
}
Controller中的代码,需要在Model中添加绑定的对象
@RequestMapping(value = "/book_input")
public String inputBook(Model model) {
List<Category> categories = bookService.getAllCategories();
model.addAttribute("categories", categories);
model.addAttribute("book", new Book());
return "BookAddForm";
}
@RequestMapping(value = "/book_save")
public String saveBook(@ModelAttribute Book book) {
Category category =
bookService.getCategory(book.getCategory().getId());
book.setCategory(category);
bookService.save(book);
return "redirect:/book_list";
}
jsp中代码,使用表单标签
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib uri="http://java.sun.com/jstl/core_rt" prefix="c" %>
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %>
<html>
<head>
<title>Add Book Form</title>
</head>
<body>
<div id="global">
<!--commandName定义了模型属性的名称,其中包含了一个backing object,其属性将用于填充所生成的表单。如果该属性存在,则必须在返回包含该表单的视图的请求处理方法中添加相应的模型属性-->
<form:form commandName="book" action="book_save" method="post">
<fieldset>
<legend>Add a book</legend>
<p>
<label for="category">Category: </label>
<!--标签也可以绑定到嵌套对象的属-->
<!--绑定book.category.id-->
<!--items指定select的可选值来自model中的categories集合-->
<!--itemLabel指定集合中对象的某个属性为每个input元素提供label-->
<!--itemValue指定集合中对象的某个属性为每个input元素提供值-->
<form:select path="category.id" items="${categories}" itemLabel="name" itemValue="id"/>
</p>
<p>
<!-- path指定输入字段绑定到formbacking object的一个属性-->
<!--绑定book.title-->
<label for="title">Title: </label>
<form:input id="title" path="title"/>
</p>
<p>
<!--绑定book.author-->
<label for="author">Author: </label>
<form:input id="author" path="author"/>
</p>
<p>
<!--绑定book.isbn-->
<label for="isbn">ISBN: </label>
<form:input id="isbn" path="isbn"/>
</p>
<p id="buttons">
<input id="reset" type="reset" tabindex="4">
<input id="submit" type="submit" tabindex="5"
value="Add Book">
</p>
</fieldset>
</form:form>
</div>
</body>
</html>
表单标签库中包含了可以用在JSP页面中渲染HTML元素的标签。为了使用这些标签,必须在JSP页面的开头处声明这个taglib指令:<%@taglib prefix="form" uri="http://www.springframework.org/tags/form" %>,form标签用于渲染HTML表单。form标签必须利用渲染表单输入字段的其他任意标签。commandName属性或许是其中最重要的属性,因为它定义了模型属性的名称,其中包含了一个backingobject,其属性将用于填充所生成的表单。如果该属性存在,则必须在返回包含该表单的视图的请求处理方法中添加相应的模型属性。在该例子中,book_input请求的处理方法中,会在Model中添加要绑定的对象,然后在BookAddForm.jsp中通过commandName绑定该对象。其他输入标签中的path指定了要与其绑定的对象属性。
springmvc表单标签库的使用的更多相关文章
- SpringMvc表单标签库
HTML密码框 <td><form:label path="password">密码:</form:label></td><t ...
- 关于Spring MVC中的表单标签库的使用
普通的MVC设计模式中M代表模型层,V代表视图层,C代表控制器,SpringMVC是一个典型的MVC设置模式的框架,对于视图和控制器的优化很多,其中就有与控制器相结合的JSP的表单标签库. 我们先简单 ...
- (转载)SPRINGMVC表单标签简介
SpringMVC表单标签简介 在使用SpringMVC的时候我们可以使用Spring封装的一系列表单标签,这些标签都可以访问到ModelMap中的内容.下面将对这些标签一一介绍. 在正式介绍Spri ...
- SpringMVC表单标签简介
在使用SpringMVC的时候我们可以使用Spring封装的一系列表单标签,这些标签都可以访问到ModelMap中的内容.下面将对这些标签一一介绍. 在正式介绍SpringMVC的表单标签之前,我们需 ...
- Spring MVC 数据绑定和表单标签库
数据绑定是将用户输入绑定到领域模型的一种特性.作用是将 POJO 对象的属性值与表单组件的内容绑定. 数据绑定的好处: 1. 类型总是为 String 的 HTTP 请求参数,可用于填充不同类型的对象 ...
- SpringMVC听课笔记(SpringMVC 表单标签 & 处理静态资源)
1.springmvc表单标签,可以快速开发,表单回显,但是感触不深 2.静态资源的获取,主要是要配置这个
- SpringMVC 表单标签 & 处理静态资源
使用 Spring 的表单标签 通过 SpringMVC 的表单标签可以实现将模型数据中的属性和 HTML 表单元素相绑定,以实现表单数据更便捷编辑和表单值的回显. form 标签 一般情况下,通过 ...
- SpringMVC 表单标签
引入标签库 <%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" ...
- SpringMVC表单标签
SpringMVC学习系列(11) 之 表单标签 本篇我们来学习Spring MVC表单标签的使用,借助于Spring MVC提供的表单标签可以让我们在视图上展示WebModel中的数据更加轻松. ...
随机推荐
- 查看mongodb状态
netstat -ntlp|grep 27017
- 实在是秒啊,我还从来没见过把Spring之AOP讲的这么通俗易懂的,安排!
Spring之AOP 什么是AOP? AOP 为 Aspect Oriented Programming 的缩写,意思为面向切面编程,是通过预编译方式和运行期动态代理实现程序功能的统一维护的一种技术. ...
- 如何使用MindManager更改思维导图布局
思维导图可以帮您直观地捕捉想法和信息,并将其组织起来,进一步创建行动计划,思维导图软件MindManager不仅可以帮您分析问题.使用头脑风暴得出解决方案,还可以规划复杂的项目.下面是MindMana ...
- symfony中,使用原声的sql语句
1 /** 2 * 数组形式的原生sql 3 */ 4 public function arrayA(array $did) 5 { 6 $statement = $this->getEntit ...
- 通用于wps和excel的ntlm hashes窃取利用方式
https://evi1cg.me/archives/Get_NTLM_Hashes.html介绍了通过Microsoft Office 窃取 NTLM Hashes. 不过这种插入方法不适用于wps ...
- Django踩坑记录2
错误如下 OperationalError no such table 解决方法: 首先执行: python manage.py makemigrations 再执行 python manage.py ...
- 【初等数论】费马小定理&欧拉定理&扩展欧拉定理(暂不含证明)
(不会证明--以后再说) 费马小定理 对于任意\(a,p \in N_+\),有 \(a^{p-1} \equiv 1\pmod {p}\) 推论: \(a^{-1} \equiv a^{p-2} \ ...
- iOS UIImageView contentMode使用详解
UIImageView 的contentMode这个属性是用来设置图片的显示方式,如居中.居右,是否缩放等,有以下几个常量可供设定:UIViewContentModeScaleToFill //填满i ...
- Verilog单周期CPU(未完待续)
单周期CPU:指令周期=CPU周期 Top模块作为数据通路 运算器中有ALU,通路寄存器(R1.R2.R3.R4),数据缓冲寄存器(鉴于书上的运算器只有R0)........... 此为ALU和通用寄 ...
- Hbase 2.2.2 安装、配置(兼容 Hadoop 3.1.3)
准备 Hbase 2.2.2 安装包 下载链接 链接:https://pan.baidu.com/s/1TqEry-T7sYpq4PdhgLWdcQ 提取码:de5z 安装 上传到虚拟机上,之后解压即 ...