SpringMVC使用Map处理模型数据,传值到页面

背景:SpringBoot+thymeleaf

SpringMVC在调用方法前会创建一个隐含的模型对象作为模型数据的存储容器。

Controller层

@Controller
public class LoginController {

    //@RequestMapping(value = "/user/login",method = RequestMethod.POST)
    @PostMapping(value = "/user/login")
    public String login(@RequestParam("username") String username,
                        @RequestParam("password") String password,
                        Map<String,Object> map, HttpSession session){
        if(!StringUtils.isEmpty(username) && "123456".equals(password)){
            //登陆成功,防止表单重复提交,可以重定向到主页
            session.setAttribute("loginUser",username);
            return "redirect:/main.html";
        }else{
            //登陆失败

            map.put("msg","用户名密码错误");
            return  "login";
        }

    }
}

html

<p style="color: red" th:text="${msg}" th:if="${not #strings.isEmpty(msg)}"></p>

https://jingyan.baidu.com/article/90895e0f21132964ed6b0b77.html

posted @ 2020-04-25 20:37  你猜lovlife  阅读(423)  评论(0)    收藏  举报