微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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.

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐