请求和会话属性值在@ModelAttribute方法中返回null

时间:2013-12-06 07:25:38

标签: spring spring-mvc spring-annotations modelattribute

使用spring mvc我想访问@ModelAttribute方法中的请求属性,但它只给出null

@RequestMapping(value = "/abc", method = RequestMethod.GET, params = "data")
    public ModelAndView aaaa()
             {
        String courseId = httpServletRequest.getParameter("courseValue");
        System.out.println("course value data :" + courseId); // here am getting value

        httpServletRequest.setAttribute("courseId", courseId); // setting in request 
         attribute
        WebUtils.setSessionAttribute(httpServletRequest, "courseId", courseId);
        // setting in   session attribute
        ModelAndView modelAndView = new ModelAndView("abc");
        return modelAndView;
    }



   @ModelAttribute("termList")
   public Map<String, String> def(HttpServletRequest httpServletRequest) 
    {
    String courseId = (String) WebUtils
            .getSessionAttribute(httpServletRequest, "courseId");
    System.out.println("course value in term :" + courseId); // here its giving null
    Map<String, String>map = courseSubLinkService.getTermDetailsBasedOnCourseId
            (courseId);

    httpServletRequest.setAttribute("termList", map);
    return map;

   }

我不知道我哪里做错了请帮助我获得这个价值

1 个答案:

答案 0 :(得分:0)

来自Spring docs:

  

之前调用控制器中的@ModelAttribute方法   @RequestMapping方法,在同一个控制器中

这意味着在调用def的时刻,HttpServletRequest没有您需要的属性,因为您在aaaa方法中设置了此属性,该方法将在def之后调用。