guigui42
guigui42

Reputation: 2500

how to display an object on jsp page?

i m pretty new to Spring MVC, i m trying to setup a page to display user information

I have trouble with the controler and the view.

Controler (getDetail returns a User object, it has an email field) :

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

in 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>

But i get this error when i go on the page :

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

I am using Spring 3, on Tomcat6

So i hope you can tell me what i am doing wrong ...

Thank you

Upvotes: 0

Views: 11654

Answers (1)

skaffman
skaffman

Reputation: 403611

ModelMap.addAttribute() does not permit the attribute value to be null, and will throw an IllegalArgumentException if it is.

Your controller needs to check whether the result of simpleUserManager.getDetail(code) returns a null, and only try and render the result if it's not. If it is null, you need to do something appropriate to that situation.

Upvotes: 5

Related Questions