SpringBoot -基础学习笔记 - 01
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;
}
SpringBoot -基础学习笔记 - 01的更多相关文章
- xml基础学习笔记01
注意:刚刚看了网上对于XML中的标签,节点和元素?到底应该怎么表述?起初我也有这个疑惑,现在我的想法是:下面出现node的应称作节点,节点对象.element应称作元素,毕竟这更符合英文的本意.至于标 ...
- handlebars.js基础学习笔记
最近在帮学校做个课程网站,就有人推荐用jquery+ajax+handlebars做网站前端,刚接触发现挺高大上的,于是就把一些基础学习笔记记录下来啦. 1.引用文件: jquery.js文件下载:h ...
- 尚学堂JAVA基础学习笔记
目录 尚学堂JAVA基础学习笔记 写在前面 第1章 JAVA入门 第2章 数据类型和运算符 第3章 控制语句 第4章 Java面向对象基础 1. 面向对象基础 2. 面向对象的内存分析 3. 构造方法 ...
- MacOS下SpringBoot基础学习
学于黑马和传智播客联合做的教学项目 感谢 黑马官网 传智播客官网 微信搜索"艺术行者",关注并回复关键词"springboot"获取视频和教程资料! b站在线视 ...
- SaToken学习笔记-01
SaToken学习笔记-01 SaToken版本为1.18 如果有排版方面的错误,请查看:传送门 springboot集成 根据官网步骤maven导入依赖 <dependency> < ...
- Redis:学习笔记-01
Redis:学习笔记-01 该部分内容,参考了 bilibili 上讲解 Redis 中,观看数最多的课程 Redis最新超详细版教程通俗易懂,来自 UP主 遇见狂神说 1. Redis入门 2.1 ...
- 软件测试之loadrunner学习笔记-01事务
loadrunner学习笔记-01事务<转载至网络> 事务又称为Transaction,事务是一个点为了衡量某个action的性能,需要在开始和结束位置插入一个范围,定义这样一个事务. 作 ...
- 【C#编程基础学习笔记】4---Convert类型转换
2013/7/24 技术qq交流群:JavaDream:251572072 教程下载,在线交流:创梦IT社区:www.credream.com [C#编程基础学习笔记]4---Convert类型转换 ...
- 【C#编程基础学习笔记】6---变量的命名
2013/7/24 技术qq交流群:JavaDream:251572072 教程下载,在线交流:创梦IT社区:www.credream.com [C#编程基础学习笔记]6---变量的命名 ----- ...
随机推荐
- Java设置文件权限
今天遇到一个问题: java写的API,ppt转图片生成的目录及文件 在使用php调用API完成后,再使用php进行删除时,遇到了删除失败的问题(php删除的部分 查看) 部署的环境是Ubuntu ...
- 【Android Studio】Android实现跳转功能
最近在玩Esp32,需要APP,顺带学了下这方面的东西. 主要实现功能: 从主 mainActivity 跳转到otherActivity. 新建一个hello工程,添加 otherActivit ...
- [ARM-Linux开发]linux 里 /etc/passwd 、/etc/shadow和/etc/group 文件内容解释
linux 里 /etc/passwd ./etc/shadow和/etc/group 文件内容解释 一./etc/passwd 是用户数据库,其中的域给出了用户名.加密口令和用户的其他信息 /etc ...
- zookeeper学习整理
概述 zookeeper是一个高可用的分布式数据管理与协调框架,基于ZAB算法实现,主要解决分布式一致性问题. https://www.cnblogs.com/felixzh/p/5869212.ht ...
- eslint 验证vue文件 报错 unexpected token =解决方法
解决方案:.eslintrc更改文件配置 { "extends": [ 'standard' ], "parserOptions": { "parse ...
- Python3使用xml.dom.minidom和xml.etree模块儿解析xml文件,封装函数
总结了一下使用Python对xml文件的解析,用到的模块儿如下: 分别从xml字符串和xml文件转换为xml对象,然后解析xml内容,查询指定信息字段. from xml.dom.minidom im ...
- Java对象深拷贝浅拷贝总结
目录 深拷贝 1. 手动new 2. clone方法 3. java自带序列化 4. json序列化 性能测试 深拷贝总结 浅拷贝 1. spring BeanUtils(Apache BeanUti ...
- Tkinter & mysql 的登录框练习
import tkinter as tk from tkinter import messagebox import pymysql class SignIn(object): def __init_ ...
- Spark 系列(六)—— 累加器与广播变量
一.简介 在 Spark 中,提供了两种类型的共享变量:累加器 (accumulator) 与广播变量 (broadcast variable): 累加器:用来对信息进行聚合,主要用于累计计数等场景: ...
- Java8 基础数据类型包装类-Long
https://blog.csdn.net/u012562117/article/details/79023440 基础 //final修饰不可更改,每次赋值都是新建类(其中-128~127是通过L ...