SpringBoot的@RequestMapping的return不会跳转到新页面

<form role="form" action="/success" method="post">
    账号:<input type="text" id="id" name="id"> <br>
    密码:<input type="password" id="password" name="password"> <br>
    <input type="submit" id="login" value="login">
</form>

<a href="/success">AllMovies</a>
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>success</title>
</head>
<body>
<h1>登录成功!</h1>
<table>
    <tr>
        <th>name</th>
        <th>director</th>
        <th>genres</th>
        <th>country</th>
    </tr>

    <tr th:each="movieInfo : ${list}">
        <td th:text="${movieInfo.title}"></td>
        <td th:text="${movieInfo.director_name}"></td>
        <td th:text="${movieInfo.genres}"></td>
        <td th:text="${movieInfo.coutry}"></td>
    </tr>
</table>
</body>
</html>

 

@RequestMapping(value = "/success", method = RequestMethod.POST)
    public String login(int id, String password) {
        User user = userService.loginIn(id, password);
        if (user != null) {
            return "success";
        } else {
            return "login";
        }
    }
@RequestMapping("/success")
    public String index(ModelMap map) {
        List<MovieInfo> list = movieInfoService.getAllMovies();
        map.addAttribute("list", list);
        return "success";
    }

点击form里的提交按钮

点击超链接

如果想在点击form的提交按钮后加载内容该怎么办

将你的代码整合一下就行了,把上面的两个后端代码合为一起,前端的那个AllMovies超链接就不要了,下面的两个后端代码整合如下

@RequestMapping(value = "/success", method = RequestMethod.POST)
    public String login(int id,String password,ModelMap map) {
        User user = userService.loginIn(id, password);
        if (user != null) {
List<MovieInfo> list = movieInfoService.getAllMovies();
        map.addAttribute("list", list);
            return "success";
        } else {
            return "login";
        }
    }

 

登陆成功后,user!=null后面,把index函数里面代码拷贝进去就可以了。

您的问题已经有小伙伴解答了,请点击【采纳】按钮,采纳帮您提供解决思路的答案,给回答的人一些鼓励哦~~

ps:开通问答VIP,享受5次/月 有问必答服务,了解详情↓↓↓

【电脑端】戳>>>  https://vip.csdn.net/askvip?utm_source=1146287632
【APP 】  戳>>>  https://mall.csdn.net/item/52471?utm_source=1146287632