踩坑 SpringBoot如何跳转到网页

首先不使用模板

那该如何做呢

先建一个controller如下

@Controller

public class indexController {
    @RequestMapping("/user")
        public ModelAndView index() {
            return new ModelAndView("login");

    }
}

注意:@controller很重要,不要使用@restcontroller,因为他是返回json

嗯,在resource/static下新建一个login.html

<body>
<form action="/user/login" method="post">
    请输入用户名 <input type="text" name="name" style="align-content: center"/>
    <br>
    请输入密码 <input type="password" name="password"  style="align-content: center"/>
    <br>
    <input type="submit" value="Submit" style="margin-left: 50px"/>
    <br>
    <a href="register.html">未注册的话,请点击该行文字</a>
</form>
</body>

然后再新建一个controller

@Controller
@RequestMapping("/user")
public class LoginController {
    @Autowired
    private ILoginService iLoginService;

    @PostMapping(value = "/login")
    public ModelAndView postUser(HttpServletRequest request) {
        String name=request.getParameter("name");
        String password=request.getParameter("password");
        String text = iLoginService.loginUser(name, password);
        if (text.equals("登录成功")) {
            return new  ModelAndView("userPage");
        } else {
            return new  ModelAndView("redirect:/user");
        }

    }
}

从一般接口的请求方式到HttpServletRequest request,然后能出现表单等按钮,点击之后
却死活显示不出来

再数据库中一查,数据有变动,说明逻辑代码是对的,那为什么就不行呢

百度了好久,,,,,,,好久,,,,,好久,,,

竟然发现也有人和我遇到一样的问题,哈,原来还有同道中人哇

唧唧,但是他们也没得出结论

百度一般的结果就是:不要@restcontroller啥的

终于百度到了一个可以有用的,哈,就是使用模板

再pom下添加依赖

<dependency>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>

再application.properties下添加配置

spring.thymeleaf.prefix=classpath:/templates/

然后奇迹出现啦,竟然成功了

能跳到userPage的网页

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容