tags:

views:

67

answers:

2

Hi, 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

+2  A: 

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.

skaffman
and what is the best way to handle null object in the controler? should i redirect to another page with an error message on it? or create an empty user and handle this in the JSP ? to check for empty ID and display error message...
guigui42
@guigui42: That's entirely up to you, and what makes sense for your application. Simplest thing would be to return the name of an error JSP, instead of the `userdetail`.
skaffman
I created an error.jsp and changed the code to if (!usr.equals(null)) { model.addAttribute("user",usr); return "userdetail"; } else { model.addAttribute("error","User does not exist !!!"); return "error"; }, but i still get NullPointerException when the user doesnt exist
guigui42
A: 

@guigui42: try to correct your condition from !usr.equals(null) to usr != null

php-coder