package bookstore.controller;
import javax.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;
import bookstore.domain.Book;
import bookstore.service.BookService;
@Controller
@RequestMapping ("/*")
public class BookController {
@Autowired
BookService bookService;
@RequestMapping (value = "/addBook", method=RequestMethod.GET)
public ModelAndView addBook() {
Book b1 = new Book();
b1.setTitle("book name");
return new ModelAndView ("addBook", "book", b1);
}
@RequestMapping (value = "/saveBook", method=RequestMethod.POST)
public String saveBook(@Valid Book book, BindingResult result, ModelMap model) {
if (result.hasErrors()) {
return "addBook";
} else {
bookService.persist(book);
model.put("allBooks", bookService.getAll());
return "listBooks";
}
}
@RequestMapping (value = "/abc", method=RequestMethod.GET)
public ModelAndView anyName() {
Book book1 = new Book();
book1.setTitle("Tale of Two Cities");
return new ModelAndView("addBook", "xyz", book1);
}
}
No comments:
Post a Comment