springboot整合freemarker,打成jar包访问不了webapp下的资源

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的错(感谢博主cungirl提醒,上文第九行代码META-INF错写成了MATE-INF,修改过来即可),查看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模板应该有更简单、更灵活的方法,可以去学习下

©️2020 CSDN 皮肤主题: 大白 设计师:CSDN官方博客 返回首页