java – JSP没有抛出NullPointerException

前端之家收集整理的这篇文章主要介绍了java – JSP没有抛出NullPointerException前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

我有控制器:

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

和jsp页面

...

它运行正常,但我很困惑,当railwayServiceRepository.findOne(id)返回null时,NullPointerException不会抛出.

最佳答案
不确定StackOverflow wiki on Expression Language是否值得信赖(我一直试图在官方规格中找到它,但还没有运气),但是:

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

${user.name}

does basically the same as the following in “raw” scriptlet code (the below example is for simplicity,in reality the reflection API is used to obtain the methods and invoke them):

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

(…) Please note that it thus doesn’t print “null” when the value is null nor throws a NullPointerException unlike as when using scriptlets. In other words,EL is null-safe.

原文链接:https://www.f2er.com/spring/432292.html

猜你在找的Spring相关文章