JSP does not throw a NullPointerException

I have a controller:

@RequestMapping(method = RequestMethod.GET) public String getViewRailwayService(@RequestParam long id, Model model) { model.addAttribute("railwayService",railwayServiceRepository.findOne(id)); return "admin/railwayService/view"; } 

and jsp:

 ... <title>${railwayService.name}</title> <c:forEach var="company" items="${railwayService.companies}"> ... 

It works fine, but I'm confused when railwayServiceRepository.findOne(id) return null NullPointerException does not throw.

+6
source share
2 answers

Not sure if fooobar.com/tags/el / ... is a reliable link (I tried to find it in the official specifications until I got lucky), but

EL depends on the JavaBeans specification when it comes to accessing properties. In JSP, the following expression:

${user.name}

has basically the same thing as in the "source" scriptlet code (the example below is for simplicity, in fact, the reflection API is used to get methods and call them):

 <% User user = (User) pageContext.findAttribute("user"); if (user != null) { String name = user.getName(); if (name != null) { out.print(name); } } %> 

(...) Note that it does not print “null” when the value is null or does not throw a NullPointerException , as opposed to using scripts. In other words, EL is zero.

+6
source

In model.addAttribute("railwayService",railwayServiceRepository.findOne(id));

when the value is null, it will not throw a NullPointerException , it will just pass the value as null to the view.

-2
source

Source: https://habr.com/ru/post/983376/


All Articles