0
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;
}
}