gpt4 book ai didi

java - Vaadin:将自定义 javascript 添加到引导页面

转载 作者:行者123 更新时间:2023-11-29 04:37:47 26 4
gpt4 key购买 nike

<分区>

我需要将 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:?]

在这种情况下获取上下文路径的正确方法是什么?

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