问题
I have migrated a Java-JSP-Tomcat WebApplication to Spring boot with Web MVC. I can run the application in the IDE (eclipse Mars 2). Everything is working fine. I've created a executable jar-File with embedded tomcat. The application starts without errors, but if I access a page, the tiles-framework throw an Exception because of missing .jsp file.
In other posts on SO I read, that there are special folders for static webcontent (/META-INF/resources/, /resources/, /static/, /public/). I've tried to use the static-folder, but in this case the jsp-content was not found, also when running in Eclipse.
The needed files does exist in the jar file. I've controlled this a lot of times.
Project structure:
pom.xml:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.example</groupId>
<artifactId>webapp</artifactId>
<version>2.0.0-SNAPSHOT</version>
<packaging>jar</packaging>
<name> WebApp</name>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.9.RELEASE</version>
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
</dependency>
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-jasper</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<!-- needed to run as service under linux (start|stop|status) -->
<executable>true</executable>
</configuration>
</plugin>
</plugins>
</build>
</project>
tiles.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE tiles-definitions PUBLIC
"-//Apache Software Foundation//DTD Tiles Configuration 3.0//EN"
"http://tiles.apache.org/dtds/tiles-config_3_0.dtd">
<tiles-definitions>
<definition name="public.definition" template="/jsp/frontend/public/layout/Layout.jsp">
<put-attribute name="title" value="WebApp" />
<put-attribute name="topNav" value="/jsp/frontend/public/layout/TopNav.jsp" />
<put-attribute name="header" value="/jsp/frontend/public/layout/Header.jsp" />
<put-attribute name="menu" value="/jsp/frontend/public/layout/MainMenu.jsp" />
<put-attribute name="body" value="" />
<put-attribute name="footer" value="/jsp/frontend/public/layout/Footer.jsp" />
</definition>
<definition name="singlePage.definition" template="/jsp/frontend/public/layout/LayoutSinglePage.jsp">
<put-attribute name="content" value="" />
</definition>
<definition name="privacySinglePage" extends="singlePage.definition">
<put-attribute name="content" value="/jsp/frontend/public/privacy.jsp" />
</definition>
<definition name="privacy" extends="public.definition">
<put-attribute name="body" value="/jsp/frontend/public/privacy.jsp" />
</definition>
<definition name="imprint" extends="public.definition">
<put-attribute name="body" value="/jsp/frontend/public/imprint.jsp" />
</definition>
...
</tiles-definitions>
Exception
org.springframework.web.util.NestedServletException: Request processing failed; nested exception is org.apache.tiles.request.render.CannotRenderException: Servl etException including path '/jsp/frontend/public/layout/Layout.jsp'. at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:982) ~[spring-webmvc-4.3.13.RELEASE.jar!/:4.3.13.RELEASE] at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:861) ~[spring-webmvc-4.3.13.RELEASE.jar!/:4.3.13.RELEASE] at javax.servlet.http.HttpServlet.service(HttpServlet.java:635) ~[tomcat-embed-core-8.5.23.jar!/:8.5.23] at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:846) ~[spring-webmvc-4.3.13.RELEASE.jar!/:4.3.13.RELEASE] at javax.servlet.http.HttpServlet.service(HttpServlet.java:742) ~[tomcat-embed-core-8.5.23.jar!/:8.5.23] at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:231) ~[tomcat-embed-core-8.5.23.jar!/:8.5.23] at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) ~[tomcat-embed-core-8.5.23.jar!/:8.5.23] at org.apache.catalina.core.ApplicationDispatcher.invoke(ApplicationDispatcher.java:728) ~[tomcat-embed-core-8.5.23.jar!/:8.5.23] at org.apache.catalina.core.ApplicationDispatcher.processRequest(ApplicationDispatcher.java:469) ~[tomcat-embed-core-8.5.23.jar!/:8.5.23] at org.apache.catalina.core.ApplicationDispatcher.doForward(ApplicationDispatcher.java:392) ~[tomcat-embed-core-8.5.23.jar!/:8.5.23] at org.apache.catalina.core.ApplicationDispatcher.forward(ApplicationDispatcher.java:311) ~[tomcat-embed-core-8.5.23.jar!/:8.5.23] at org.apache.catalina.core.StandardHostValve.custom(StandardHostValve.java:395) [tomcat-embed-core-8.5.23.jar!/:8.5.23] at org.apache.catalina.core.StandardHostValve.status(StandardHostValve.java:254) [tomcat-embed-core-8.5.23.jar!/:8.5.23] at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:177) [tomcat-embed-core-8.5.23.jar!/:8.5.23] at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:81) [tomcat-embed-core-8.5.23.jar!/:8.5.23] at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:87) [tomcat-embed-core-8.5.23.jar!/:8.5.23] at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:342) [tomcat-embed-core-8.5.23.jar!/:8.5.23] at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:803) [tomcat-embed-core-8.5.23.jar!/:8.5.23] at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:66) [tomcat-embed-core-8.5.23.jar!/:8.5.23] at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:868) [tomcat-embed-core-8.5.23.jar!/:8.5.23] at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1459) [tomcat-embed-core-8.5.23.jar!/:8.5.23] at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49) [tomcat-embed-core-8.5.23.jar!/:8.5.23] at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source) [na:1.8.0_144] at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source) [na:1.8.0_144] at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61) [tomcat-embed-core-8.5.23.jar!/:8.5.23] at java.lang.Thread.run(Unknown Source) [na:1.8.0_144] ...
Caused by: javax.servlet.ServletException: File [/jsp/frontend/public/layout/Layout.jsp] not found at org.apache.jasper.servlet.JspServlet.handleMissingResource(JspServlet.java:407) ~[tomcat-embed-jasper-8.5.23.jar!/:8.5.23] at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:374) ~[tomcat-embed-jasper-8.5.23.jar!/:8.5.23] at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:329) ~[tomcat-embed-jasper-8.5.23.jar!/:8.5.23] at javax.servlet.http.HttpServlet.service(HttpServlet.java:742) ~[tomcat-embed-core-8.5.23.jar!/:8.5.23] at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:231) ~[tomcat-embed-core-8.5.23.jar!/:8.5.23] at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) ~[tomcat-embed-core-8.5.23.jar!/:8.5.23]
回答1:
you can convert your project packaging to an executable war so that spring can locate your *.jsp
来源:https://stackoverflow.com/questions/47581352/spring-boot-with-tiles-static-webresources-not-found-when-deploying-as-jar