Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java SpringMVC,更新表单上的模型映射仍然显示旧信息_Java_Spring_Spring Mvc_Model - Fatal编程技术网

Java SpringMVC,更新表单上的模型映射仍然显示旧信息

Java SpringMVC,更新表单上的模型映射仍然显示旧信息,java,spring,spring-mvc,model,Java,Spring,Spring Mvc,Model,请接受我对spring mvc有限的了解。 我正在尝试在POST请求后使用更新的模型信息刷新视图。新信息和旧版本都会出现在视图上,这会完全扰乱视图 这是我的控制器代码。如果你看到一些不正确的东西,请让我知道,提前谢谢 @Controller @SessionAttributes({"Game"}) public class SimpleController { @Autowired private SessionLocaleResolver localeResolver;

请接受我对spring mvc有限的了解。 我正在尝试在POST请求后使用更新的模型信息刷新视图。新信息和旧版本都会出现在视图上,这会完全扰乱视图

这是我的控制器代码。如果你看到一些不正确的东西,请让我知道,提前谢谢

@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("LoginBean", 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, 
            BindingResult result, ModelMap model) {


        this.loginValidator.validate(loginBean, result);

        if (result.hasErrors()) {
            return "redirect:/login.htm";
        }

        Game game = new Game();     

        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,
            @ModelAttribute("game") Game game, ModelMap model){         

        ModelAndView mav = new ModelAndView("/views/index.jsp");

        if(request.getSession().getAttribute("game") != null) {
            game = (Game)request.getSession().getAttribute("game");
            mav.addObject("game", game);        
        } else {
            System.out.println("nothing received");
        }

        request.getSession().setAttribute("game", game);

        return mav;
    }

    @RequestMapping (value="/guessLetter", method=RequestMethod.POST)
    public ModelAndView guessLetter (HttpServletRequest request, HttpServletResponse response, 
            @ModelAttribute("game") Game game, @RequestParam("guess") String guess,
            SessionStatus status, ModelMap model) {

        ModelAndView mav = new ModelAndView("/views/index.jsp");

        if(request.getSession().getAttribute("game") != null) {
            game = (Game)request.getSession().getAttribute("game");

            game.guessLetter(guess);

        } else {
            System.out.println("no guess!");
        }


        //model.replace("game", game); tried this, but didn't work
        System.out.println(model.entrySet());
        model.addAttribute("Game", game);
        //request.getSession().setAttribute("game", game);  

        mav.addObject("game", game);
        status.setComplete();

        return mav;

    }


}

结果我没有完全理解SessionStatus方法。这个答案真的很有帮助

以下是我的工作控制器,以防任何人需要它作为参考:

@RequestMapping (value="/guessLetter", method=RequestMethod.POST)  
        public ModelAndView guessLetter (HttpServletRequest request, HttpServletResponse response, 
                @ModelAttribute("game") Game game, @RequestParam("guess") String guess,
                SessionStatus status, ModelMap model, BindingResult result) {
            System.out.println(guess);

            this.letterValidator.validate(game, result);

            if (result.hasErrors()) {
                return new ModelAndView("/views/index.jsp");
            }    

            request.getSession().setAttribute("game", game);        
            game.guessLetter(guess);        

            System.out.println(game.getGuessList());

            ModelAndView mav = new ModelAndView("redirect:index.htm");

            mav.addObject("game", game);
            model.addAttribute("game", game);
            return mav;
        }