项目结构
看一眼config包下的MyMvcConfig类 不用写也会 localhost:8080自动找index页面并跳转
//自己对mvc的一些配置,虽然springboot帮我们配置了,但是有些我想弄成别的样的所以得自己配一下 @Configuration public class MyMvcConfig implements WebMvcConfigurer { @Override public void addViewControllers(ViewControllerRegistry registry) { //这样的话我在浏览器的地址栏打"/"或者"/index.html"都会跳转到index(首页) 虽然直接写localhost:8080也能自动跳转到首页 registry.addViewController("/").setViewName("index"); registry.addViewController("/index.html").setViewName("index"); } }
Controller
@Controller public class LoginController { // @RequestMapping("/user/login") // public String login(User user){ // if (user.username == "hi" || user.userpassword == "hi"){ // return "dashboard"; // }else { // model.addAttribute("msg","登陆失败"); // return "index"; // } // } @RequestMapping("/user/login") public String login(@RequestParam("username") String username, @RequestParam("userpassword") String userpassword, Model model){ if (username == "hi" || userpassword == "hi"){ return "dashboard"; }else { model.addAttribute("msg","登陆失败"); return "index"; } } }
前端页面
thymleaf的头
<html lang="en" xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org">