Spring Boot Web的相关配置

Spring Boot 提供了一下两个类来配置 WEB配置

org.springframework.boot.autoconfigure.web.WebMvcAutoConfiguration
org.springframework.boot.autoconfigure.web.WebMvcProperties

具体查看源码。

1、自动配置 ViewResolver

1)ContentNegotiatingViewResolver:是Spring MVC 提供的一个特殊的 ViewResolver,它不是自己处理 View,而是代理不同的 ViewResolver 来处理不同的 View,具有最高优先级。

2)BeanNameViewResolver:在控制器(@Controller)中的一个方法的返回值的字符串(试图名)会根据 BeanNameViewResolver 去查找 Bean 的名称为返回字符串的 View 来渲染视图。示例:

 

package com.example.controller;

import com.example.model.Person;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.view.BeanNameViewResolver;
import org.springframework.web.servlet.view.json.MappingJackson2JsonView;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2017/12/21.
 */
@Configuration
@Controller
@SpringBootApplication
public class PersonApplication {

    @Bean
    public BeanNameViewResolver beanNameViewResolver() {
        BeanNameViewResolver resolver = new BeanNameViewResolver();
        return resolver;
    }

    @Bean
    public MappingJackson2JsonView jsonView() {
        MappingJackson2JsonView jsonView = new MappingJackson2JsonView();
        return jsonView;
    }

    @RequestMapping(value = "/json", produces = {MediaType.APPLICATION_JSON_VALUE})
    public String json(Model model) {
        Person person = new Person("aa", 11);
        model.addAttribute("person", person);
        return "jsonView";
    }

    @RequestMapping("/")
    public String index(Model model) {
        Person person = new Person("aa", 11);
        List<Person> people = new ArrayList<>();
        people.add(new Person("xx", 11));
        people.add(new Person("yy", 22));
        people.add(new Person("zz", 33));

        model.addAttribute("person", person);
        model.addAttribute("people", people);
        return "index";
    }

    public static void main(String[] args) {
        SpringApplication.run(PersonApplication.class, args);
    }
}

模板代码:

 

<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8"></meta>
    <title>Title</title>
</head>
<body>
<p th:text="${person.name}"></p>
<p th:text="${person.age}"></p>
<p th:each="person:${people}">
    <span th:text="${person.name}"></span>
    <span th:text="${person.age}"></span>
    <button th:onclick="'getName(\''+${person.name}+'\');'">获得名字</button>
</p>
<script th:inline="javascript">
    var people = [[${person}]];
    console.log(people.name);

    function getName(name) {
        console.log(name);
    }
</script>
</body>
</html>

 

3)org.springframework.web.servlet.view.InternalResourceViewResolver

注:这是一个很常用的 ViewResolver,主要通过设置前缀、后缀,以及控制器中方法来返回视图名的字符串,以得到实际页面。具体可看源码。

2、自动配置的静态资源

在自动配置类的 addResourceHandles 方法中定义了静态资源的自动配置。

1)类路径文件:把类路径下的 /static、/public、/resources 和 /META-INF/resources 文件夹下的静态文件直接映射为 /**,可以通过 http://localhost:8080/** 来访问

2)webjar:关于webjar的介绍可以访问 http://www.webjars.org 网站

把 webjar 的 /META-INFO/resources/webjars/ 下的静态文件映射为 /webjars/** 可以通过 http://localhost:8080/webjars/** 来访问。

3、自动配置 Formatter 和 Converter(可查看 WebMvcAutoConfiguration 类下的 addFormatters 方法)

定义了 Converter、GenericConverter 和 Formatter 接口的实现类的Bean,会自动注册到 Spring MVC中。

4、自动配置 HttpMessageConverters

从代码中可以看出直接注入了 HttpMessageConverters 的 Bean,这个 Bean 是在 HttpMessageConvertersAutoConfiguration 类中定义的。

除了以上还引入了 Jackson,Gson。

在 Spring Boot中,需要新增自定义的 HttpMessageConverter时,定义一个自己的 HttpMessageConverters 的Bean,然后在此 Bean 中注册自定义的 HttpMessageConverter。

5、静态首页的支持

把静态 index.html 文件放置在以下目录:

classpath:/META-INF/resources/index.html

classpath:/resources/index.html

classpath:/static/index.html

classpath:/public/index.html

当访问应用根目录时,会直接映射

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值