springboot項目在idea本地是可以正常運行的,在maven打成jar,在Linux服務器上運行時訪問webapp下的靜態文件html時報404的錯誤
目錄如下
發現jar包中沒有打包webapp下的文件,在pom文件中build標籤加入resources標籤,如下
<properties>
<main.basedir>${basedir}/../..</main.basedir>
<m2eclispe.wtp.contextRoot>/</m2eclispe.wtp.contextRoot>
</properties>
<bulid>
<resources>
<resource>
<directory>${basedir}/src/main/webapp</directory>
<targetPath>MATE-INF/resources</targetPath>
<includes>
<include>**/**</include>
</includes>
</resource>
<resource>
<directory>${basedir}/src/main/resources</directory>
<includes>
<include>**/**</include>
</includes>
</resource>
<resource>
<directory>${basedir}/src/main/java</directory>
<includes>
<include>**/*.xml</include>
</includes>
<filtering>false</filtering>
</resource>
</resources>
<build>
再次打包運行還是報404的錯,查看freemarker配置
<!-- 加載freeMaker配置 -->
<bean id="freemarkerConfiguration" class="org.springframework.beans.factory.config.PropertiesFactoryBean">
<property name="location" value="classpath:freemarker.properties"/>
</bean>
<!-- 配置freeMarker的模板路徑 -->
<bean id="freemarkerConfig"
class="utry.ivs.utils.FreeMarkerConfigurer">
<property name="freemarkerSettings" ref="freemarkerConfiguration" />
<property name="templateLoaderPath" value="/WEB-INF/views/html/"/>
</bean>
<!-- freemarker視圖解析器 -->
<bean id="viewResolver"
class="org.springframework.web.servlet.view.freemarker.FreeMarkerViewResolver">
<property name="cache" value="true" />
<property name="suffix" value=".html" />
<property name="viewClass" value="org.springframework.web.servlet.view.freemarker.FreeMarkerView" />
<property name="exposeSpringMacroHelpers" value="true" />
<property name="exposeRequestAttributes" value="true" />
<property name="exposeSessionAttributes" value="true" />
<property name="requestContextAttribute" value="rc" />
<property name="contentType" value="text/html;charset=UTF-8" />
</bean>
由於項目打包時我把webapp文件下的資源複製到了MATE-INF/resources下了,路徑訪問時還是去找配置好的模板路徑,所以路徑要修改,改成一下路徑
<!-- 配置freeMarker的模板路徑 -->
<bean id="freemarkerConfig"
class="utry.ivs.utils.FreeMarkerConfigurer">
<property name="freemarkerSettings" ref="freemarkerConfiguration" />
<property name="templateLoaderPath" value="classpath:MATE-INF/resources/WEB-INF/views/html/"/>
</bean>
springboot整合freemarker模板應該有更簡單、更靈活的方法,可以去學習下