【发布时间】:2016-11-10 13:14:39
【问题描述】:
我需要将 jquery 添加到我的 vaadin 应用程序的引导页面。我在我的自定义 Servlet 类中添加了以下内容
@Override
public void sessionInit(SessionInitEvent event) throws ServiceException {
event.getSession().addBootstrapListener(new BootstrapListener() {
@Override
public void modifyBootstrapPage(BootstrapPageResponse response) {
//Jquery is necessary for open in new window button
String contextPath = event.getRequest().getContextPath();
response.getDocument().head().prependElement("script").attr("type", "text/javascript").attr("src", contextPath + "/VAADIN/js/jquery.min.js");
response.getDocument().head().prependElement("script").attr("type", "text/javascript").attr("src", contextPath + "/VAADIN/js/jquery-ui.js");
}
@Override
public void modifyBootstrapFragment(BootstrapFragmentResponse response) {
}
});
}
此代码有时会在event.getRequest().getContextPath(); 中引发空指针异常
Caused by: java.lang.NullPointerException
at org.apache.catalina.connector.Request.getServletContext(Request.java:1598) ~[catalina.jar:8.0.24]
at org.apache.catalina.connector.Request.getContextPath(Request.java:1915) ~[catalina.jar:8.0.24]
at org.apache.catalina.connector.RequestFacade.getContextPath(RequestFacade.java:783) ~[catalina.jar:8.0.24]
at javax.servlet.http.HttpServletRequestWrapper.getContextPath(HttpServletRequestWrapper.java:150) ~[servlet-api.jar:?]
at com.mycompany.MyServlet$1.modifyBootstrapPage(YaanServlet.java:56) ~[web-yaan-ui-base-1.3.39.jar:?]
在这种情况下获取上下文路径的正确方法是什么?
【问题讨论】: