2

I am working on a Spring 3 project and always check if a @ModelAttribute is null, if so I redirect the user to an error page.

@Controller
@SessionAttributes({"myCommand"})
public class MyController {
  @ModelAttribute("myCommand")
  public MyCommand populate(HttpServletRequest request) {
    return new MyCommand();
  }
  @RequestMapping(value="/user/saveFoo", method=RequestMethod.POST)
  public String saveFoo(HttpServletRequest request, @ModelAttribute("myCommand") MyCommand myCommand) {
    if(myCommand == null) {
      // Add invalid session error here.
      return "error.htm";
    }
    ...
    ...
  }
}

What I want to know is if this check is necessary. The command is a session attribute and it is created using populate method of the controller when necessary. Thus the command can never be null as long as the session is alive.

What I don't know is what happens when the session is expired. Does the controller create the model attribute again? If so then the command object can never be null regardless of the state of the session.

Thanks for any help.

Bahattin Ungormus
  • 628
  • 1
  • 9
  • 23
  • I believe if the `@ModelAttribute` is coming from session and you don'd have an `@ModelAttribute` method to create a `MyCommand` and your session has timed out then it would be possible for it to be `null`. But, it's an easy test to see. Open your app, get to the point right before you hit this handler and let it sit for, say, an hour or whatever you have your session timeout set for. The proceed and see if you get a null there (might want to put a SOP there so you know it was null). – CodeChimp Jul 04 '14 at 11:07

2 Answers2

1

Spring MVC creates for all method parameter new instance minimizes the potential risk of null pointer exception in your controller methods! But beware, this doesn't apply for wrapper types and classes with custom HandlerMethodArgumentResolver which can yield null as return value.

To answer your question, Spring MVC instanciates a new instance of your model class even the session expired. It can never be null!

Quote from ModelAttributeMethodProcessor javadoc:

Model attributes are obtained from the model or if not found possibly created with a default constructor if it is available.

If no default constructor is available Spring MVC throws a BeanInstantiationException exception

org.springframework.beans.BeanInstantiationException: Could not instantiate bean class [entity.Product]: No default constructor found

You can remove all null pointer checks safely.

For a overview of what Spring MVC can handle in controller methods by default you should take a look at code and javadoc in spring-web artefact package org.springframework.web.method.annotation and spring-webmvc artefact in package org.springframework.web.servlet.mvc.method.annotation.

ksokol
  • 8,035
  • 3
  • 43
  • 56
0

Actually you can have the following:

@ModelAttribute("isNull")
public T returnNull() {
    return null;
}

@RequestMapping("/")
public String go(@ModelAttribute("isNull") T isNull) {
    return isNull ? "isNull" : "isNotNull";
}

and isNotNull view will be returned.

Adrian Ber
  • 20,474
  • 12
  • 67
  • 117