问题描述
我将studentList放入request作用域中jsp可以访问,为什么通过ModelAndView的addObject方法放入后,前台就无法访问。后台方法:@RequestMapping("/studentList")public ModelAndView studentList(HttpServletRequest request) throws Exception{ModelAndView mav = new ModelAndView("studentList");//List<Student> studentList = iStudentService.queryAllStudent();//System.out.println(studentList.size());//request.setAttribute("studentList", studentList);mav.addObject("studentList", iStudentService.queryAllStudent());mav.addObject("xx", "xx");//mav.addObject("studentList",studentList);return mav;}前台代码<body>${xx}<table border="1"><tr><td>id</td><td>name</td><td>birthday</td><td>creatTime</td></tr><c:forEach items="${studentList}" var="student"><tr><td>${student.id}</td><td>${student.name}</td><td>${student.birthday}</td><td>${student.createTime}</td></tr></c:forEach></table></body>
解决方案
按照标准用法的话应该是没问题内部其实把model 通过request.setAttribute(modelName, modelValue); 放入的protected void exposeModelAsRequestAttributes(Map<String, Object> model, HttpServletRequest request) throws Exception {for (Map.Entry<String, Object> entry : model.entrySet()) {String modelName = entry.getKey();Object modelValue = entry.getValue();if (modelValue != null) {request.setAttribute(modelName, modelValue);if (logger.isDebugEnabled()) {logger.debug("Added model object '" + modelName + "' of type [" + modelValue.getClass().getName() +"] to request in view with name '" + getBeanName() + "'");}}else {request.removeAttribute(modelName);if (logger.isDebugEnabled()) {logger.debug("Removed model object '" + modelName +"' from request in view with name '" + getBeanName() + "'");}}}}一个可能是你实现了HandlerInterceptor并实现了:void postHandle(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response, java.lang.Object o, org.springframework.web.servlet.ModelAndView modelAndView) throws java.lang.Exception;在此处修改了modelAndView如果方便可以贴全代码,或者把代码站内信我