静态资源

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,谨慎使用
@Configuration
public 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.jpghttp://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.
 */
@Controller
public 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这个目录下的文件。到这里我们就完成了一个简单的页面开发。

GitHub 加速计划 / sp / spring-boot
39
7
下载
spring-projects/spring-boot: 是一个用于简化Spring应用开发的框架。适合用于需要快速开发企业级Java应用的项目。特点是可以提供自动配置、独立运行和内置的Tomcat服务器,简化Spring应用的构建和部署。
最近提交(Master分支:2 个月前 )
cf1ecca8 See gh-48253 17 小时前
3e82ede6 Previously, spring-boot-restclient was a required dependency of spring-boot-resttestclient. This had the unwanted side-effect of increasing the risk of the test classpath enabling auto-configuration for RestClient.Builder when it was main code that needed such a bean. This could lead to integration tests passing but the application itself failing to start when its run through its main method. This commit makes spring-boot-restclient an optional dependency of spring-boot-resttestclient. As a result, a dependency on spring-boot-resttestclient is no longer sufficient to auto-configure a RestClient.Builder bean, although it is still sufficient to auto-configure a RestTestClient bean. Those that wish to use TestRestTemplate rather than migrating to RestTestClient will now have to add a dependency on spring-boot-restclient. This makes it presence more obvious. It now has to be declared directly rather than being somewhat hidden due to being pulled in transitively. The hope is that this will reduce the chances of the dependency being accidentially on the test classpath when main code requires it to be on the runtime classpath. Fixes gh-48253 17 小时前
Logo

新一代开源开发者平台 GitCode,通过集成代码托管服务、代码仓库以及可信赖的开源组件库,让开发者可以在云端进行代码托管和开发。旨在为数千万中国开发者提供一个无缝且高效的云端环境,以支持学习、使用和贡献开源项目。

更多推荐