请承担我对spring mvc的有限知识。 我正在尝试在POST请求后使用更新的模型信息刷新视图。新信息显示在视图上,以及旧版本,它完全混淆了视图。
这是我的控制器代码。如果你看到不正确的东西,请提前告知我们!
@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;
}
}
答案 0 :(得分:0)
事实证明我并不完全了解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;
}