Java SpringMVC:在同一控制器中跨不同方法获取会话属性
请接受我有限的SpringMVC知识,我仍在努力学习它是如何工作的 我的问题如下:我正在做一个简单的猜谜游戏,在这个游戏中,你可以从下拉选择选项中选择一封信,然后用你猜了多少次的更新信息刷新同一个视图 游戏(模型课)Java SpringMVC:在同一控制器中跨不同方法获取会话属性,java,spring,forms,spring-mvc,session,Java,Spring,Forms,Spring Mvc,Session,请接受我有限的SpringMVC知识,我仍在努力学习它是如何工作的 我的问题如下:我正在做一个简单的猜谜游戏,在这个游戏中,你可以从下拉选择选项中选择一封信,然后用你猜了多少次的更新信息刷新同一个视图 游戏(模型课) 如果我需要用更多信息更新问题,请告诉我。您可以声明一个会话范围bean类,在该类中可以保留所有必要的属性 当bean在控制器中自动连接时,您可以从控制器的任何方法获取/设置所需的所有字段 只需将游戏注释为@Component,并自动连接它,而不是手动创建它。@modeldattri
如果我需要用更多信息更新问题,请告诉我。您可以声明一个会话范围bean类,在该类中可以保留所有必要的属性 当bean在控制器中自动连接时,您可以从控制器的任何方法获取/设置所需的所有字段
只需将游戏注释为
@Component
,并自动连接它,而不是手动创建它。@modeldattribute(“游戏”)
与@SessionAttributes(“游戏”)
中的名称不匹配,因此不会从会话中存储和检索。@M.Deinum就是这样!非常感谢。如果你把它作为一个答案发布,我会检查它是否正确。由于答案的性质,我投票赞成结束这个问题,因为它只是一个打字错误。
public class Game {
private Player player;
private Language language;
private Random randomGenerator;
private List<String> dictionary;
private char[] selectedWord;
@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";
}
}