java - Spring Mvc : getting session attributes accross different methods in the same Controller

标签 java spring forms spring-mvc session

请原谅我对 Spring MVC 的了解有限,我仍在尝试了解它是如何工作的。

我的问题如下:我正在开发一个简单的猜测游戏,您可以从下拉选择选项中选择一个字母,然后使用有关您猜测了多少次的更新信息刷新相同的 View 。

游戏(模型类)

public class Game {

    private Player player;
    private Language language;  
    private Random randomGenerator;

    private List<String> dictionary;
    private char[] selectedWord;    

及其各自的 getter/setter 等。

Controller 类:

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

}



}

如果我需要用更多信息更新问题,请告诉我

最佳答案

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

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

只需将游戏注释为 @Component 并 Autowiring 它,而不是手动创建它。

关于java - Spring Mvc : getting session attributes accross different methods in the same Controller,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43785286/

相关文章:

html - 单选按钮 css 和 IE9

java - 使用单选按钮和 jquery/Ajax 更新 div 与发布结果

javascript - 使用 JS 访问操作和值属性

java - 在java中将json转换为类时出现问题(谷歌自定义搜索)

java - Hibernate 搜索索引单个租户

spring - 云中的 Neo4j

java - 如何将 Spring Beans 自动添加到 TaskExecutor

java - 更改/覆盖 String.valueOf Java Android

java - 如何找到文本文件中最小的数字? ( java )

java - Spring boot - 使用 @PropertySource 记录到文件不起作用