如何在jsp页面上显示对象?

时间:2010-09-25 14:56:32

标签: jsp spring-mvc

我对Spring MVC很新,我试图设置一个页面来显示用户信息

我在控制器和视图方面遇到了麻烦。

Controler(getDetail返回一个User对象,它有一个电子邮件字段):

 @RequestMapping("/{code}")
 public String get(@PathVariable long code,ModelMap model) throws Exception {
  model.addAttribute("user",simpleUserManager.getDetail(code));
  return "userdetail";
 }
在userdetail.jsp中:

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%@ taglib uri="http://www.springframework.org/tags/form" prefix="form"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn"%>

<html>
  <head><title><fmt:message key="title"/></title></head>
  <body>
User Detail :
${user.email}
  </body>
</html>

但是当我进入页面时,我得到了这个错误:

Request processing failed; nested exception is java.lang.IllegalArgumentException: Attribute value must not be null

我在Tomcat6上使用Spring 3

所以我希望你能告诉我我做错了什么......

谢谢

1 个答案:

答案 0 :(得分:5)

ModelMap.addAttribute()不允许属性值为null,如果属性值为IllegalArgumentException则会抛出。{/ p>

您的控制器需要检查simpleUserManager.getDetail(code)的结果是否返回null,并且只检查结果(如果不是)。如果 null,则需要针对该情况采取适当的措施。