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;
}

最新文章

  1. 浏览器全屏事件(Html5)
  2. ajax缓存问题
  3. hadoop集群安装无密码登录
  4. .NET 泛型分析
  5. HashPasswordForStoringInConfigFile中的Md5算法并非常用的Md5算法
  6. Android--ListView下拉刷新
  7. JNI笔记之 初体验
  8. Android SDK在线更新镜像服务器
  9. 转 系统级编程语言性能PK
  10. 支付宝也要上&quot;服务号&quot;?斗战微信继续升级
  11. PHP的SQL注入攻击的技术实现以及预防措施
  12. 2016阿里巴巴校招offer面经
  13. 全球SEO行业调查报告
  14. DataTable 导出到 Excel 类
  15. Android面试题集锦 (转)
  16. Abandoned country
  17. COGS 144. [USACO Dec07] 魅力手镯【01背包复习】
  18. 马凯军201771010116《面向对象与程序设计Java》第十五周学习知识总结
  19. creator NDK_PROJECT_PATH=null
  20. 【详解】Tomcat是如何监控并删除超时Session的?

热门文章

  1. [神经网络与深度学习][计算机视觉]SSD编译时遇到了json_parser_read.hpp:257:264: error: ‘type name’ declared as function ret
  2. Postman 使用方法详细介绍
  3. vm采用NAT方式连接时,设置静态ip
  4. vue --- axios拦截器+form格式请求体
  5. 【C#】上机实验七
  6. 可能这些是你想要的H5软键盘兼容方案
  7. MySQL Group Replication的安装部署
  8. python线程(转)
  9. JAVA的枚举基本操作,对原码反码补码的理解及为运算的深入理解,浮点数计算的误差分析
  10. DevExtreme学习笔记(一) DataGrid中数据提交注意事项