zoukankan      html  css  js  c++  java
  • 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指定了要与其绑定的对象属性。

  • 相关阅读:
    Linux 详细命令操作大全二
    Liunx 环境安装配置Jupyter
    Linux 详细命令操作大全一
    Mysql int(10)和int(11)区别
    Python 面向对象编程
    Oracle数据库工具设置时间显示格式
    vue安装时报错:npm WARN deprecated urix@0.1.0: Please see https://github.com/lydell/urix#deprecated
    Oracle对当前时间的处理
    Oracle合并不同条件下统计出的总数
    记录一下vue-cli3搭建项目后遇到axios请求跨域的问题
  • 原文地址:https://www.cnblogs.com/darange/p/11164773.html
Copyright © 2011-2022 走看看