1

I am going to use @ModelAttribute instead of @RequestParam to bind the user input and write it to the database. I am just confused when the @ModelAttribute bind the data in my request handler method (@ModelAttribute book Book) as an book object then how should I pass this object to the database? Normally using @RequestParam I bind the user inputs variable by variable according to my model class and then I send them to the db using the related DAO method. I show my classes in below. Can anybody say how my request handler method should look like if I use @ModelAttribute?

Model Class:

@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;
}
}

DAO:

public class BookDAO extends JdbcDaoSupport {
@Autowired
AuthorDAO authorDAO;
@Autowired
PublisherDAO publisherDAO;

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

    String sql = "insert into tbl_book (title, authId, pubId) values (?, ?, ?)";
    this.getJdbcTemplate().update(sql, new Object[]{title, authorId, publisherId});
}
}

Service:

@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);
}
}

Controller:

@Controller

public class BookController {

@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;

}
}
Harshal Patil
  • 6,659
  • 8
  • 41
  • 57
Nisman
  • 1,271
  • 2
  • 26
  • 56
  • check this http://stackoverflow.com/questions/3423262/what-is-modelattribute-in-spring-mvc – Ramesh Kotha Feb 28 '15 at 17:02
  • Thanks for reference. I know the usage of the @ModelAttribute and how to bind the data. I just do not know how to pass the object to the database. If you see my addBook() method it needs to get three parameters to do the job. But in this case I have an object. My question is can I send the object without breaking it into those parameters? – Nisman Feb 28 '15 at 17:18
  • you dont need to do anything all you need is the object property names and the form parameter names should be equal – Ramesh Kotha Feb 28 '15 at 17:34
  • That is completely true. How to use it in my service method to add a row in the database? – Nisman Feb 28 '15 at 17:47

1 Answers1

0

Your form should have parameters names as your book object, check below sample code

<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;

}
Ramesh Kotha
  • 8,266
  • 17
  • 66
  • 90
  • welcome @Nisman, please accept it as answer if it works for you – Ramesh Kotha Feb 28 '15 at 18:54
  • Ramesh I got another problem. I have four variables in my Book model that two of them are objects(int authorId, String title, Author author, Publisher publisher). the user select the "author" and "publisher" and enter the "title" when wants to add a new book. I use tag to send the inputs to the controller. But I can not get the "author" and "publisher" objects in my ModelAttribute. I just received the title which is a String. Do you have any hint to solve this problem? – Nisman Feb 28 '15 at 21:24
  • please put a comment before you down vote, so that i can improve it. – Ramesh Kotha Mar 01 '15 at 11:17
  • What do you mean? (I am new in Stack Overflow) – Nisman Mar 01 '15 at 15:08
  • I did not down vote your answer. It was really helpful! But it just applicable for (int bookId, String title) which are not objects. My question is if I have objects as member variables in the Book class like (Author author, Publisher publisher) how can I send them to the model ? my jsp sends null for them. – Nisman Mar 01 '15 at 18:06