0

我正在使用 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;

   }

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

4

1 回答 1

0

来自 Spring 文档:

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

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

于 2013-12-06T08:14:06.940 回答