Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Spring 如何使用@ModelAttribute注释将对象传递到数据库?_Spring_Spring Mvc - Fatal编程技术网

Spring 如何使用@ModelAttribute注释将对象传递到数据库?

Spring 如何使用@ModelAttribute注释将对象传递到数据库?,spring,spring-mvc,Spring,Spring Mvc,我将使用@modeldattribute而不是@RequestParam来绑定用户输入并将其写入数据库。当@modeldattribute将我的请求处理程序方法(@modeldattributebook book)中的数据绑定为book对象时,我感到困惑,那么我应该如何将该对象传递到数据库?通常使用@RequestParam我根据模型类逐个绑定用户输入变量,然后使用相关的DAO方法将它们发送到db。我在下面展示我的课程。如果我使用@modeldattribute,有人能告诉我请求处理程序方法应该

我将使用
@modeldattribute
而不是
@RequestParam
来绑定用户输入并将其写入数据库。当
@modeldattribute
将我的请求处理程序方法(
@modeldattribute
book book)中的数据绑定为book对象时,我感到困惑,那么我应该如何将该对象传递到数据库?通常使用
@RequestParam
我根据模型类逐个绑定用户输入变量,然后使用相关的DAO方法将它们发送到db。我在下面展示我的课程。如果我使用
@modeldattribute
,有人能告诉我请求处理程序方法应该是什么样子吗

模型类:

@Component
public class Book {
int bookId;
String title;
Author author;
Publisher publisher;

public int getBookId() {
    return bookId;
}

public void setBookId(int bookId) {
    this.bookId = bookId;
}

public String getTitle() {
    return title;
}

public void setTitle(String title) {
    this.title = title;
}

public Author getAuthor() {
    return author;
}

public void setAuthor(Author author) {
    this.author = author;
}

public Publisher getPublisher() {
    return publisher;
}

public void setPublisher(Publisher publisher) {
    this.publisher = publisher;
}
}
道:

服务:

@Service
public class BookService {

@Autowired
BookDAO bookDAO;

public Book getBookById(int bookId) throws ClassNotFoundException,
        SQLException {

    return bookDAO.getBookById(bookId);

}

public List<Book> getAllBooks() throws ClassNotFoundException,
        SQLException {

    List<Book> bookList = bookDAO.getAllBooks();
    return bookList;

}

public void addBook(String title, int authorId, int publisherId) throws ClassNotFoundException,
        SQLException {

    bookDAO.addBook(title, authorId, publisherId);
}
}
公共类图书管理员{

@RequestMapping(value = "/addBookExecution", method = equestMethod.POST)
protected ModelAndView addBookExecution(@RequestParam String title,
        @RequestParam int authorId, @RequestParam int blisherId)
        throws ClassNotFoundException, SQLException {
    bookService.addBook(title, authorId, publisherId);
    ModelAndView model = new ModelAndView("adminFunctionsPage");
    model.addObject("Msg", "Your request has been processed successfully.");
    return model;

}
}

表单应该有参数名作为book对象,请检查下面的示例代码

<form >
<input type="text" name="authorId"/>
<input type="text" name="authorName"/>
etc...
</form>


Book.java

class Book{
  Integer authorId;
  String authorName;
  etc..
}
@RequestMapping(value = "/addBookExecution", method = equestMethod.POST)
protected ModelAndView addBookExecution(@ModelAttribute Book book)
        throws ClassNotFoundException, SQLException {
    bookService.addBook(book);
    ModelAndView model = new ModelAndView("adminFunctionsPage");
    model.addObject("Msg", "Your request has been processed successfully.");
    return model;

}

等
Book.java
课堂用书{
整数授权;
字符串authorName;
等
}
@RequestMapping(value=“/addBookExecution”,method=equestMethod.POST)
受保护的ModelAndView addBookExecution(@ModelAttribute Book)
抛出ClassNotFoundException、SQLException{
bookService.addBook(book);
ModelAndView模型=新的ModelAndView(“adminFunctionsPage”);
addObject(“Msg”,“您的请求已成功处理。”);
收益模型;
}

检查此项谢谢参考。我知道@modeldattribute的用法以及如何绑定数据。我只是不知道如何将对象传递到数据库。如果您看到我的addBook()方法它需要获得三个参数来完成这项工作。但在这种情况下,我有一个对象。我的问题是,我可以发送对象而不将其分解为这些参数吗?您不需要做任何事情,只需要对象属性名和表单参数名应该相等这是完全正确的。如何在我的服务方法中使用它来在数据库中添加一行?欢迎@Nisman,如果它适用于youRamesh,请接受它作为答案。我遇到了另一个问题。我的图书模型中有四个变量,其中两个是对象(int-authorId、String-title、Author-Author、Publisher-Publisher)。用户选择“Author”和“Publisher”并输入“title”当想要添加一本新书时。我使用tag将输入发送到控制器。但是我无法在ModelAttribute中获取“author”和“publisher”对象。我刚收到一个字符串标题。你有任何解决此问题的提示吗?请在投票前发表评论,以便我可以对其进行改进。你的意思是什么?(我是堆栈溢出新手)我没有否决你的答案。这真的很有帮助!但它只适用于非对象的(int bookId,String title)。我的问题是,如果我在图书类中有对象作为成员变量,比如(Author Author,Publisher Publisher),我如何将它们发送到模型?我的jsp为它们发送null。
@RequestMapping(value = "/addBookExecution", method = equestMethod.POST)
protected ModelAndView addBookExecution(@RequestParam String title,
        @RequestParam int authorId, @RequestParam int blisherId)
        throws ClassNotFoundException, SQLException {
    bookService.addBook(title, authorId, publisherId);
    ModelAndView model = new ModelAndView("adminFunctionsPage");
    model.addObject("Msg", "Your request has been processed successfully.");
    return model;

}
}
<form >
<input type="text" name="authorId"/>
<input type="text" name="authorName"/>
etc...
</form>


Book.java

class Book{
  Integer authorId;
  String authorName;
  etc..
}
@RequestMapping(value = "/addBookExecution", method = equestMethod.POST)
protected ModelAndView addBookExecution(@ModelAttribute Book book)
        throws ClassNotFoundException, SQLException {
    bookService.addBook(book);
    ModelAndView model = new ModelAndView("adminFunctionsPage");
    model.addObject("Msg", "Your request has been processed successfully.");
    return model;

}