gpt4 book ai didi

java - JSP 不抛出 NullPointerException

转载 作者:塔克拉玛干 更新时间:2023-11-03 04:32:16 25 4
gpt4 key购买 nike

我有 Controller :

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

和jsp页面:

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

它工作正常,但我很困惑,当 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.

关于java - JSP 不抛出 NullPointerException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28877772/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com