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模板应该有更简单、更灵活的方法,可以去学习下

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
Freemarker是一种模板引擎,可以将数据和模板进行整合生成输出内容。SpringBoot提供了对Freemarker的支持,可以很方便地整合Freemarker。 1. 添加依赖 在pom.xml文件中添加以下依赖: ```xml <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-freemarker</artifactId> </dependency> ``` 2. 配置文件 在application.properties文件中添加以下配置: ```properties spring.freemarker.template-loader-path=classpath:/templates/ spring.freemarker.cache=false ``` - template-loader-path:模板文件的路径,这里设置为classpath:/templates/,表示在项目的classpath下的templates目录中查找模板文件。 - cache:是否开启模板缓存,这里设置为false,表示关闭缓存。 3. 创建模板文件 在classpath:/templates/目录下创建一个名为index.ftl的模板文件,内容如下: ```html <!DOCTYPE html> <html> <head> <title>SpringBoot整合Freemarker</title> </head> <body> <h1>${message}</h1> </body> </html> ``` 4. 创建控制器 创建一个名为IndexController的控制器,代码如下: ```java @Controller public class IndexController { @RequestMapping("/") public String index(Model model) { model.addAttribute("message", "Hello, World!"); return "index"; } } ``` 该控制器中,使用@RequestMapping注解指定了请求路径为/,并将一个名为message的属性值设置为“Hello, World!”,然后返回了index作为视图名称。由于配置了spring.freemarker.template-loader-path=classpath:/templates/,所以SpringBoot会在classpath:/templates/目录下查找名为index的模板文件,并将模板文件中的${message}替换为“Hello, World!”。 5. 运行程序 启动应用程序,访问http://localhost:8080/,可以看到页面中显示了“Hello, World!”的字样。
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值