Spring Boot的web之旅(一)

1.创建web项目首先加入web依赖

<!--web依赖 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

2.添加webFlux依赖(这我暂时还没搞清楚,加了再说)

<!-- webflux是个新东西,要专门学习,先放在这里用着 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-webflux</artifactId>
        </dependency>

3.使用热部署

<!-- 热部署 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
        </dependency>

4.配置文件

#配置端口号,默认8080
server.port=8888
#配置自定义属性 因为中文会乱码,所以采用Unicode编码形式
#实战之旅
book.name=spring boot2\u5b9e\u6218\u4e4b\u65c5
#杨洋
book.author=\u6768\u6D0B
#配置使用随机数
#随机字符串
book.value=${random.value}
#随机int值
book.intValue=${random.int}
#随机long值
book.longValue=${random.long}
#随机uuid
book.uuid=${random.uuid}
#1000以内的随机数
book.randomNumber=${random.int(1000)}
#自定义属性间引用
#书名是
book.title=\u4e66\u540d\u662f:${book.name}

5.创建controller

//相当于@Controller和@ResponseBody之和
@RestController
public class TestController {
    //通过@Value("${属性名}")读取配置文件
    @Value("${book.name}")
    private String bookName;
    @Value("${book.author}")
    private String bookAuthor;
    
    //相当于@RequestMapping(method=RequestMethod.GET)
    @GetMapping("test1")
    public String test1() {
        return "本书书名是"+bookName+",本书作者是"+bookAuthor;
    }
}
结果是:
本书书名是Spring Boot2实战之旅,本书作者是杨洋

上面有一个缺点是当属性多了的时候,使用@Value("${属性名}")会比较麻烦,可以创建一个JavaBean来封装这些属性,比如:

//由于自定义属性都以book为前缀
//还需要在启动类上加上@EnableConfigurationProperties(BookConfigBean.class)启动这个配置类
@ConfigurationProperties(prefix = "book")
public class BookConfigBean {
    private String name;
    private String author;
    private String value;
    private int intValue;
    private long longValue;
    private String uuid;
    private int randomNumber;
    private String title;
//setter getter
}

给controller添加一个方法:

@Autowired
    private BookConfigBean bookConfigBean;
    @GetMapping("test2")
    public BookConfigBean test2() {
        return bookConfigBean;
    }

结果:

{"name":"spring boot2实战之旅","author":"杨洋","value":"44a5b03e2dcdd7a8e428cc8149b22197","intValue":-1288322961,"longValue":-7180575194781037483,"uuid":"44d75104-4b67-4844-ad40-910cf2719b58","randomNumber":993,"title":"书名是:spring boot2实战之旅"}

刚刚返回的这个对象就包括这个对象的所有属性。

还有一种方法不需要需要在启动类上加上@EnableConfigurationProperties(BookConfigBean.class)启动这个配置类,例如:

@Component
@PropertySource(value="classpath:test.properties")
@ConfigurationProperties(prefix = "com.book")
public class ConfigBean {
    private String name;
    private String author;
//setter getter
}

在JavaBean上加上@Component,@PropertySource(value="classpath:对应的配置文件名")就行了(不写就是默认配置文件,所以第二种方法其实不在启动类上加上@EnableConfigurationProperties(BookConfigBean.class),直接在BookConfigBean类上加上@Component也可以,测试也成功),再写一个controller:

@Autowired
    private ConfigBean configBean;
    @GetMapping("test3")
    public ConfigBean test3() {
        return configBean;
    }
结果:
{"name":"Spring Boot2实战之旅bbb","author":"杨洋"}

多环境配置

application-dev.properties:开发环境

application-test.properties:测试环境

application-prod.properties:生产环境

在application.properties里设置使用哪个环境:例如spring.profiles.active=test使用测试环境

使用页面模板

1.使用Thymeleaf模板

1.1 引入相关依赖

<!-- thymeleaf -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
        </dependency>
        <!-- 去除html严格校验 -->
        <dependency>
            <groupId>net.sourceforge.nekohtml</groupId>
            <artifactId>nekohtml</artifactId>
            <version>1.9.22</version>
        </dependency>
1.2 配置文件内容
#是否开启缓存,开发时建议关闭,否则更改页面后不会实时展示效果
spring.thymeleaf.cache=false
#编码格式
spring.thymeleaf.encoding=UTF-8
#用这个去除thymeleaf严格校验
spring.thymeleaf.mode=LEGACYHTML5
#前缀
spring.thymeleaf.prefix=classpath:/static/
#后缀
spring.thymeleaf.suffix=.html
注意今天使用时出现找不到模板,但配置都是好的,后来把这个删了自己写一遍就没问题了,所以有时候复制粘贴会出现一些莫名的格式问题,需要注意。1.3 写个controller:
@Controller
public class IndexController {
    @GetMapping("/")
    public String index(ModelMap modelMap) {
        modelMap.addAttribute("msg","hello dalaoyang");
        return "index";
    }
}
1.4 写个html:
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.w3.org/1999/xhtml">
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
    <h1 th:text="${msg}"></h1>
</body>
</html>

th:text=””这个是thymeleaf前后端交互的格式,后面再讲。

2.使用FreeMarker模板

2.1 引入依赖

<!-- FreeMarker-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-freemarker</artifactId>
        </dependency>
2.2  配置文件内容
#是否开启缓存,开发时建议关闭,否则更改页面后不会实时展示效果
spring.freemarker.cache=false
#编码格式
spring.freemarker.charset=UTF-8
#前缀
spring.freemarker.template-load-path=classpath:/static/
#后缀
spring.freemarker.suffix=.ftl

2.3  写个controller

@Controller
public class IndexController {
    @GetMapping("/")
    public String index(ModelMap modelMap) {
        modelMap.addAttribute("msg","hello dalaoyang");
        return "index";
    }
}
2.4 写个html
<!DOCTYPE html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
    <h1>${msg}></h1>
</body>
</html>

这里相比于ThymeLeaf少了一行。应该是thymeleaf要配置th这些东西,而FreeMarker不需要。

3.使用传统jsp

不推荐使用,日后再说。

猜你喜欢

转载自www.cnblogs.com/xc-xinxue/p/12393060.html
今日推荐