您的位置:首页 > 其它

Sring boot学习笔记(二)

2017-07-19 00:00 295 查看
1.常用注解标签:

@Controller
:修饰class,用来创建处理http请求的对象

@RestController
:Spring4之后加入的注解,原来在
@Controller
中返回json需要
@ResponseBody
来配合,如果直接用
@RestController
替代
@Controller
就不需要再配置
@ResponseBody
,默认返回json格式。

@RequestMapping
:配置url映射

2.yml文件配置替代properties文件

application.yml 文件位于src/main/resources文件夹下

# spring 配置
spring:
# 当前激活配置项
profiles:
active: dev

# thymeleaf 配置
thymeleaf:
cache: false
check-template-location: true
content-type: text/html
enabled: true
encoding: utf-8
excluded-view-names:
mode: HTML5
prefix: classpath:/templates/
suffix: .html
template-resolver-order:

# HTTP ENCODING
http:
encoding.charset: UTF-8
encoding.enabled: true
encoding.force: true

3.国际化

首先我们先定义国际化资源文件,spring boot默认就支持国际化的,而且不需要你过多的做什么配置,只需要在resources/下定义国际化配置文件即可,注意名称必须以messages开发。

我们定义如下几个文件:
messages.properties (默认,当找不到语言的配置的时候,使用该文件进行展示)。
messages_zh_CN.properties(中文)
messages_en_US.properties(英文)

具体的代码如下:
messages.properties:

welcome=欢迎光临本站!

messages_en_US.properties:

welcome=Welcome to our weijuer's website!

前端展示使用#{key}的方式进行使用messages中的字段信息:

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.w3.org/1999/xhtml">
<head>
<meta charset="UTF-8" />
<title>weijuer Demos</title>
</head>
<body>
<p th:text="#{welcome}">Welcome to weijuer's store!</p>
<p>Today is: <span th:text="${today}">13 February 2011</span></p>
<p>Established locale country: <span th:text="${#locale.country}">US</span></p>
</body>
</html>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  Sring boot学习笔记