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

gui*_*i42 0 jsp spring-mvc

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

我对控制器和视图有困难.

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

 @RequestMapping("/{code}")
 public String get(@PathVariable long code,ModelMap model) throws Exception {
  model.addAttribute("user",simpleUserManager.getDetail(code));
  return "userdetail";
 }
Run Code Online (Sandbox Code Playgroud)

在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>
Run Code Online (Sandbox Code Playgroud)

但是当我进入页面时,我收到此错误:

Request processing failed; nested exception is java.lang.IllegalArgumentException: Attribute value must not be null
Run Code Online (Sandbox Code Playgroud)

我在Tomcat6上使用Spring 3

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

谢谢

ska*_*man 5

ModelMap.addAttribute()不允许属性值null,IllegalArgumentException如果是,则抛出一个属性值.

您的控制器需要检查simpleUserManager.getDetail(code)返回的结果是否为a null,并且只检查结果,如果不是则.如果 null,您需要做一些适合这种情况的事情.