Home  >  Q&A  >  body text

java - Spring MVC怎么实现提交表单后跳转?

我想在addItem的页面输入了信息之后点提交,可以提交表单然后跳转到queryItems的页面。请问要怎么写?是在Controller里写还是在JSP页面里写?
基础问题,刚开始学,求回答,非常感谢。

两个Jsp页面:
addItem.jsp

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html><head><title>添加</title></head>
<body>
<form method="post" action="/items/add.action">
    输入姓名<input name="username" type="text" id="txtname"><br>
    输入邮箱<input name="useremail" type="text" id="txtemail"><br>
    <input type="submit" value="录入" >
</form>
</body>
</html>

queryItems.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
         pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/fmt"  prefix="fmt"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8"><title>查询</title></head>
<body>
<h1>用户列表</h1>
<form action="/items/query.action" method="get">
    <c:forEach var="user" items="${userList}">
        <c:out value="${user.user_name}"/>
        ----<c:out value="${user.user_email}"/>
        <br>
    </c:forEach>
</form>
</body>
</html>

我的Controller类

@Controller
@RequestMapping("/items")
public class ItemsController {
    @Autowired
    private ItemsService itemsService;
    
    @RequestMapping("/query")
    public ModelAndView queryItems() throws Exception{
        List<User> itemsList = itemsService.findAllUser();
        ModelAndView mv = new ModelAndView();
        mv.addObject("userList", itemsList);
        mv.setViewName("queryItems");
        return mv;

    }
    @RequestMapping("/add")
    public ModelAndView addItem(String username, String useremail) throws Exception{
        User user = new User();
        user.setUser_name(username);
        user.setUser_email(useremail);
        itemsService.insertUser(user);
        ModelAndView mv = new ModelAndView();
        mv.addObject(user);
        mv.setViewName("addItem");
        return mv;
    }
}
PHP中文网PHP中文网2742 days ago664

reply all(3)I'll reply

  • ringa_lee

    ringa_lee2017-04-18 10:58:20

    Write it in the controller
    After processing the request (a certain method) of the addItem page, return and write like thisreturn "redirect:url";You can redirect to the url you specify, and the url is the url of your queryItems

    reply
    0
  • ringa_lee

    ringa_lee2017-04-18 10:58:20

    I just modified the code and wrote two Controller methods separately for get and post on the add page. The post method returns a redirection, achieving the desired effect. Is there any other way?

    reply
    0
  • 伊谢尔伦

    伊谢尔伦2017-04-18 10:58:20

    Is your addItem.jsp jumped through the controller add?
    If not, access addItem.jsp directly.
    Then when submitting, add the words if successful. Jump to queryItems.jsp, and return to addItem.jsp if the addition is not successful, then the addItem.jsp page will prompt the user with an error message.

    reply
    0
  • Cancelreply