SpringBoot簡單項目學習筆記02(國際化語言設定,以及攔截器的設定)

這一節要總結的是:國際化語言設定,以及攔截器的設定
上一節總結的是:項目跟路徑設定、項目首址設定( http://www.javashuo.com/article/p-elyuazeo-kn.html
##四、根據國際化的設置:
在application.properties中進行設置國際化語言的存放地址
spring.messages.basename=i18n.login
而後在resources中進行設置

進行中英文的設定css

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<meta name="description" content="">
<meta name="author" content="">
<title>Signin Template for Bootstrap</title>
<!-- Bootstrap core CSS -->
<link th:href="@{asserts/css/bootstrap.min.css}" rel="stylesheet">
<!--<link href="asserts/css/bootstrap.min.css" rel="stylesheet">-->
<!-- Custom styles for this template -->
<!--<link href="asserts/css/signin.css" rel="stylesheet">-->
<link th:href="@{asserts/css/signin.css}" rel="stylesheet">
</head>
<body class="text-center">
<form class="form-signin" th:action="@{/user/login}" action="dashboard.html" th:method="post">
<img class="mb-4" th:src="@{asserts/img/bootstrap-solid.svg}" alt="" width="72" height="72">
<h1 class="h3 mb-3 font-weight-normal" th:text="#{login.tip}"></h1>
<!--判斷是否提示信息-->
<p th:text="${mes}" style="color: red" th:if="${not #strings.isEmpty(mes)}"/>
<label class="sr-only"th:text="#{login.username}"></label>
<input type="text" name="username" class="form-control" th:placeholder="#{login.username}" required="" autofocus="">
<label class="sr-only"th:text="#{login.password}"></label>
<input type="password" name="password" class="form-control" th:placeholder="#{login.password}" required="">
<div class="checkbox mb-3">
<label>
<input type="checkbox" value="remember-me"> [[#{login.remember}]]
</label>
</div>
<button class="btn btn-lg btn-primary btn-block" type="submit" th:text="#{login.btn}"></button>
<p class="mt-5 mb-3 text-muted">© 2017-2018</p>
<a class="btn btn-sm" th:href="@{/index.html(l='zh_CN')}">中文</a>
<a class="btn btn-sm" th:href="@{/index.html(l='en_US')}">English</a>
</form>
</body>
</html>
這裏面,<label class="sr-only"th:text="#{login.password}"></label>其中的#{login.password}訪問到的就是根據不一樣語言設定訪問到的不一樣的語言格式。
<a class="btn btn-sm" th:href="@{/index.html(l='zh_CN')}">中文</a>
<a class="btn btn-sm" th:href="@{/index.html(l='en_US')}">English</a>
在點擊不一樣的語言格式的時候,根絕th:href進行訪問到"@{/index.html},根絕從controller進行解析,訪問到的仍是login.html頁面
(l='en_US')這個就是參數的傳遞,(key=value)
而後在MyLocalResolver進行參數的獲取
public class MyLocalResolver implements LocaleResolver {
@Override
public Locale resolveLocale(HttpServletRequest request) {
String l=request.getParameter("l");
Locale locale =Locale.getDefault();
if(!StringUtils.isEmpty(l)){
String [] spilt=l.split("_");
locale = new Locale(spilt[0],spilt[1]);
}
return locale;
}
@Override
public void setLocale(HttpServletRequest request, HttpServletResponse response, Locale locale) {
}
} 
其中,根據(HttpServletRequest request)進行從網頁上進行request進行請求
而後在configuration中進行Bean的添加,將這個模塊添加到容器系統中,進行應用: 
@Bean//添加組件(將網頁獲取的中英文標識的字符串進行返回)
public LocaleResolver localeResolver(){
return new MyLocalResolver();
}
}
經過Bean進行容器的添加,在系統中就存在相應的功能
##5.在登錄界面form表單裏面進行提交的 
<form class="form-signin" th:action="@{/user/login}" action="dashboard.html" th:method="post">,
會在controller中進行解析(method中的是post方式,在controller中要以)@PostMapping(value = "/user/login")進行解析
package com.example.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.thymeleaf.util.StringUtils;
import javax.servlet.http.HttpSession;
import java.util.Map;
@Controller
public class LoginController {
@PostMapping(value = "/user/login")
// @RequestParam ("username") String Username,根絕從login網頁中傳入的值進行數據的接收
public String login(@RequestParam ("username") String Username,
@RequestParam ("password")String Password,
Map<Object,String> map, HttpSession session){
//登錄成功
if(!StringUtils.isEmpty(Username)&&Password.equals("123")) {
session.setAttribute("loginusername",Username);
return "redirect:/main.html";
}
//登陸失敗
else{
map.put("mes","用戶名密碼錯誤");
return "login";
}
}
}
根據th:action="@{/user/login}"進行controller的解析
@Controller
public class LoginController {
@PostMapping(value = "/user/login")
// @RequestParam ("username") String Username,根絕從login網頁中傳入的值進行數據的接收
// @RequestParam ("password")String Password,
public String login(@RequestParam ("username") String Username,
@RequestParam ("password")String Password,
Map<Object,String> map, HttpSession session){
//登錄成功
if(!StringUtils.isEmpty(Username)&&Password.equals("123")) {
session.setAttribute("loginusername",Username);
return "redirect:/main.html";
}
//登陸失敗
else{
map.put("mes","用戶名密碼錯誤");
return "login";
}
}
}
這裏面進行解析,同時根據在前臺傳過來的值,進行分析,(鏈接數據庫同類),將傳入的值與正確的值進行比較,若是相同進行跳轉
而且// @RequestParam ("username") String Username,根據從login網頁中傳入的值進行數據的接收
跳轉到的就是bashboard.html
"redirect:/main.html";重定向:定向到的至關因而localhost:8080/crud/main.html(main.html進行controller解析)
##6.而後就是攔截器的設定
寫一個攔截器
/*
* 登錄攔截器
* */
public class LoginHandlerInterceptor implements HandlerInterceptor {
//在目標方法執行以前
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
Object user = request.getSession().getAttribute("loginusername");
if(user == null){
//未登陸,進行攔截,返回登錄界面
request.setAttribute("mes","沒有權限請先進行登錄操做");
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 {
}
}
在這裏面就是集成現成的攔截器函數,在其中進行部分功能的豐富(返回錯誤信息)
在config中添加到bean中進行功能的豐富:
@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("/main.html").setViewName("dashboard");
}
//註冊攔截器
@Override
public void addInterceptors(InterceptorRegistry registry) {
//super.addInterceptors(registry);
//靜態資源; *.css , *.js
//SpringBoot已經作好了靜態資源映射
registry.addInterceptor(new LoginHandlerInterceptor()).addPathPatterns("/**")
.excludePathPatterns("/index.html","/","/user/login","/asserts/**");
}
};
return adapter;
}
其中進行攔截registry.addInterceptor(new LoginHandlerInterceptor()).addPathPatterns("/**")
.excludePathPatterns("/index.html","/","/user/login","/asserts/**");
除去(excludePathPatterns)部分的請求,其他的請求都會進行攔截("/index.html","/","/user/login","/asserts/**");

 

 下一節總結:員工列表、多個頁面的項目佈局的提取(http://www.javashuo.com/article/p-efifkgfb-kh.htmlhtml

相關文章
相關標籤/搜索