spring-boot笔记-静态资源及页面开发(六)

来源:互联网 发布:卖好看的书包的淘宝店 编辑:程序博客网 时间:2024/06/05 05:55

静态资源

1.1:默认方式

在SpringBoot中加载静态资源和在普通的web应用中不太一样。静态资源(js、css、图片等资源)默认目录位置需置于classpath下,并且符合以下目录规则:

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

我们通过一个例子来看下,先来看一个目录结构:
这里写图片描述
我们在resources目录下新建一个目录static,其下面又有个image目录,static符合我们上面所说的springboot默认的静态资源目录,所以我们如果访问这个图片,就可以通过http://localhost:8080/image/timg.jpg。也就是说,上面那几个目录,都是静态资源的映射路径,优先级顺序为:META-INF/resources > resources > static > public
建议:直接使用Spring Boot的默认配置方式

1.2 修改默认方式

  1. 配置文件方式
# 默认值为 /**spring.mvc.static-path-pattern=# 默认值为 classpath:/META-INF/resources/,classpath:/resources/,classpath:/static/,classpath:/public/spring.resources.static-locations=这里设置要指向的路径,多个使用英文逗号隔开

比如我把spring.mvc.static-path-pattern=/demo/**,那么上面我们的那个图片就需要通过http://localhost:8080/demo/image/timg.jpg才能访问的到,如果我改了下面的locations,那么我图片依然在static就不能访问的到了。
2. WebMvcConfigurerAdapter
通过写一个类继承WebMvcConfigurerAdapter,重写其addResourceHandlers方法,可以增加一些资源路径的配置:

import org.springframework.context.annotation.Configuration;import org.springframework.util.ResourceUtils;import org.springframework.web.servlet.DispatcherServlet;import org.springframework.web.servlet.config.annotation.EnableWebMvc;import org.springframework.web.servlet.config.annotation.InterceptorRegistry;import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;/** * Created by gonghao on 2017/6/3. *///@EnableWebMvc :如果添加了该注解,则是完全控制MVC,谨慎使用@Configurationpublic class MyWebAppConfigurer extends WebMvcConfigurerAdapter{    @Override    public void addResourceHandlers(ResourceHandlerRegistry registry) {        registry.addResourceHandler("/myResource/**").addResourceLocations(ResourceUtils.CLASSPATH_URL_PREFIX+"/myResource/");        registry.addResourceHandler("/static/**").addResourceLocations(ResourceUtils.CLASSPATH_URL_PREFIX+"/static/");        super.addResourceHandlers(registry);    }}

这里我们没有使用@EnableWebMvc注解,所以我们这里是增加了一些资源路径的配置,而不是完全修改默认的资源路径配置。也就是说我增加了这个配置后:我通过http://localhost:8080/static/image/timg.jpg和http://localhost:8080/image/timg.jpg都可以访问到图片。

页面开发

springboot支持的动态模板引擎包括以下类型:

  • Thymeleaf
  • FreeMarker
  • Velocity
  • Groovy
  • Mustache

springboot对JSP的支持不是很好,应该避免使用JSP。
如果使用上述的几种模板引擎,默认的模板配置路径为:src/main/resources/templates, templates不要拼写错误。本文最上面有个截图中,我们已经在templates下面新建了一个index.html。我们来看下如何访问到该页面。
我们这里采用的是Thymeleaf,要记得引入pom文件。

<dependency>            <groupId>org.springframework.boot</groupId>            <artifactId>spring-boot-starter-thymeleaf</artifactId>        </dependency>

再看下index.html的内容:

<!DOCTYPE html><html xmlns:th="http://www.thymeleaf.org"><head>    <meta charset="UTF-8"/>    <title>Title</title></head><body>    <h1>Hello:</h1>    <h2><span th:text="${hello}"></span></h2>    <h2><span th:text="${hello2}"></span></h2>    <image th:src="@{/image/timg.jpg}"/>    <!--<img th:src="@{/myResource/timg.jpg}"/>--></body></html>

看到上面的写法你可能会有些奇怪,th:src和@{}这都是什么鬼。其实这是Thymeleaf的语法。@{}是引入外部资源用的。【不理解的同学自己去单独学习下】
再来看下我们的Controller代码:

import org.springframework.stereotype.Controller;import org.springframework.ui.ModelMap;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.servlet.ModelAndView;/** * Created by gonghao on 2017/6/15. */@Controllerpublic class IndexController {    @RequestMapping("/index")    public String index(ModelMap map) {        map.addAttribute("hello2", "hello2 Thymeleaf!");        map.addAttribute("hello", "hello Thymeleaf!");        return "index";    }}

注意这里不再是@RestController了,而是@Controller。这种写法跟我们的springMVC是一模一样的,所以我们就可以通过 http://localhost:8080/index 就可以访问到我们的页面。因为SpringBoot集成了Thymeleaf,所以它会默认查找resources下面的templates这个目录下的文件。到这里我们就完成了一个简单的页面开发。

原创粉丝点击