请忍受我有限的Spring MVC知识,并且仍在尝试了解其工作原理。

我的问题如下:我正在开发一个简单的猜测游戏,在该游戏中,您将从下拉选择选项中选择一个字母,并使用有关您做出的猜测的更新信息刷新同一视图。

游戏(模型类)

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";

}



}


请让我知道是否需要更多信息来更新问题

最佳答案

您可以声明一个会话范围Bean类,在其中可以保留所有必要的属性。

当bean在控制器中为@Autowired时,您可以从控制器的任何方法获取/设置所需的所有字段。

只需将游戏注释为@Component并自动装配,而不是手动创建即可。

09-30 15:12