springboot学习入门简易版四---springboot2.0静态资源访问及整合freemarker视图层

 

 

 

2.4.4 SpringBoot静态资源访问(9

Springboot默认提供静态资源目录位置需放在classpath下,目录名需要符合如下规则

/static  /public  /resources  /META-INF/resources

可以在src/main/resources目录下创建static,在该位置放置一个图片文件。

启动程序后,尝试访问http://localhost:8080/D.JPG,如能显示图片,配置成功。

2.5 SpringBoot整合freemarker视图层(10

 项目结构

1 pom文件

<!-- spring-boot-starter-parent 整合第三方常用框架依赖信息(包含各种依赖信息) -->
  <parent>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-parent</artifactId>
      <version>2.0.0.RELEASE</version>
  </parent>
  
  <!-- spring-boot-starter-web springboot整合springmvc web 
      实现原理:maven依赖继承关系,相当于把第三方常用maven依赖信息,在parent项目中已封装
  -->
  <dependencies>
      <!-- 根据需要选择parent中封装的第三方框架 -->
      <dependency>
          <groupId>org.springframework.boot</groupId>
          <artifactId>spring-boot-starter-web</artifactId>
          <!-- 不需要写版本号,因为在parent中已封装好版本号 -->
      </dependency>
      
      <!-- freemarker依赖包 -->
      <dependency>
          <groupId>org.springframework.boot</groupId>
          <artifactId>spring-boot-starter-freemarker</artifactId>
      </dependency>
  </dependencies>

2创建controller

@Controller //注意:不能使用@RestController(没有视图层),否则无法找到对应的ftl模板,类似jsp
public class FtlController {

    @RequestMapping("/ftlIndex")
    public String ftlIndex(Map<String, Object> map) {
        map.put("name", "test");//相当于request.setName()
        map.put("age", "2");
        return "ftlIndex";//自动查找对应的页面,类似jsp
    }
    
}

3创建ftl模板

必须在templates目录下,且ftl文件名和controller类中的return "ftlIndex"名一致才能匹配。

模板内容如下:${name}====${age}

4启动项目并访问:

 

http://localhost:8080/ftlIndex

页面显示:freemarker页面 test====2 

原文地址:https://www.cnblogs.com/cslj2013/p/10851989.html