当前位置: 首页 > 知识库问答 >
问题:

我的Spring引导vaadin应用程序如何提供动态生成的超文本标记语言文件

萧和平
2023-03-14

我有一个spring boot vaadin应用程序,它在运行时根据用户输入生成HTML报告。我想将HTML文件保存在某个服务器目录中,并在浏览器中将其呈现给用户。我阅读了以下线程并尝试实现该解决方案:使用Spring Boot动态更改静态内容

我修改了应用程序类并添加了HTML\u DIR属性:

@SpringBootApplication
public class Applicationextends SpringBootServletInitializer {

    public static String HTML_DIR;

    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
        return application.sources(Application.class);
    }

    public static void main(String[] args) throws IOException {
        HTML_DIR = new File(".").getCanonicalPath() +"\\html_reports\\";
        SpringApplication.run(Application.class, args);
    }
}

然后,我添加了一个StaticResourceConfiguration,它将新的静态内容位置配置到我的服务器文件系统中的一个目录中:

public class StaticResourceConfiguration implements org.springframework.web.servlet.config.annotation.WebMvcConfigurer {

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry
            .addResourceHandler("/html_reports/**")
            .addResourceLocations("file:" + Application.HTML_DIR);
    }
}

我在服务器上的项目根目录(这是Application.HTML_DIR指向的地方)中创建了一个新的目录html_reports,并在这个目录中保存了一个名为test.html的报告。

但是当我打电话的时候http://my-server.com:8081/downloads/test.html我没有收到报告,但收到一条错误消息:

java.lang.IllegalAccessError: tried to access method org.jsoup.nodes.Node.<init>(Ljava/lang/String;)V from class com.vaadin.flow.router.RouteNotFoundError
    at com.vaadin.flow.router.RouteNotFoundError.routeToHtml(RouteNotFoundError.java:108) ~[flow-server-1.4.8.jar:1.4.8]
    at java.util.stream.ReferencePipeline$3$1.accept(ReferencePipeline.java:193) ~[na:1.8.0_121]
    at java.util.stream.SortedOps$SizedRefSortingSink.end(SortedOps.java:352) ~[na:1.8.0_121]
    at java.util.stream.AbstractPipeline.copyInto(AbstractPipeline.java:482) ~[na:1.8.0_121]
    at java.util.stream.AbstractPipeline.wrapAndCopyInto(AbstractPipeline.java:471) ~[na:1.8.0_121]
    at java.util.stream.ReduceOps$ReduceOp.evaluateSequential(ReduceOps.java:708) ~[na:1.8.0_121]
    at java.util.stream.AbstractPipeline.evaluate(AbstractPipeline.java:234) ~[na:1.8.0_121]
    at java.util.stream.ReferencePipeline.collect(ReferencePipeline.java:499) ~[na:1.8.0_121]
    at com.vaadin.flow.router.RouteNotFoundError.getRoutes(RouteNotFoundError.java:98) ~[flow-server-1.4.8.jar:1.4.8]
    at com.vaadin.flow.router.RouteNotFoundError.setErrorParameter(RouteNotFoundError.java:62) ~[flow-server-1.4.8.jar:1.4.8]
    at com.vaadin.flow.router.internal.ErrorStateRenderer.notifyNavigationTarget(ErrorStateRenderer.java:61) ~[flow-server-1.4.8.jar:1.4.8]
    at com.vaadin.flow.router.internal.AbstractNavigationStateRenderer.handle(AbstractNavigationStateRenderer.java:193) ~[flow-server-1.4.8.jar:1.4.8]
    at com.vaadin.flow.router.internal.ErrorStateRenderer.handle(ErrorStateRenderer.java:50) ~[flow-server-1.4.8.jar:1.4.8]
    at com.vaadin.flow.router.Router.handleExceptionNavigation(Router.java:261) ~[flow-server-1.4.8.jar:1.4.8]
    at com.vaadin.flow.router.Router.navigate(Router.java:196) ~[flow-server-1.4.8.jar:1.4.8]
    at com.vaadin.flow.router.Router.initializeUI(Router.java:92) ~[flow-server-1.4.8.jar:1.4.8]
    at com.vaadin.flow.server.BootstrapHandler.createAndInitUI(BootstrapHandler.java:1117) ~[flow-server-1.4.8.jar:1.4.8]
    at com.vaadin.flow.server.BootstrapHandler.synchronizedHandleRequest(BootstrapHandler.java:396) ~[flow-server-1.4.8.jar:1.4.8]
    at com.vaadin.flow.server.SynchronizedRequestHandler.handleRequest(SynchronizedRequestHandler.java:40) ~[flow-server-1.4.8.jar:1.4.8]
    at com.vaadin.flow.server.VaadinService.handleRequest(VaadinService.java:1507) ~[flow-server-1.4.8.jar:1.4.8]
    at com.vaadin.flow.server.VaadinServlet.service(VaadinServlet.java:242) ~[flow-server-1.4.8.jar:1.4.8]
    at com.vaadin.flow.spring.SpringServlet.service(SpringServlet.java:81) ~[vaadin-spring-11.0.0.jar:na]
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:741) ~[tomcat-embed-core-9.0.19.jar:9.0.19]
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:231) ~[tomcat-embed-core-9.0.19.jar:9.0.19]
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) ~[tomcat-embed-core-9.0.19.jar:9.0.19]
    at org.apache.catalina.core.ApplicationDispatcher.invoke(ApplicationDispatcher.java:712) ~[tomcat-embed-core-9.0.19.jar:9.0.19]
    at org.apache.catalina.core.ApplicationDispatcher.processRequest(ApplicationDispatcher.java:459) ~[tomcat-embed-core-9.0.19.jar:9.0.19]
    at org.apache.catalina.core.ApplicationDispatcher.doForward(ApplicationDispatcher.java:352) ~[tomcat-embed-core-9.0.19.jar:9.0.19]
    at org.apache.catalina.core.ApplicationDispatcher.forward(ApplicationDispatcher.java:312) ~[tomcat-embed-core-9.0.19.jar:9.0.19]
    at org.springframework.web.servlet.mvc.ServletForwardingController.handleRequestInternal(ServletForwardingController.java:141) ~[spring-webmvc-5.1.7.RELEASE.jar:5.1.7.RELEASE]
    at org.springframework.web.servlet.mvc.AbstractController.handleRequest(AbstractController.java:177) ~[spring-webmvc-5.1.7.RELEASE.jar:5.1.7.RELEASE]
    at org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter.handle(SimpleControllerHandlerAdapter.java:52) ~[spring-webmvc-5.1.7.RELEASE.jar:5.1.7.RELEASE]
    at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:1039) ~[spring-webmvc-5.1.7.RELEASE.jar:5.1.7.RELEASE]
    at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:942) ~[spring-webmvc-5.1.7.RELEASE.jar:5.1.7.RELEASE]
    at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:1005) ~[spring-webmvc-5.1.7.RELEASE.jar:5.1.7.RELEASE]
    at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:897) ~[spring-webmvc-5.1.7.RELEASE.jar:5.1.7.RELEASE]
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:634) ~[tomcat-embed-core-9.0.19.jar:9.0.19]
    at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:882) ~[spring-webmvc-5.1.7.RELEASE.jar:5.1.7.RELEASE]
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:741) ~[tomcat-embed-core-9.0.19.jar:9.0.19]
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:231) ~[tomcat-embed-core-9.0.19.jar:9.0.19]
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) ~[tomcat-embed-core-9.0.19.jar:9.0.19]
    at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53) ~[tomcat-embed-websocket-9.0.19.jar:9.0.19]
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) ~[tomcat-embed-core-9.0.19.jar:9.0.19]
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) ~[tomcat-embed-core-9.0.19.jar:9.0.19]
    at org.springframework.security.web.FilterChainProxy.doFilterInternal(FilterChainProxy.java:209) ~[spring-security-web-5.1.5.RELEASE.jar:5.1.5.RELEASE]
    at org.springframework.security.web.FilterChainProxy.doFilter(FilterChainProxy.java:178) ~[spring-security-web-5.1.5.RELEASE.jar:5.1.5.RELEASE]
    at org.springframework.web.filter.DelegatingFilterProxy.invokeDelegate(DelegatingFilterProxy.java:357) ~[spring-web-5.1.7.RELEASE.jar:5.1.7.RELEASE]
    at org.springframework.web.filter.DelegatingFilterProxy.doFilter(DelegatingFilterProxy.java:270) ~[spring-web-5.1.7.RELEASE.jar:5.1.7.RELEASE]
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) ~[tomcat-embed-core-9.0.19.jar:9.0.19]
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) ~[tomcat-embed-core-9.0.19.jar:9.0.19]
    at org.springframework.web.filter.RequestContextFilter.doFilterInternal(RequestContextFilter.java:99) ~[spring-web-5.1.7.RELEASE.jar:5.1.7.RELEASE]
    at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107) ~[spring-web-5.1.7.RELEASE.jar:5.1.7.RELEASE]
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) ~[tomcat-embed-core-9.0.19.jar:9.0.19]
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) ~[tomcat-embed-core-9.0.19.jar:9.0.19]
    at org.springframework.web.filter.FormContentFilter.doFilterInternal(FormContentFilter.java:92) ~[spring-web-5.1.7.RELEASE.jar:5.1.7.RELEASE]
    at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107) ~[spring-web-5.1.7.RELEASE.jar:5.1.7.RELEASE]
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) ~[tomcat-embed-core-9.0.19.jar:9.0.19]
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) ~[tomcat-embed-core-9.0.19.jar:9.0.19]
    at org.springframework.web.filter.HiddenHttpMethodFilter.doFilterInternal(HiddenHttpMethodFilter.java:93) ~[spring-web-5.1.7.RELEASE.jar:5.1.7.RELEASE]
    at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107) ~[spring-web-5.1.7.RELEASE.jar:5.1.7.RELEASE]
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) ~[tomcat-embed-core-9.0.19.jar:9.0.19]
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) ~[tomcat-embed-core-9.0.19.jar:9.0.19]
    at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:200) ~[spring-web-5.1.7.RELEASE.jar:5.1.7.RELEASE]
    at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107) ~[spring-web-5.1.7.RELEASE.jar:5.1.7.RELEASE]
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) ~[tomcat-embed-core-9.0.19.jar:9.0.19]
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) ~[tomcat-embed-core-9.0.19.jar:9.0.19]
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:200) ~[tomcat-embed-core-9.0.19.jar:9.0.19]
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:96) [tomcat-embed-core-9.0.19.jar:9.0.19]
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:490) [tomcat-embed-core-9.0.19.jar:9.0.19]
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:139) [tomcat-embed-core-9.0.19.jar:9.0.19]
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:92) [tomcat-embed-core-9.0.19.jar:9.0.19]
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:74) [tomcat-embed-core-9.0.19.jar:9.0.19]
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:343) [tomcat-embed-core-9.0.19.jar:9.0.19]
    at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:408) [tomcat-embed-core-9.0.19.jar:9.0.19]
    at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:66) [tomcat-embed-core-9.0.19.jar:9.0.19]
    at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:836) [tomcat-embed-core-9.0.19.jar:9.0.19]
    at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1747) [tomcat-embed-core-9.0.19.jar:9.0.19]
    at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49) [tomcat-embed-core-9.0.19.jar:9.0.19]
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142) [na:1.8.0_121]
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617) [na:1.8.0_121]
    at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61) [tomcat-embed-core-9.0.19.jar:9.0.19]
    at java.lang.Thread.run(Thread.java:745) [na:1.8.0_121]

为了测试,我将模式html_reports/**添加到我的Spring Security配置中以允许访问:

@Override
public void configure(WebSecurity web) throws Exception {
    web.ignoring().antMatchers("/html_reports/**");
}

我不明白我错过了什么。

我不确定,但也许有更好的方法来做我想要的(在浏览器中向用户显示运行时生成的超文本标记语言文件,最好是在新的选项卡或窗口中)。

亲切的问候

提莫

共有1个答案

百里伟
2023-03-14

您必须创建RequestMapping以防止Vaadin拦截URL。

@Controller or @RestController
@RequestMapping(value = "/html_reports")
public class HtmlReprotsController {
    ...
}
 类似资料:
  • 我正在使用ITextRenderer从html生成PDF。然而,我得到以下异常: 你能帮帮我吗? 任何帮助都将不胜感激。 先谢谢你。

  • 我试图在HTML的pre标签中包装文本,但它不起作用。我使用下面的CSS作为我的标签。 我从如何在pre标记中换行文本? 我已添加

  • 我使用Spring MVC 开发了一个 REST Web 应用程序,我可以将 JSON 对象发送到客户端。 我想构造一个连接到我的 Web 应用程序的脚本/AJAX 客户端,但我不知道如何发送第一个 HTML 页面(使用 JSP)。我知道我应该为JSP页面提供一些嵌入式AJAX。此 AJAX 将向我的 Web 服务发送请求。 更新:我无法实现的要求是在浏览器中编写默认URI(),并查看我在JSP页

  • 我在一个Web发布者那里有一个HTML页面,我想将其内容加载到我的Web应用程序中的JSP中。我尝试了JSP:Include操作和Include指令,但它们似乎都只采用相对路径。我无法拥有该html文件的本地副本,因为该文件可能会在将来发生更改,我需要动态包含它。如何使用绝对路径包含文件?

  • 我想在单击输入字段时触发一个处理程序,在取消选择输入字段时触发另一个处理程序(即,如果有人在字段外单击)。有没有办法做到这一点? 单击处理程序非常简单: 是否可以创建“取消单击”处理程序?

  • 我正在想办法完全自动化詹金斯的硒测试。 我的全部想法是:我想用Selenium IDE记录一个测试。然后将测试另存为HTML代码。将HTML文件放在test1中。和詹金斯一起在服务器上测试。Jenkins应该根据HTML文件的信息进行测试。 最后一句话现在是问题所在。我找不到一种简单的方法将HTML文件转换成可能的JUnit测试并运行它。 我找到了selenese4j,这个想法很好,但它只返回测试