0
請與我有限的Spring MVC的knowlodge承擔不同的方法,仍試圖瞭解它是如何工作的。Spring MVC的:獲取會話屬性進行的跨在同一個控制器
我的問題如下:我正在進行一個簡單的猜謎遊戲,在該遊戲中,您將從下拉選擇選項中選擇一封信,並刷新相同的視圖,並更新您做出多少次猜測的信息。
遊戲(模型類)
public class Game {
private Player player;
private Language language;
private Random randomGenerator;
private List<String> dictionary;
private char[] selectedWord;
與其相應的getter/setter方法和這樣。
控制器類:
@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("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,
@ModelAttribute("Game") Game game,
BindingResult result,
SessionStatus status, ModelMap model) {
/* I'm aware I would need a validator of some sort, but for now I'm trying to get this to work without one*/
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, HttpSession session,
@ModelAttribute("Game") Game game) {
ModelAndView mav = new ModelAndView("/views/index.jsp");
if(session.getAttribute("game") != null) {
System.out.println("finding session attributes");
game = (Game)session.getAttribute("game");
mav.addObject("game", game);
} else {
System.out.println("no luck finding those");
}
return mav;
}
@RequestMapping (value="/guessLetter", method=RequestMethod.POST)
public String guessLetter (HttpServletRequest request, HttpServletResponse response,
HttpSession session, @ModelAttribute("Game") Game game) {
if(session.getAttribute("game") != null) {
System.out.println("ESTOY buscando session attr mietras adivino");
game = (Game)session.getAttribute("game");
System.out.println("guess?" + game.getGuess());
} else {
System.out.println("nothing gets here");
}
return "redirect:/index.htm";
}
}
請讓我知道,如果我需要更多的一些信息
'@ModelAttribute(「遊戲」)'根本不匹配'@SessionAttributes名稱(「遊戲」)',因此不能存儲,並從會話檢索。 –
@ M.Deinum就是這樣!謝謝!如果您將它作爲答案發布,我會將其作爲正確答案進行檢查。 –
由於我投了問題的封閉,因爲它只是一個錯字答案的性質。 –