文章插圖

文章插圖
spring.messages.basename=i18n.login
這樣就相當于把國際化資源文件讓SpringBoot配置的ResourceBundleMessageSource管理了起來2021新版IDEA修改全部默認配置中的文件編碼模式,解決properties配置文件亂碼問題
SpringMVC的自動配置中有默認的區域信息解析器===>國際化Locale(區域信息對象),LocaleResolver(獲取區域信息對象)
自定義區域信息解析器:
/*
可以攜帶區域信息*/
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 內置對象和內置方法
轉發到某一頁面導致的表單重復提交問題
解決表單重復提交問題
登錄成功后,要防止表單被重復提交,可以重定向到主頁
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 模板布局 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.jspforward:前綴的轉發,[email protected]
*/
@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.jspresponse.sendRedirect(“/hello.jsp”)//訪問不到,要加上項目名 [email protected](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 日期格式化處理
【javaweb項目開發案例精粹視頻 javaweb項目開發案例pdf精粹】JQuery中的submit事件來提交表單,也可以阻止表單的提交
- redis安裝環境 redis運行環境
- 定向營銷策略 營銷導向案例
- 撩妹實戰案例套路,有點污有點***
- 遺傳算法經典實例matlab代碼 遺傳算法matlab程序案例詳解
- vue開發網頁案例 jsp重構vue
- 撩妹案例分析,教你趨利避害
- 陳寶蓮養小鬼真假 被小鬼反噬的明星真實案例
- 唐山大地震陰間借兵是真的嗎 陰兵借道事件真實案例
- 4對夫妻的離婚自白 離婚陳述詞案例十范文
- 經典營銷案例 如何把梳子賣給和尚-香怎么開光
