Spring Boot-Controller类重定向页面记得加/


@Controller
public class LoginController {

    @PostMapping("user/login")
    public String login(Map map,
                      @RequestParam("username") String username,
                      @RequestParam("password") String password) {
        if (!StringUtils.isEmpty(username)&&"123".equals(password)){
            return "redirect:/main.html";
        }
        else {
            map.put("message","用户名或密码错误");
            return "login";
        }
    }
}
@Configuration
public class MyMvcConfig implements WebMvcConfigurer {

    @Override
    public void addViewControllers(ViewControllerRegistry registry) {
        registry.addViewController("/").setViewName("login");
        registry.addViewController("/index.html").setViewName("login");
        registry.addViewController("/main.html").setViewName("dashboard");
    }
}

重定向一定要加/,表示从当前项目下寻找

不然,不加斜杠访问的就是user/main.html,user是@PostMapping("user/login")里的user,换成@PostMapping("nnn/login")就变成nnn/main.html

你可能感兴趣的:(spring,boot)