SpringBoot源码学习之路(八、Web实战详解(拦截器、视图解析、国际化、Thymeleaf模板引擎))

Web实战详解

一、默认访问登录页实现

此功能可以在上一篇文章也有具体描述,其实就是实现一些自定义的视图解析功能。将请求都映射至登录页(login.html),实现如下:


@Configuration
public class MyMvcConfig extends 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");
            }
        };
        return adapter;
    }
}

这样请求就会被模板引擎Thymeleaf映射到classpath:templates/login.html了。


二、国际化实现

1.编写国际化配置文件,抽取页面需要显示的国际化消息
国际化资源文件中不带国家和语言后缀的代表为默认展示语言(login.properties)。不设置语言参数时会展示默认的。
SpringBoot源码学习之路(八、Web实战详解(拦截器、视图解析、国际化、Thymeleaf模板引擎))_第1张图片

2使用ResourceBundleMessageSource管理国际化资源文件。
SpringBoot已经自动配置好了ResourceBundleMessageSource.class组件,分析其部分源码:

@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";  
    //从源码的注释“it will be resolved from the classpath root.”可以看出
    //SpringBoot会默认在类路径下找名为 messages.properties 的文件作为国际化资源文件
    //如果我们自己定义的话,需要在配置中加【spring.message.basename=xxx】(xxx代表国际化资源文件的基础名)

    @Bean
    public MessageSource messageSource() {
        ResourceBundleMessageSource messageSource = new ResourceBundleMessageSource();
        if (StringUtils.hasText(this.basename)) {
            //设置国际化资源文件的基础名(去掉语言国家代码文件名,无需后缀)
            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;
    }

从源码中可以看出我们的国际化配置文件需要在spirngboot的配置文件中配置出来:

# 国际化配置文件(包名.基础名)
spring.messages.basename=i18n.login

3在页面中获取国际化的值,
我们的login.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 Bootstraptitle>
        

        
        <link th:href="@{/webjars/bootstrap/4.0.0/css/bootstrap.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}" method="post">
            <img class="mb-4"  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 inh1>
            
            <p style="color: red" th:text="${msg}" th:if="${not #strings.isEmpty(msg)}">p>
            
            <label class="sr-only" th:text="#{login.username}">Usernamelabel>
            <input type="text"  name="username" class="form-control" placeholder="Username" th:placeholder="#{login.username}" required="" autofocus="">
            
            <label class="sr-only" th:text="#{login.password}">Passwordlabel>
            <input type="password" name="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 inbutton>
            <p class="mt-5 mb-3 text-muted">© 2017-2018p>
            <a class="btn btn-sm">中文a>
            <a class="btn btn-sm">Englisha>
        form>
    body>

html>

到此我们的登录页就可以实现根据浏览器的语言来决定使用哪个对应的国际化配置文件的值进行展示。
那么,根据浏览器语言设置的信息切换了国际化的底层原理是什么呢?
先来了解两个对象:国际化Locale(区域信息对象)和’LocaleResolver’(获取区域信息对象);

参考mvc的自动配置类WebMvcAutoConfiguration.class源码中;

        @Bean
        @ConditionalOnMissingBean
        @ConditionalOnProperty(
            prefix = "spring.mvc",
            name = {"locale"}
        )
        public LocaleResolver localeResolver() {
            if (this.mvcProperties.getLocaleResolver() == org.springframework.boot.autoconfigure.web.servlet.WebMvcProperties.LocaleResolver.FIXED) {
                return new FixedLocaleResolver(this.mvcProperties.getLocale());
            } else {
                AcceptHeaderLocaleResolver localeResolver = new AcceptHeaderLocaleResolver();
                localeResolver.setDefaultLocale(this.mvcProperties.getLocale());
                return localeResolver;
            }
        }

继而根据源码可一直到,springBoot在默认情况下,根据请求头带来的区域信息获取Locale进行国际化的,因而就会根据浏览器所设置的语言信息选择不同的国际化配置。


4尝试实现使用自定义国际化切换按钮,而不使用浏览器的语言设置来判断国际化
(1)、其实实现就是通过自定义’LocaleResolver’来获取Locale

public class MyLocaleResolver implements LocaleResolver {

    @Override
    public Locale resolveLocale(HttpServletRequest request) {
        String lang = request.getParameter("lang ");
        Locale locale = Locale.getDefault();
        //lang的值一般是以【语言_国家】的形式组成,如en_US
        if(!StringUtils.isEmpty(lang)){
            String[] split = l.split("_");
            //根据不同的参数定义不同的locale对象
            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();
    }
}

(2)、然后在html上定义对应的中英文切换按钮:

<a class="btn btn-sm" th:href="@{/index.html(lang='zh_CN')}">中文a>
<a class="btn btn-sm" th:href="@{/index.html(lang='en_US')}">Englisha>

即可完成。页面效果:

SpringBoot源码学习之路(八、Web实战详解(拦截器、视图解析、国际化、Thymeleaf模板引擎))_第2张图片


三、登录功能实现

(1)、SpringBoot开发如何做到模板引擎实时生效:
①、禁用模板引擎的缓存

spring.thymeleaf.cache=false 

②、页面修改完成以后ctrl+f9:重新编译。

(2)、登录功能处理:
html:



<p style="color: red" th:text="${msg}" th:if="${not #strings.isEmpty(msg)}">p>
<label class="sr-only" th:text="#{login.username}">Usernamelabel>
<input type="text"  name="username" class="form-control" placeholder="Username" th:placeholder="#{login.username}" required="" autofocus="">
<label class="sr-only" th:text="#{login.password}">Passwordlabel>
<input type="password" name="password" class="form-control" placeholder="Password" th:placeholder="#{login.password}" required="">

controller:

 @PostMapping(value = "/user/login")
    public String login(@RequestParam("username") String username,
                        @RequestParam("password") String password,
                        Map map, HttpSession session){
        if(!StringUtils.isEmpty(username) && "123456".equals(password)){
            //登陆成功,防止表单重复提交,使用重定向到主页
            session.setAttribute("loginUser",username);
            return "redirect:/main.html";
        }else{
            //登陆失败
            map.put("msg","用户名密码错误");
            return  "login";
        }

    }

(3)、定义拦截器,防止用户直接访问main.html:
在mvc配置类中配置拦截路径:MyMvcConfig.class


            @Override
            public void addInterceptors(InterceptorRegistry registry) {

                //静态资源; 
                //SpringBoot已经做好了静态资源映射
               registry.addInterceptor(new LoginHandlerInterceptor()).addPathPatterns("/**")
               //此处还需要配置不需要登录的访问地址不拦截。至于静态资源( *.css , *.js)SpringBoot已经做好了静态资源映射,所以不需要我们  exclude
                  .excludePathPatterns("/index.html","/","/user/login");
            }
        };
        return adapter;
    }

定义LoginHandlerInterceptor:

/**
 * 登陆检查,
 */
public class LoginHandlerInterceptor 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 {

    }
}

到此就完成了登录功能,具体项目参考springboot笔记中:【spring-boot-04-web-restfulcrud】。

你可能感兴趣的:(架构之路,SpringBoot,Thymeleaf)