春季MVC,更新表单上的模型贴图仍然显示旧信息
问题描述:
请承担我对spring mvc的有限了解。 我试图在POST请求后用更新的模型信息刷新视图。新的信息出现在视图上,以及旧版本,这完全混淆了视图。春季MVC,更新表单上的模型贴图仍然显示旧信息
这是我的控制器代码。如果您发现某些不正确的信息,请提前通知我!
@Controller
@SessionAttributes({"Game"})
public class SimpleController {
@Autowired
private SessionLocaleResolver localeResolver;
private LoginValidator loginValidator;
private GameValidator gameValidator;
@Autowired
public void setLoginValidator(LoginValidator loginValidator) {
this.loginValidator = loginValidator;
}
@Autowired
public void setGameValidator(GameValidator gameValidator) {
this.gameValidator = gameValidator;
}
@RequestMapping(value = "/login", method = RequestMethod.GET)
public ModelAndView init(ModelMap model) {
ModelAndView mav = new ModelAndView("/views/login.jsp");
LoginBean loginBean = new LoginBean();
model.addAttribute("LoginBean", loginBean);
model.addAttribute("game", new Game());
model.addAttribute("ENGLISH", Language.ENGLISH);
model.addAttribute("SPANISH", Language.SPANISH);
mav.addObject("LoginBean", loginBean);
return mav;
}
@RequestMapping (value="/processLogin", method=RequestMethod.POST)
public String login (HttpServletRequest request, HttpServletResponse response,
@ModelAttribute("loginBean") LoginBean loginBean,
BindingResult result, ModelMap model) {
this.loginValidator.validate(loginBean, result);
if (result.hasErrors()) {
return "redirect:/login.htm";
}
Game game = new Game();
if (loginBean.getLanguage() == Language.ENGLISH) {
localeResolver.setLocale(request, response, new Locale("EN"));
}
else{
localeResolver.setLocale(request, response, new Locale("ES"));
}
loginBean.setDictionary(FileLoader.loadDictionary(loginBean.getLanguage()));
game.setPlayer(loginBean.getPlayer());
game.setLanguage(loginBean.getLanguage());
game.setDictionary(loginBean.getDictionary());
game.setSelectedWord(game.getRandomWord(game.getDictionary()));
model.addAttribute("Game", game);
request.getSession().setAttribute("game", game);
System.out.println(game.getSelectedWord());
return "redirect:/index.htm";
}
@RequestMapping(value = "/index", method = RequestMethod.GET)
public ModelAndView play(HttpServletRequest request,
@ModelAttribute("game") Game game, ModelMap model){
ModelAndView mav = new ModelAndView("/views/index.jsp");
if(request.getSession().getAttribute("game") != null) {
game = (Game)request.getSession().getAttribute("game");
mav.addObject("game", game);
} else {
System.out.println("nothing received");
}
request.getSession().setAttribute("game", game);
return mav;
}
@RequestMapping (value="/guessLetter", method=RequestMethod.POST)
public ModelAndView guessLetter (HttpServletRequest request, HttpServletResponse response,
@ModelAttribute("game") Game game, @RequestParam("guess") String guess,
SessionStatus status, ModelMap model) {
ModelAndView mav = new ModelAndView("/views/index.jsp");
if(request.getSession().getAttribute("game") != null) {
game = (Game)request.getSession().getAttribute("game");
game.guessLetter(guess);
} else {
System.out.println("no guess!");
}
//model.replace("game", game); tried this, but didn't work
System.out.println(model.entrySet());
model.addAttribute("Game", game);
//request.getSession().setAttribute("game", game);
mav.addObject("game", game);
status.setComplete();
return mav;
}
}
答
原来我没有完全理解SessionStatus方法。这个答案已经真正有用的https://stackoverflow.com/a/24342814/2438413
下面是任何人的情况下我的职业控制器想以供参考:
@RequestMapping (value="/guessLetter", method=RequestMethod.POST)
public ModelAndView guessLetter (HttpServletRequest request, HttpServletResponse response,
@ModelAttribute("game") Game game, @RequestParam("guess") String guess,
SessionStatus status, ModelMap model, BindingResult result) {
System.out.println(guess);
this.letterValidator.validate(game, result);
if (result.hasErrors()) {
return new ModelAndView("/views/index.jsp");
}
request.getSession().setAttribute("game", game);
game.guessLetter(guess);
System.out.println(game.getGuessList());
ModelAndView mav = new ModelAndView("redirect:index.htm");
mav.addObject("game", game);
model.addAttribute("game", game);
return mav;
}