亚洲国产日韩欧美一区二区三区,精品亚洲国产成人av在线,国产99视频精品免视看7,99国产精品久久久久久久成人热,欧美日韩亚洲国产综合乱

java - Spring MVC怎么實(shí)現(xiàn)提交表單后跳轉(zhuǎn)?
PHP中文網(wǎng)
PHP中文網(wǎng) 2017-04-18 10:56:20
0
3
760

我想在addItem的頁面輸入了信息之后點(diǎn)提交,可以提交表單然后跳轉(zhuǎn)到queryItems的頁面。請(qǐng)問要怎么寫?是在Controller里寫還是在JSP頁面里寫?
基礎(chǔ)問題,剛開始學(xué),求回答,非常感謝。

兩個(gè)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中文網(wǎng)
PHP中文網(wǎng)

認(rèn)證0級(jí)講師

reply all(3)
左手右手慢動(dòng)作

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

左手右手慢動(dòng)作

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?

伊謝爾倫

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.

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template