springmvc 关于数据绑定

最基本数据绑定

将所有字段在form表单中直接提交

例如:

Jsp 数据提交

<formaction="submitall.do"method="post"> 

   <div>姓名:</div> 

   <div><input name="name" value="zzz"/></div> 

   <div></div> 

   <div>年龄:</div> 

   <div><input name="age" value="55"/></div> 

   <div>兴趣:</div> 

   <div> 

   <inputtype="checkbox"name="interests"value="听歌"checked="checked"/>听歌 

   <inputtype="checkbox"name="interests"value="书法"checked="checked"/>书法 

   <inputtype="checkbox"name="interests"value="看电影"/>看电影 

   </div> 

   <divclass="clear"></div> 

   <div><inputtype="submit"value="提交表单"/></div> 

</form> 


Controller 接收数据

   @RequestMapping("/submitall")

   public void login(String name, Integerage, Double income, BooleanisMarried,

        String[]interests){

       System.out.println("简单数据类型绑定========="); 

           System.out.println("名字:" +name); 

           System.out.println("年龄:" +age); 

           System.out.println("兴趣:"); 

           for (Stringinterest : interests

           { 

               System.out.println(interest); 

           } 

           System.out.println("===================="); 

       }



对象类型绑定

将数据封装在对象中,book对象:Book.java

package com.test.entity;

import java.io.Serializable;

import javax.persistence.*;

importorg.hibernate.annotations.GenericGenerator;

/**

 *The persistent class for the book database table.

 *

 */

@Entity

@Table(name="book") 

public class Book implements Serializable {

         privatestatic final long serialVersionUID = 1L;

   @Id 

   @GeneratedValue(generator="system-uuid") 

   @GenericGenerator(name ="system-uuid",strategy="uuid") 

   @Column

         privateint id;

   @Column

   private String auther;

   @Column

   private String bookname;

   @Column

   private String isbn;

   @Column

   private int price;

   @Column

   private int stock;

         publicBook() {

         }

         publicint getId() {

                   returnthis.id;

         }

         publicvoid setId(int id) {

                   this.id= id;

         }

         publicString getAuther() {

                   returnthis.auther;

         }

         publicvoid setAuther(String auther) {

                   this.auther= auther;

         }

         publicString getBookname() {

                   returnthis.bookname;

         }

         publicvoid setBookname(String bookname) {

                   this.bookname= bookname;

         }

         publicString getIsbn() {

                   returnthis.isbn;

         }

         publicvoid setIsbn(String isbn) {

                   this.isbn= isbn;

         }

         publicint getPrice() {

                   returnthis.price;

         }

         publicvoid setPrice(int price) {

                   this.price= price;

         }

         publicint getStock() {

                   returnthis.stock;

         }

         publicvoid setStock(int stock) {

                   this.stock= stock;

         }

}

Jsp页面数据的提交:Book_edit.jsp

<formaction="<%=basePath%>book/updatebook.do"name="Form"

            method="post">

            <inputtype="hidden"name="id"value="${book.id }">

            书名:<inputtype="text"name="bookname"value="${book.bookname}"/>

            作者:<inputtype="text"name="auther"value="${book.auther}"/>

            isbn<inputtype="text"name="isbn"value="${book.isbn }"/>

            单价:<inputtype="text"name="price"value="${book.price }"/>

                    库存:<inputtype="text"name="stock"value="${book.stock }"/>

            <inputtype="submit"value="编辑">

     

</form>

 

数据提交

   @RequestMapping("/updatebook")

   public ModelAndViewupdateBook(Modelmodel,Bookdto){

       System.out.println("***Bookname****"+dto.getBookname());

       System.out.println("***Auther****"+dto.getAuther());

       System.out.println("***Price****"+dto.getPrice());

       System.out.println("***Id****"+dto.getId());

       System.out.println("***Stock****"+dto.getStock());

       

      // bookManager.updateBook(dto);

        return new ModelAndView("success"); 

   } 

  

     

猜你喜欢

转载自blog.csdn.net/qi95719/article/details/53135956