spring.messages.basename=i18n.login
這樣就相當於把國際化資源文件讓SpringBoot配置的ResourceBundleMessageSource管理了起來
2021新版IDEA修改全部默認配置中的文件編碼模式,解決properties配置文件亂碼問題

通過以上設置,我們根據瀏覽器語言的設置切換國際化,下面展示原理:
SpringMVC的自動配置中有默認的區域信息解析器===>國際化Locale(區域信息對象),LocaleResolver(獲取區域信息對象)

點擊鏈接實現國際化切換
1.編寫自己的區域信息解析器,並放到容器中
自定義區域信息解析器:
/*
- 可以攜帶區域信息
- */
public class MyLocaleResolver implements LocaleResolver
{
@Override
public Locale resolveLocale(HttpServletRequest Request) {
String l=Request.getParameter(“l”);
Locale locale=Locale.getDefault();//Locale.getDefault()獲取當前的語言環境—操作系統的語言環境
if(!StringUtils.isEmpty(l))
{
String[] s = l.split(“_”);
locale=new Locale(s[0],s[1]);//第一個參數是國家,第二個參數是語言
}
return locale;
}
@Override
public void setLocale(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Locale locale) {
}
}
SpringMVC擴展類: 負責將自定義的組件加入到容器中
//使用WebMvcConfigurerAdapter可以來擴展SpringMvc的功能
@Configuration
public class myConfig extends WebMvcConfigurerAdapter
{
//所有的WebMvcConfigurerAdapter組件都會一起起作用
@Bean//將容器註冊在容器中
public WebMvcConfigurerAdapter addViewControllers()
{
WebMvcConfigurerAdapter adapter=new WebMvcConfigurerAdapter() {
@Override
public void addViewControllers(ViewControllerRegistry registry) {
registry.addViewController(“/”).setViewName(“index”);
registry.addViewController(“/index.html”).setViewName(“index”);
}
};
return adapter;
}
@Bean
//在SpringMVC擴展類中,將剛才寫的區域信息解析器放到容器中
public LocaleResolver localeResolver()
{
return new MyLocaleResolver();
}
}
效果展示:


登錄模塊
===================================================================
SpringMVC新特性支持的Rest風格的註解
@RestController註解
@RestController等常見註解
@PostMapping, @GetMapping, @PutMapping, @DeleteMapping四個支持Rest風格的註解
模板引擎頁面修改後要時時生效==>禁用掉模板引擎的緩存+重新編譯
在全局配置文件中禁用掉模板引擎的緩存
#禁用掉模板引擎的緩存,這樣頁面內容一修改,就可以看到修改後的效果
spring.thymeleaf.cache=false
IDEA在項目運行期間,不會讓我們對頁面的修改生效,如果想讓我們對頁面的修改時時生效,第一步禁用緩存,第二步按住ctrl+f9重新編譯當前頁面
Thymeleaf 內置對象和內置方法
Thymeleaf 內置對象和內置方法
轉發到某一頁面導致的表單重複提交問題
解決表單重複提交問題
登錄成功後,要防止表單被重複提交,可以重定向到主頁

攔截器進行登錄檢查,防止不經過登錄直接來到某一頁面
SpringBoot已經做好了靜態資源的映射
1.自定義登錄攔截器,通過獲取session中存放的數據,來判斷是否已經登錄過
public class LoginHanlderIntercept implements HandlerInterceptor
{
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
Object user=request.getSession().getAttribute(“loginUser”);
if(user==null)
{
//未登陸,返回登陸頁面
request.setAttribute(“msg”,“沒有權限請先登陸”);
request.getRequestDispatcher(“/index.html”).forward(request,response);
return false;
}
else
{
//已登陸,放行請求
return true;
}
}
@Override
public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
}
@Override
public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
//登陸後,將之前存儲在session裡面的登錄憑證銷毀,無論是否存在憑證,都執行銷毀操作
request.getSession().removeAttribute(“loginUser”);
}
}
2.如果登錄成功,那麼往session中存放一個username作為登錄憑證
@Controller
public class LoginController
{
@PostMapping(“/user/login”)
public String Login(@RequestParam(“username”)String username,
@RequestParam(“password”)String password
, Map<String,Object> map, HttpSession session)
{
if(username.equals(“大忽悠”)&&“123456”.equals(password))
{
session.setAttribute(“loginUser”,username);
//登錄成功
return “redirect:/main.html”;
}
//登錄失敗
map.put(“msg”,“用戶名或密碼錯誤”);
return “index”;
}
}
3.在springmvc擴展類中將自定義的攔截器進行註冊
//使用WebMvcConfigurerAdapter可以來擴展SpringMvc的功能
@Configuration
public class myConfig extends WebMvcConfigurerAdapter
{
//所有的WebMvcConfigurerAdapter組件都會一起起作用
@Bean//將容器註冊在容器中
public WebMvcConfigurerAdapter addViewControllers()
{
WebMvcConfigurerAdapter adapter=new WebMvcConfigurerAdapter() {
@Override
public void addViewControllers(ViewControllerRegistry registry) {
registry.addViewController(“/”).setViewName(“index”);
registry.addViewController(“/index.html”).setViewName(“index”);
registry.addViewController(“/main.html”).setViewName(“success”);
}
@Override
public void addInterceptors(InterceptorRegistry registry) {
registry.addInterceptor(new LoginHanlderIntercept()).addPathPatterns(“/**”)//攔截任意多層路徑下的所有請求
.excludePathPatterns(“/index.html”,”/”,”/user/login”);//某些請求不進行攔截
}
};
return adapter;
}
@Bean
//在SpringMVC擴展類中,將剛才寫的區域信息解析器放到容器中
public LocaleResolver localeResolver()
{
return new MyLocaleResolver();
}
}
小細節:如果已經登錄成功了,那麼session域中就會存在已經登錄的憑證,如果此時回退到登錄頁面,那麼就可以不登錄直接訪問對應網頁,這個的解決方法如下:
使用下面這個解決方法的前提是攔截器只攔截登錄頁面,而不是所有請求,不然當登錄成功後,點擊當前頁面的任何請求,都會回到登錄頁面
@Override
public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
//登陸後,將之前存儲在session裡面的登錄憑證銷毀,無論是否存在憑證,都執行銷毀操作
request.getSession().removeAttribute(“loginUser”);
}
攔截器如果攔截所有請求,靜態資源也會被攔截,因此注意排除掉對應的靜態資源訪問路徑
CRUD—員工列表


thymeleaf公共頁面元素抽取

這裡模板名就是html頁面的名字,即xxx(模板名).html
這裡的模板名會使用thymeleaf的前後綴配置規則進行解析
三種引入功能片段的th屬性

具體使用參考下面這篇文章
Thymeleaf 模板布局 th:fragment、th:replace、th:insert、th:remove
如果使用了thymeleaf模板引擎,那麼controller層的返回值就會由模板引擎自動拼串,因此如果我們還想轉發或者重定向到某個請求,就需要加上forward或者redirect前綴
加上forward或者redirect前綴後,springboot也提供了各自的視圖解析處理器,底層就是原生的轉發和重定向
SpringMVC中的forward和redirect前綴路徑問題:
package com.czl.controller;
@Controller
public class HelloController {
/**
- forward:轉發到一個頁面
- /hello.jsp:轉發當前項目下的hello;
- 一定加上/,如果不加/就是相對路徑。容易出問題;
- forward:/hello.jsp
- forward:前綴的轉發,不會由我們配置的視圖解析器拼串
- @return
*/
@RequestMapping(“handle01”)
public String handle01(){
System.out.println(“handle01…”);
return “forward:/hello.jsp”;
}
@RequestMapping(“handle02”)
public String handle02(){
System.out.println(“handle02…”);
return “forward:/handle01”;
}
/**
- 重定向到hello.jsp頁面
- 有前綴的轉發和重定向操作,配置的視圖解析器就不會進行拼串;
- 轉發 forward:轉發的路徑
- 重定向 redirect:重定向的路徑
- /hello.jsp:代表就是從當前項目下開始;在SpringMVC中會為路徑自動的拼接上項目名
- 原生的Servlet重定向/路徑需要加上項目名才能成功,
- 重定向的url路徑是要發給瀏覽器讓瀏覽器按照該url訪問服務器的,而瀏
- 覽器解析/ 只到站點,如 localhost:8080/,使用response.sendRedirect(“/hello.jsp”),瀏覽器只會解析為:
- localhost:8080/hello.jsp
- response.sendRedirect(“/hello.jsp”)//訪問不到,要加上項目名 /SpringMVC_viewResolver_06/hello.jsp
- @returnrd.include(requestToExpose, response);
*/
@RequestMapping(“handle03”)
public String handle03(){
System.out.println(“handle03…”);
return “redirect:/hello.jsp”;
}
@RequestMapping(“handle04”)
public String handle04(){
System.out.println(“handle04…”);
return “redirect:/handle03”;
}
}
SprinBoot中的日期格式化問題
SpringBoot底層日期格式化原理:
默認有一個日期格式化器:

默認使用的日期格式是/方式,如果後台接收到前台的日期格式不是\,那麼就會報錯:

我們可以在配置文件中進行日期格式修改,替換默認的日期格式:
spring.mvc.date-format=yyyy-MM-dd
Thymeleaf 日期格式化處理
${#dates.format(key)}
${#dates.format(key, ‘yyyy-MM-dd HH:mm:ss’)}
格式化傳遞過來的 Date 對象,如果沒有指定時間格式,將使用瀏覽器當前使用的時間格式
Thymeleaf 日期格式化處理
JQuery中的submit事件來提交表單,也可以阻止表單的提交

原創文章,作者:投稿專員,如若轉載,請註明出處:https://www.506064.com/zh-hant/n/221419.html