七.SpringMVC和Thymeleaf的簡(jiǎn)單應(yīng)用(1);登錄,國(guó)際化

基本了解后可以寫(xiě)一個(gè)項(xiàng)目來(lái)講SpringMVC和Thymeleaf來(lái)簡(jiǎn)單應(yīng)用一下。

1.環(huán)境準(zhǔn)備&需求

1.環(huán)境準(zhǔn)備:

1.JQuery
2.Bootstrap
3.一些必要的靜態(tài)資源(放在static文件夾中)
4.靜態(tài)頁(yè)面(放在templates中)
5.開(kāi)發(fā)工具idea
6.SpringBoot

2.需求

1)、登錄(實(shí)現(xiàn)攔截器),實(shí)現(xiàn)國(guó)際化;
2)、RestfulCRUD:CRUD滿足Rest風(fēng)格;

URI: /資源名稱(chēng)/資源標(biāo)識(shí) HTTP請(qǐng)求方式區(qū)分對(duì)資源CRUD操作

普通CRUD(uri來(lái)區(qū)分操作) RestfulCRUD
查詢 getEmp emp---GET
添加 addEmp?xxx emp---POST
修改 updateEmp?id=xxx&xxx=xx emp/{id}---PUT
刪除 deleteEmp?id=1 emp/{id}---DELETE

3)、實(shí)驗(yàn)的請(qǐng)求架構(gòu);

實(shí)驗(yàn)功能 請(qǐng)求URI 請(qǐng)求方式
查詢所有員工 emps GET
查詢某個(gè)員工(來(lái)到修改頁(yè)面) emp/1 GET
來(lái)到添加頁(yè)面 emp GET
添加員工 emp POST
來(lái)到修改頁(yè)面(查出員工進(jìn)行信息回顯) emp/1 GET
修改員工 emp PUT
刪除員工 emp/1 DELETE

3.實(shí)現(xiàn)國(guó)際化

1)、編寫(xiě)國(guó)際化配置文件


image.png

2)、使用ResourceBundleMessageSource管理國(guó)際化資源文件(SpringBoot自動(dòng)配置好了管理國(guó)際化資源文件的組件)


image.png
@ConfigurationProperties(prefix = "spring.messages")
public class MessageSourceAutoConfiguration {
    
    /**
     * Comma-separated list of basenames (essentially a fully-qualified classpath
     * location), each following the ResourceBundle convention with relaxed support for
     * slash based locations. If it doesn't contain a package qualifier (such as
     * "org.mypackage"), it will be resolved from the classpath root.
     */
    private String basename = "messages";  
    //我們的配置文件可以直接放在類(lèi)路徑下叫messages.properties;
    
    @Bean
    public MessageSource messageSource() {
        ResourceBundleMessageSource messageSource = new ResourceBundleMessageSource();
        if (StringUtils.hasText(this.basename)) {
            //設(shè)置國(guó)際化資源文件的基礎(chǔ)名(去掉語(yǔ)言國(guó)家代碼的)
            messageSource.setBasenames(StringUtils.commaDelimitedListToStringArray(
                    StringUtils.trimAllWhitespace(this.basename)));
        }
        if (this.encoding != null) {
            messageSource.setDefaultEncoding(this.encoding.name());
        }
        messageSource.setFallbackToSystemLocale(this.fallbackToSystemLocale);
        messageSource.setCacheSeconds(this.cacheSeconds);
        messageSource.setAlwaysUseMessageFormat(this.alwaysUseMessageFormat);
        return messageSource;
    }

3)、在頁(yè)面使用fmt:message取出國(guó)際化內(nèi)容
我們知道取國(guó)際化的Thymeleaf的標(biāo)簽是#{}


image.png
<!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 href="asserts/css/bootstrap.min.css" th:href="@{/webjars/bootstrap/4.0.0/css/bootstrap.css}" rel="stylesheet">
        <!-- Custom styles for this template -->
        <link href="asserts/css/signin.css" th:href="@{/asserts/css/signin.css}" rel="stylesheet">
    </head>

    <body class="text-center">
        <form class="form-signin" action="dashboard.html">
            <img class="mb-4" th:src="@{/asserts/img/bootstrap-solid.svg}" src="asserts/img/bootstrap-solid.svg" alt="" width="72" height="72">
            <h1 class="h3 mb-3 font-weight-normal" th:text="#{login.tip}">Please sign in</h1>
            <label class="sr-only" th:text="#{login.username}">Username</label>
            <input type="text" class="form-control" placeholder="Username" th:placeholder="#{login.username}" required="" autofocus="">
            <label class="sr-only" th:text="#{login.password}">Password</label>
            <input type="password" class="form-control" placeholder="Password" 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}">Sign in</button>
            <p class="mt-5 mb-3 text-muted">? 2017-2018</p>
            <a class="btn btn-sm">中文</a>
            <a class="btn btn-sm">English</a>
        </form>

    </body>

</html>
  • 測(cè)試


    image.png

    我們看看下面有兩個(gè)按鈕,中文,English;
    我們?cè)鯓涌梢宰龅近c(diǎn)擊這兩個(gè)按鈕,分別切換到不同語(yǔ)言呢?
    我們看看我們點(diǎn)擊兩個(gè)按鈕的時(shí)候發(fā)送的請(qǐng)求;
    我們用的原理就是根據(jù)請(qǐng)求頭帶來(lái)的區(qū)域信息獲取Locale進(jìn)行國(guó)際化
    SpringBoot提供了國(guó)際化的自動(dòng)配置
    原理:

? 國(guó)際化Locale(區(qū)域信息對(duì)象);LocaleResolver(獲取區(qū)域信息對(duì)象);

        @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;
        }
默認(rèn)的就是根據(jù)請(qǐng)求頭帶來(lái)的區(qū)域信息獲取Locale進(jìn)行國(guó)際化

4)、點(diǎn)擊鏈接切換國(guó)際化
步驟:
1.實(shí)現(xiàn)LocaleResovler接口

/**
 * 可以在連接上攜帶區(qū)域信息
 */
public class MyLocaleResolver implements LocaleResolver {
    
    @Override
    public Locale resolveLocale(HttpServletRequest request) {
        String l = request.getParameter("l");
        Locale locale = Locale.getDefault();
        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();
    }
}


我們?cè)趓esolveLocale方法在重寫(xiě)我們的方法,從請(qǐng)求頭里面判斷使用哪種標(biāo)準(zhǔn)化,然年將locale直接返回
2.改造login.html
加上以上兩句話(全文的代碼如上)


image.png
  • 測(cè)試


    image.png

    image.png

4.實(shí)現(xiàn)登錄功能

請(qǐng)求方式:post;
url:/user/login
返回:

  • 成功:重定向到主頁(yè)面
  • 失?。悍祷氐卿浗缑?/p>

    image.png

實(shí)現(xiàn)代碼

@Controller
public class LoginController {
    @PostMapping(value = "/user/login")
    public String login(@RequestParam(name = "username") String username, @RequestParam(name = "password") String password
    , HttpServletRequest request, Map<String ,Object> map) {
        //登錄成功
        if(!StringUtils.isEmpty(username) && password.equals("12345")) {
            HttpSession session = (HttpSession) request.getSession();
            session.setAttribute("loginUser",username);
            return "redirect:/main.html";
        }
        //登錄失敗
        map.put("msg","用戶密碼錯(cuò)誤");
        return "login";
    }
}

從上面的代碼我們可以看到了有一個(gè)傳遞到頁(yè)面的值msg,這個(gè)值如果是錯(cuò)誤的話會(huì)返回到Login.html頁(yè)面
那么我們?cè)趺词褂媚??如下圖:
我們?cè)陧?yè)面中加上這句話,就可以進(jìn)行判斷

image.png

測(cè)試的時(shí)候我們稍后再測(cè)試~
我們?cè)傧胂耄覀儗W(xué)SpringMVC還有Servlet的時(shí)候在做登錄的時(shí)候一般都要寫(xiě)個(gè)攔截器?不然直接使用功能,都不要用戶登錄啦,用戶量還怎么來(lái)!
下面是實(shí)現(xiàn)登錄攔截器
SpringBoot實(shí)現(xiàn)攔截器也很簡(jiǎn)單,基本步驟和SpringMVC差不多,都是:
1.實(shí)現(xiàn)HandlerInterceptor接口
2.注冊(cè),并設(shè)置要攔截的路徑
image.png

public class LoginInterceptor implements HandlerInterceptor {
    //在目標(biāo)方法執(zhí)行之前
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        HttpSession session = request.getSession();
        if(session.getAttribute("loginUser") == null) {
            request.setAttribute("msg","請(qǐng)先登錄");
            //請(qǐng)求轉(zhuǎn)發(fā),跳到登錄界面
            request.getRequestDispatcher("/index.html").forward(request,response);
            //不放行
            return false;
        }
        //已經(jīng)登錄
        //放行,看看賬號(hào)密碼對(duì)不對(duì)
        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 {

    }
}

在配置類(lèi)中把攔截器加進(jìn)去


image.png
@Configuration
public class MyConfig extends WebMvcConfigurerAdapter {

    //設(shè)置映射的路徑
    //設(shè)置默認(rèn)首頁(yè)
    @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) {
        InterceptorRegistration registration = registry.addInterceptor(new LoginInterceptor());
        //設(shè)置攔截路徑
        registration.addPathPatterns("/**");
        //放行
        registration.excludePathPatterns("/index.html", "/", "/user/login", "/static/**");
    }

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/static/**").addResourceLocations("classpath:/static/");

    }


    @Bean
    public MyLocaleResolver localeResolver() {
        return new MyLocaleResolver();
    }
}
  • 測(cè)試


    image.png

    我們想直接訪問(wèn)main.html結(jié)果直接被攔截下來(lái)了


    image.png

    賬號(hào)密碼正確
    image.png
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

友情鏈接更多精彩內(nèi)容