使用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;
}
我不知道我哪里做错了请帮助我获得这个价值
答案 0 :(得分:0)
来自Spring docs:
之前调用控制器中的@ModelAttribute方法 @RequestMapping方法,在同一个控制器中
这意味着在调用def
的时刻,HttpServletRequest没有您需要的属性,因为您在aaaa
方法中设置了此属性,该方法将在def
之后调用。