SpringBoot个人笔记-szs
一.使用thymeleaf模板引擎来指定所需资源的位置
可以做到当项目名进行更改后,模板引擎也会进行更新相关的路径;如下图展示,会自动添加crud根目录!
<!-- Bootstrap core CSS -->
<link href="asserts/css/bootstrap.min.css"th:href="@{/webjars/bootstrap/4.0.0/css/bootstrap.css}" rel="stylesheet">
<!--img的索引路径,使用th:src进行辅助说明-->
<img class="mb-4" th:src="@{/asserts/img/bootstrap-solid.svg}" src="asserts/img/bootstrap-solid.svg" alt="" width="72" height="72">
application.properties
server.context-path=/crud
运行测试:(查看网页源代码)
<!-- Bootstrap core CSS -->
<link href="/crud/webjars/bootstrap/4.0.0/css/bootstrap.css"rel="stylesheet">
<!-- Custom styles for this template -->
<link href="/crud/asserts/css/signin.css"rel="stylesheet">
</head>
<body class="text-center">
<form class="form-signin" action="dashboard.html">
<img class="mb-4" src="/crud/asserts/img/bootstrap-solid.svg" alt="" width="72" height="72">
二、SpringBoot配置国际化资源页面
1.设置FileEncodings中的"default encoding for properties files "为utf-8选项,并且设置自动转换为ascil编码.
2.使用thymeleaf引擎语法添加消息编码头
<a class ="btn" th:href="@{/index.html(l='zh_CN)}">中文</a>
3.国际化Locale(区域信息对象);LocaleResolver(获取区域信息对象);
默认的就是根据请求头带来的区域信息获取Locale进行国际化
@Bean
@ConditionalOnMissingBean
@ConditionalOnProperty(prefix = "spring.mvc", name = "locale")
public LocaleResolver localeResolver() {
if (this.mvcProperties
.getLocaleResolver() == WebMvcProperties.LocaleResolver.FIXED) {
return new FixedLocaleResolver(this.mvcProperties.getLocale());
}
AcceptHeaderLocaleResolver localeResolver = new AcceptHeaderLocaleResolver();
localeResolver.setDefaultLocale(this.mvcProperties.getLocale());
return localeResolver;
}
默认的就是根据请求头带来的区域信息获取Locale进行国际化
4)、点击链接切换国际化
/**
* 可以在连接上携带区域信息
*/
public class MyLocaleResolver implements LocaleResolver {
@Override
public Locale resolveLocale(HttpServletRequest request) {
String l = request.getParameter("l");
Locale locale = Locale.getDefault(); //在这里默认进行获得default,否则会null报错
if(!StringUtils.isEmpty(l)){
String[] split = l.split("_");
locale = new Locale(split[0],split[1]);
}
return locale;
}
@Override
public void setLocale(HttpServletRequest request, HttpServletResponse response, Locale locale) {
}
}
@Bean
public LocaleResolver localeResolver(){
return new MyLocaleResolver();
}
}
三.禁用掉thymeleaf的缓存
可以做到刷新thymeleaf下的页面,否则更新代码后也不刷新;ctrl+F9,idea进行重新编译即可生效.
spring.thymeleaf.cache=false
四.指定国际化页面messages的目录
在application.properties中声明:
spring.messages.basename=i18n.login
---------------------
login.btn=登陆~
login.password=密码~
login.remember=记住我~
login.tip=请登陆~
login.username=用户名~
五.thymeleaf的标准表达式简单使用#@$总结
(Standard Expression Syntax)
Simple expressions:(表达式语法)
Message Expressions: #{...}:获取国际化内容
Link URL Expressions: @{...}:定义URL;
@{/order/process(execId=${execId},execType='FAST')}
Variable Expressions: ${...}:获取变量值;OGNL;
1)、获取对象的属性、调用方法
2)、使用内置的基本对象:
#ctx : the context object.
#vars: the context variables.
#locale : the context locale.
#request : (only in Web Contexts) the HttpServletRequest object.
#response : (only in Web Contexts) the HttpServletResponse object.
#session : (only in Web Contexts) the HttpSession object.
#servletContext : (only in Web Contexts) the ServletContext object.
${session.foo}
3)、内置的一些工具对象:
#execInfo : information about the template being processed.
#messages : methods for obtaining externalized messages inside variables expressions, in the same way as they would be obtained using #{…} syntax.
#uris : methods for escaping parts of URLs/URIs
.....
Selection Variable Expressions: *{...}:选择表达式:和${}在功能上是一样;
补充:配合 th:object="${session.user}:
<div th:object="${session.user}">
<p>Name: <span th:text="*{firstName}">Sebastian</span>.</p>
<p>Surname: <span th:text="*{lastName}">Pepper</span>.</p>
<p>Nationality: <span th:text="*{nationality}">Saturn</span>.</p>
</div>
Fragment Expressions: ~{...}:片段引用表达式
<div th:insert="~{commons :: main}">...</div>
六.防止表单重复提交,可以重定向到主页dashboard
LoginController
@PostMapping(value = "/user/login")//等价下面的写法
// @RequestMapping(value = "/user/login",method = RequestMethod.POST)
public String login(@RequestParam("username") String username,
@RequestParam("password") String password, Map<String,Object> map){
if(!username.equals("")&&"123456".equals(password))
return "redirect:/main.html";//登陆成功,防止表单重复提交.可以重定向到主页dashboard
else {
map.put("msg", "用户名或者密码错误");
return "login";//登陆失败
}
}
WebMvcConfigurerAdapter
@Configuration
public class MvcConfig extends WebMvcConfigurerAdapter{
//所有的WebMvcConfigurerAdapter组件都会一起起作用
@Bean //将该组件注册到容器中
public WebMvcConfigurerAdapter webMvcConfigurerAdapter(){
WebMvcConfigurerAdapter adapter = new WebMvcConfigurerAdapter() {
@Override
public void addViewControllers(ViewControllerRegistry registry) {
registry.addViewController("/").setViewName("login");
registry.addViewController("/index.html").setViewName("login");
registry.addViewController("/szs").setViewName("login");
registry.addViewController("/main.html").setViewName("dashboard");
}
};
return adapter;
}