problems_springmvc

1 session中的user对象失效后,页面一刷新,又会提交request中的username和userpwd,导致登录成功

该问题出现在2个场景中,一个是tomcat重启后,另一个是session失效后。并且,平时刷新页面,系统还会提示,是否需要重复提交表单。
解决步骤:

  1. Controller中设置:登录成功后,移除request对象的username和userpwd参数
    该方法不可行,应为request中的parameterMap对象,是final类型的,不能重新赋值。
@RequestMapping(value = "/login" , method=RequestMethod.POST)
	public ModelAndView login(HttpServletRequest req, HttpServletResponse resp,
			RedirectAttributes rattr){
		log.info("login start...");
		String userId = req.getParameter("username");
		String pwd = req.getParameter("userpwd");
		ModelAndView mav = new ModelAndView();
		//... 省略中间的业务逻辑
		Map map = req.getParameterMap();
		map.put("username",""); //移除request对象的username参数
		map.put("userpwd",""); //移除request对象的userpwd参数
		mav.setViewName("main/index");
  1. Controller中设置:登录成功后,重定向到另一个Controller方法
    可行,该方法不仅解决了上述问题,而且在刷新首页时,不会提示“重复提交表单”。
	@RequestMapping(value = "/login" , method=RequestMethod.POST)
	public ModelAndView login(HttpServletRequest req, HttpServletResponse resp,
			RedirectAttributes rattr){
		log.info("login start...");
		String userId = req.getParameter("username");
		String pwd = req.getParameter("userpwd");
		ModelAndView mav = new ModelAndView();
		//... 省略中间的业务逻辑
		mav.setViewName("redirect:/index");

	@RequestMapping("/index")
	public String index(HttpServletRequest req, HttpServletResponse resp){
		log.info("index() =================");
		return "main/index";
	}

2

3

4

5

6

7

8

原文地址:https://www.cnblogs.com/mediocreWorld/p/15188873.html