加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

java – JSP没有抛出NullPointerException

发布时间:2020-12-15 01:30:46 所属栏目:大数据 来源:网络整理
导读:我有控制器: @RequestMapping(method = RequestMethod.GET)public String getViewRailwayService(@RequestParam long id,Model model) { model.addAttribute("railwayService",railwayServiceRepository.findOne(id)); return "admin/railwayService/view";

我有控制器:

@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.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读