2017-05-04 5 views
0

私の限られた春MVCの知識を持ち、それがどのように動作するかを学んでください。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クラスを宣言することができますいくつかの詳細情報

+1

'@ModelAttribute(" Game ")'は単に@SessionAttributes( "game")の名前と一致しないため、セッションには格納されず、セッションから取得されません。 –

+0

@M.Deinumそれでした!ありがとうございました!あなたが答えとして投稿するなら、私は正しいものとしてそれをチェックします。 –

+0

答えの性質上、私は質問の閉鎖に投票しました。これは単なる入力ミスであったためです。 –

答えて

1

との質問を更新する必要がある場合は私に知らせてください。

コントローラ内のBeanが@Autowiredである場合、必要なすべてのフィールドをコントローラの任意のメソッドから取得または設定できます。

ゲームには@Componentと注釈を付けて手動で作成するのではなく、自動で作成してください。

関連する問題