简体   繁体   English

如何从 Spring MVC 中同一控制器中不同 JSP 页面上的表单访问值?

[英]How do I access values from forms on different JSP pages in the same controller in Spring MVC?

I am trying to make a Book Management project where I have three buttons on the home.jsp page.我正在尝试制作一个图书管理项目,其中 home.jsp 页面上有三个按钮。 Each button redirects to a separate page and each of these pages has a form.每个按钮重定向到一个单独的页面,每个页面都有一个表单。 I have one Controller class that has three methods to handle each form submissions from each of these pages but when I try to use @ModelAttribute in the JSP page for any form, I am unable to get the value that I add to the Model.我有一个 Controller 类,它具有三种方法来处理来自每个页面的每个表单提交,但是当我尝试在 JSP 页面中为任何表单使用 @ModelAttribute 时,我无法获得添加到模型中的值。

Here is the home.jsp:这是 home.jsp:

 <body> <div class="container"> <div> <h1>Spring Boot Web JSP Example</h1> <h2>Name: ${book.name}</h2> <h2>Author: ${book.author}</h2> <h2>ISBN: ${book.isbn}</h2> </div> </div> <form:form method="POST" action="/get" modelAttribute="newBook"> <div class="form-group"> <label for="authorInput">Author</label> <form:input path="author" cssClass="form-control" id="authorInput"></form:input> </div> <div class="form-group"> <label for="dateInput">Date</label> <form:input path="date" cssClass="form-control" id="dateInput"></form:input> </div> <button type="submit" class="btn btn-primary">Get Book</button> </form:form> <a href="../add.jsp"><button type="submit" class="btn btn-primary">Add Book</button></a> <a href="../update.jsp"><button type="submit" class="btn btn-primary">Update Book</button></a> </body>

Here is the controller class:这是控制器类:

@Controller public class MainController { @Controller 公共类 MainController {

@GetMapping(value = "/")
public String welcome(Map<String, Object> model) {
    model.put("newBook", new Book());
    model.put("updateBook", new Book());
    model.put("addBook",new Book());
    return "home";
}

@PostMapping(value = "/get")
public String change(@RequestParam("author") String author, Model model,
                     @ModelAttribute("newBook")Book book) {
    System.out.println(author);
    Book b = BookDao.getBook(book.getAuthor(), book.getDate());
    if(b == null){
        return "home";
    }
    model.addAttribute("book", b);
    model.addAttribute("newBook", new Book());
    return "home";
}
@RequestMapping(value = "/add")
public String addBook(@RequestParam("author") String author, @RequestParam("isbn") int isbn, Model model,
                      @ModelAttribute("addBook") Book book){
    System.out.println("Author: "+author + " ISBN: "+isbn);
    model.addAttribute("addBook", new Book());
    Book b= new Book(book.getName(), author,isbn, book.getDate());
    model.addAttribute("add", book);
    boolean result = BookDao.addBook(b);
    if(result)
        return "home";
    else
        return "error";
}
@RequestMapping( value = "/update")
public String updateBook(@RequestParam("author") String author, @RequestParam("isbn") int isbn, Model model,
                         @ModelAttribute("updateBook") Book book){
    System.out.println("Author: "+author + " ISBN: "+isbn);
    Book b= new Book(book.getName(), author,isbn, book.getDate());
    model.addAttribute("updateBook", new Book());
    model.addAttribute("update",b);
    BookDao.updateBook(isbn, b);
    return "home";
}

} }

And here are the other two jsp pages: Add.jsp:这是另外两个 jsp 页面:Add.jsp:

 <body> <h1>Add a Book</h1> <form:form method="POST" action="/add" modelAttribute="addBook"> <div class="form-group"> <label for="nameInput">Name</label> <form:input path="name" cssClass="form-control" id="nameInput"></form:input> </div> <div class="form-group"> <label for="authorInput">Author</label> <form:input path="author" cssClass="form-control" id="authorInput"></form:input> </div> <div class="form-group"> <label for="isbnInput">ISBN</label> <form:input path="isbn" cssClass="form-control" id="isbnInput"></form:input> </div> <div class="form-group"> <label for="dateInput">Date</label> <form:input path="date" cssClass="form-control" id="dateInput"></form:input> </div> <button type="submit" class="btn btn-primary">Add</button> </form:form> </body>

Update Book JSP Page:更新书籍 JSP 页面:

 <body> <form:form method="POST" action="/update" modelAttribute="third"> <div class="form-group"> <label for="authorInput">ISBN</label> <form:input path="isbn" cssClass="form-control" id="authorInput"></form:input> </div> <div class="form-group"> <label for="nameInput">Name</label> <form:input path="name" cssClass="form-control" id="nameInput"></form:input> </div> <div class="form-group"> <label for="authorInput">Author</label> <form:input path="author" cssClass="form-control" id="authorInput"></form:input> </div> <div class="form-group"> <label for="dateInput">Date</label> <form:input path="date" cssClass="form-control" id="dateInput"></form:input> </div> <button type="submit" class="btn btn-primary">Update Book</button> </form:form> </body>

The problem is that the lines modelAttribute="addBook" and modelAttribute="third" in the add.jsp page and update.jsp page throw an error.问题是 add.jsp 页面和 update.jsp 页面中的modelAttribute="addBook" 和 modelAttribute="third"行抛出错误。 The IDE says "Cannot resolve symbol 'addBook/third'". IDE 显示“无法解析符号‘addBook/third’”。 Those values are available in the home.jsp page though.不过,这些值在 home.jsp 页面中是可用的。

Since I found the answer, I will post it just in case somebody else gets stuck with it.既然我找到了答案,我会发布它,以防其他人被它卡住。 In order to access the forms on another JSP page, we can't just directly redirect to the page in an MVC design.为了访问另一个 JSP 页面上的表单,我们不能直接重定向到 MVC 设计中的页面。 In order to do so, we need to create a @GetMapping annotation method for each of those JSP forms @PostMapping annotations and then redirect to the @GetMapping methods first and the @GetMapping will redirect to the JSP page.为此,我们需要为每个 JSP 表单 @PostMapping 注释创建一个 @GetMapping 注释方法,然后首先重定向到 @GetMapping 方法,@GetMapping 将重定向到 JSP 页面。 This is how it should be done:应该这样做:

Controller Class:控制器类:

@Controller public class MainController { @Controller 公共类 MainController {

@GetMapping(value = "/")
public String welcome(Map<String, Object> model) {
    model.put("newBook", new Book());
    return "home";
}

@PostMapping(value = "/get")
public String change(@RequestParam("author") String author, Model model,
                     @ModelAttribute("newBook")Book book) {
    System.out.println(author);
    Book b = BookDao.getBook(book.getAuthor(), book.getDate());
    if(b == null){
        return "home";
    }
    model.addAttribute("book", b);
    model.addAttribute("newBook", new Book());
    return "home";
}

@GetMapping("/add")
public String show(Model model) {
    model.addAttribute("addBook", new Book());
    return "add";
}

@PostMapping(value = "/add")
public String addBook(@RequestParam("author") String author, @RequestParam("isbn") int isbn, Model model,
                      @ModelAttribute("addBook") Book book){
    System.out.println("Author: "+author + " ISBN: "+isbn);
    model.addAttribute("addBook", new Book());
    Book b= new Book(book.getName(), author,isbn, book.getDate());
    model.addAttribute("add", book);
    boolean result = BookDao.addBook(b);
    if(result)
        return "home";
    else
        return "error";
}

@GetMapping("/update")
public String showUpdate(Model model) {
    model.addAttribute("updateBook", new Book());
    return "update";
}

@PostMapping( value = "/update")
public String updateBook(@RequestParam("author") String author, @RequestParam("isbn") int isbn, Model model,
                         @ModelAttribute("updateBook") Book book){
    System.out.println("Author: "+author + " ISBN: "+isbn);
    Book b= new Book(book.getName(), author,isbn, book.getDate());
    model.addAttribute("updateBook", new Book());
    model.addAttribute("update",b);
    BookDao.updateBook(isbn, b);
    return "home";
}

And the JSP page for Home.jsp page should be as follows:而 Home.jsp 页面的 JSP 页面应该如下:

 <form:form method="POST" action="/get" modelAttribute="newBook"> <div class="form-group"> <label for="authorInput">Author</label> <form:input path="author" cssClass="form-control" id="authorInput"></form:input> </div> <div class="form-group"> <label for="dateInput">Date</label> <form:input path="date" cssClass="form-control" id="dateInput"></form:input> </div> <button type="submit" class="btn btn-primary">Get Book</button> </form:form> <a href ="/add"><button type="submit" class="btn btn-primary">Add Book</button></a> <a href ="/update"><button type="submit" class="btn btn-primary">Update Book</button></a> </body>

So the href maps to the @GetMapping method to get the ModelAttributes.所以href 映射到@GetMapping 方法来获取ModelAttributes。 The other JSP pages are fine as it is.其他 JSP 页面也没有问题。

Also, another good practice I might suggest is to Use a Service layer/package to perform all the operations.另外,我可能建议的另一个好做法是使用服务层/包来执行所有操作。 So instead of performing it in the Controller, we delegate tasks like the CRUD operations to the Service layer which in turn interacts with the DAO layer.因此,我们不是在控制器中执行它,而是将诸如 CRUD 操作之类的任务委托给服务层,服务层又与 DAO 层交互。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM