weixin_42502696
cpf2333
2012-12-16 16:48

springMVC后台的值无法通过ModelAndView的addObject传到前台

已采纳

我将studentList放入request作用域中jsp可以访问,为什么通过ModelAndView的addObject方法放入后,前台就无法访问。

后台方法:
@RequestMapping("/studentList")
public ModelAndView studentList(HttpServletRequest request) throws Exception{
ModelAndView mav = new ModelAndView("studentList");
// List 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;
}
前台代码

${xx}












/c:forEach
id name birthday creatTime
${student.id} ${student.name} ${student.birthday} ${student.createTime}

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答

1条回答

  • jinnianshilongnian jinnianshilongnian 9年前

    按照标准用法的话应该是没问题
    内部其实把model 通过request.setAttribute(modelName, modelValue); 放入的
    [code="java"] protected void exposeModelAsRequestAttributes(Map model, HttpServletRequest request) throws Exception {
    for (Map.Entry 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() + "'");
    }
    }
    }
    }[/code]

    一个可能是你实现了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

    如果方便可以贴全代码,或者把代码站内信我

    点赞 评论 复制链接分享

相关推荐