很久沒有寫過代界面的東西,突然有需求需要做一個(gè)簡(jiǎn)單的 JSP再悼,然后發(fā)現(xiàn)打包后運(yùn)行訪問的時(shí)候訪問總是404,查了一下解決了這個(gè)問題,順序記錄一下欢嘿。
先說原因,打包一定要使用 1.4.2.RELEASE 這個(gè)打包插件炼蹦,2.x 的都不行羡宙。
application 配置
# jsp 配置
spring.mvc.view.prefix=/WEB-INF/page/
spring.mvc.view.suffix=.jsp
在 src/main 下建立一個(gè) webapp 目錄 跟 resources 平級(jí), 下面建立 webapp/WEB-INF 目錄掐隐。 測(cè)試, 不管是 IDEA 起, 還是java -jar 起, 都可以啟動(dòng), 只需要 webapp/WEB-INF/page 這三個(gè)目錄即可,
不需要 webapp/MATE-INF/resources/WEB-inf/page
POM 配置 , 注意 反復(fù)驗(yàn)證 <resources>
標(biāo)簽是必須的, 否則啟動(dòng)后就是 404 訪問不到, 打包插件必須是 1.4.2.RELEASE
這個(gè)是個(gè)大坑, 搞了好久才搞定, 2.x 以上的版本都不行虑省。
pom 配置
<!--jsp-->
<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>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>1.4.2.RELEASE</version>
<executions>
<execution>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
<resources>
<!-- 打包時(shí)將jsp文件拷貝到META-INF目錄下-->
<resource>
<!-- 指定resources插件處理哪個(gè)目錄下的資源文件 -->
<directory>src/main/webapp</directory>
<!--注意此次必須要放在此目錄下才能被訪問到-->
<targetPath>META-INF/resources</targetPath>
<includes>
<include>**/**</include>
</includes>
</resource>
<resource>
<directory>src/main/resources</directory>
<includes>
<include>**/**</include>
</includes>
<filtering>false</filtering>
</resource>
</resources>
</build>