请承担我对春季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";
}
}
如果我需要更多信息更新问题,请告诉我
答案 0 :(得分:1)
您可以声明一个会话范围bean类,您可以在其中保留所有必需的属性。
当控制器中的bean为@Autowired
时,您可以从控制器的任何方法获取/设置所需的所有字段。
只需将游戏注释为@Component
并自动加密,而不是手动创建。