我有 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/