首先将模板文件导入resources/templates,配置MyMvcConfig

@Configurationpublic class MyMvcConfig implements WebMvcConfigurer {//      无逻辑视图跳转    @Override    public void addViewControllers(ViewControllerRegistry registry) {        registry.addViewController("/").setViewName("index");        registry.addViewController("/index.html").setViewName("index");    }}

通过在继承了WebMvcConfigurer接口的MyMvcConfig类中注册bean或重写方法,来自定义SpringBoot底层默认的代码

此时启动ConfigApplication主类,在地址栏输入"localhost:8080/"或"localhost:8080/index.html",无逻辑跳转到index,addViewControllers方法的好处是实现起来比写一个Controller类简单,但只能实现无逻辑跳转

现在浏览器页面中没有配置的css样式和img,在html标签中加入如下配置

xmlns:th="http://www.thymeleaf.org"

Spring官方支持的服务的渲染模板中,并不包含jsp。而是Thymeleaf等,而Thymeleaf与SpringMVC的视图技术,及SpringBoot的自动化配置集成非常完美,几乎没有任何成本,只用关注Thymeleaf的语法即可。也就是说jsp已经被SpringBoot官方淘汰。

<link th:href="@{/css/bootstrap.min.css}"  rel="stylesheet"><link th:href="@{/css/signin.css}" rel="stylesheet">

Thymeleaf语言非常简单,只需在html标签前加 "th:" ,后边类似EL表达式,取地址用@{},取值用${},导入用~{}等等

这样配置后,html页面中的css样式终于显示了出来

i18n页面国际化:

Springboot底层默认实现了国际化,先来看一下官方国际化是怎样配置的:

进入AcceptHeaderLocaleResolver

可以看到这个类继承了LoclaResolver接口,所有我们自定义国际化配置时的类也模仿官方来做

    public Locale resolveLocale(HttpServletRequest request) {        Locale defaultLocale = this.getDefaultLocale();        if (defaultLocale != null && request.getHeader("Accept-Language") == null) {            return defaultLocale;        } else {            Locale requestLocale = request.getLocale();            List supportedLocales = this.getSupportedLocales();            if (!supportedLocales.isEmpty() && !supportedLocales.contains(requestLocale)) {                Locale supportedLocale = this.findSupportedLocale(request, supportedLocales);                if (supportedLocale != null) {                    return supportedLocale;                } else {                    return defaultLocale != null ? defaultLocale : requestLocale;                }            } else {                return requestLocale;            }        }    }

这是官方的国际化核心方法,可以看出官方是通过将Locale存入request的头文件配置中,再在此方法中取出,如果locale为空,则使用默认国际化,若不为空则new一个与传入值对应的locale,虽然官方给我们实现了,但是从头文件里取出参数难免对我门不友好,操作复杂,所以我们可以自定义一个国际化配置器

首先,将index中转换的标签修改

      class=      class=

我们直接将参数写在url里,这样取出会很方便,zh_CN即为简体中文,依此类推,接着要新建默认配置文件,简体中文配置文件和英文配置文件

idea提供了编写国际化配置的可视化界面,为每个要国际化的文本位置都写配置

public class MyLocale implements LocaleResolver {    @Override    public Locale resolveLocale(HttpServletRequest httpServletRequest) {        //获取默认Locale        Locale locale = Locale.getDefault();        //获取url中的language参数        String language=httpServletRequest.getParameter("language");        if(!StringUtils.isEmpty(language)){            //若不为空则用_分割,再拼接            String[] strings = language.split("_");            System.out.println(language);            locale = new Locale(strings[0], strings[1]);        }        return locale;    }    @Override    public void setLocale(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Locale locale) {    }}

MyLocale类就继承LocaleResolver接口,实现它的全部方法,只对resolveLocale重写

再MyMvcConfig中注册bean

//    注册LocaleResolver国际化    @Bean    public LocaleResolver localeResolver(){        return new MyLocale();    }

国际化完成

登录功能实现:

form表单提交到/login请求中,编写Controller类

    @RequestMapping("/login")    public String login(Model model,                        @RequestParam("username") String username,                        @RequestParam("password") String password,                        HttpSession httpSession){        if(!StringUtils.isEmpty(username) & "123".equals(password)){            return "redirect:/main.html";        }        httpSession.setAttribute("loginUser",username);        model.addAttribute("msg","用户名或密码错误");        return "index";    }

若username不为空且password等于123,重定向到main.html请求中,反之则在session和model中都加入错误信息后返回到index主页

配置拦截器,防止用户自行在地址栏输入main.html直接进入后台

//拦截器public class LoginInterceptor implements HandlerInterceptor {    @Override    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {        String msg = (String)request.getSession().getAttribute("loginUser");        if(msg==null){            //未登录            request.setAttribute("loginMsg","请先登录");//            response.sendRedirect("/index.html");            request.getRequestDispatcher("/index.html").forward(request,response);            return false;        }else {            return true;        }    }}

若未登录则跳转到登陆页面,并阻止放行,若已登录则放行

注册拦截器

//    注册拦截器@Overridepublic void addInterceptors(InterceptorRegistry registry) {    registry.addInterceptor(new LoginInterceptor()).addPathPatterns("/main.html");}

只拦截/main.html请求

用户输入/main.html请求时若未登录会拦截,已登录会直接进入后台

用户注销实现:

    @RequestMapping("/del")    public String del(HttpSession httpSession){        httpSession.removeAttribute("loginUser");        return "index";    }
class="nav-link" th:href="@{/del}">Sign out

展示全部成员信息:用Thymeleaf-each循环实现

@Controllerpublic class UserController {    @Autowired    private EmployeeDao employeeDao;    @RequestMapping("/Users")    public String Users(Model model){        Collection employeeAll = employeeDao.getEmployeeAll();        model.addAttribute("users",employeeAll);        return "/list";    }}
@Repositorypublic class EmployeeDao {    public static Map employMap=null;    @Autowired    public DepartmentDao departmentDao;    static {        employMap = new HashMap<>();        employMap.put(101,new Employee(1001,"张三","123@qq.com",1,new Department(101,"吃饭部")));        employMap.put(102,new Employee(1002,"李四","122@qq.com",1,new Department(102,"学习部")));        employMap.put(103,new Employee(1003,"张四","123@qq.com",0,new Department(103,"睡觉部")));        employMap.put(104,new Employee(1004,"李三","121@qq.com",1,new Department(104,"王者部")));        employMap.put(105,new Employee(1005,"李张三","223@qq.com",0,new Department(105,"吃屎部")));    }    //查询全部员工信息    public CollectiongetEmployeeAll(){        return employMap.values();    }}
@Data@NoArgsConstructorpublic class Employee {    private Integer id;    private String lastName;    private String email;    private Integer gender;    private Department department;    private Date birth;    public Employee(Integer id, String lastName, String email, Integer gender, Department department) {        this.id = id;        this.lastName = lastName;        this.email = email;        this.gender = gender;        this.department = department;        this.birth = new Date();    }}
  <tbody>    <tr th:each="user,Employee:${users}" >      <th th:text="${user.id}">th>      <th th:text="${user.lastName}">th>      <th th:text="${user.email}">th>      <th th:text="${user.gender}">th>      <th th:text="${user.department}">th>    tr>  tbody>

功能实现

bootstrap实现单页面跳转_SpringBoot拦截器,国际化,登录实现相关推荐

  1. java登陆拦截器_SpringBoot拦截器实现登录拦截

    SpringBoot拦截器可以做什么 可以对URL路径进行拦截,可以用于权限验证.解决乱码.操作日志记录.性能监控.异常处理等. SpringBoot拦截器实现登录拦截 pom.xml:<?xm ...

  2. bootstrap实现单页面跳转_bootstrap-table.js增加跳转到xx页功能实现方法

    本文主要介绍了bootstrap-table.js扩展分页工具栏,增加跳转到xx页功能,由于小编的水平停留在dom级,此次扩展只支持页面上的表格,如果大家有好的建议欢迎提出,希望能帮助到大家. 新项目 ...

  3. Struts 拦截器权限控制【通过拦截器实现登录后跳转到登录前页面】

    应用情形:在web项目中,经常会遇到用户未登录或SESSION失效时用户发出非法的权限操作,如新闻的评论.文件的下载等等,在此我们可以使用struts拦截器对该用户发出的请求进行拦截,拦截后判断用户是 ...

  4. uniapp 拦截器-未登录跳转到登陆页面

    uniapp实现未登陆拦截 效果: 拦截器前 加了拦截器后 建一个interceptor.js //页面白名单const whiteList = ['/user-pages/bindPhone',]f ...

  5. Django实践(二)——使用模型类定义数据表,实现表单页面跳转

    Django实践(二)--使用模型类定义数据表,实现表单页面跳转 1.设计和开发信息发布的数据防访问层 配置djangosite/settings.py中的INSTALLED_APPS,添加应用app ...

  6. Struts2 框架笔记主要对跳转路径,挎包跳转,拦截器进行应用,定义成员变量,收取客户端参数等详细说明

    MVC概述 1)MVC是一种编程思想,人为的将 web系统分为三个层次. 2)MVC好处:解耦合,提高系统的可维护型和可拓展性. M-model:数据模型层 (DAO service entity) ...

  7. Bootstrap实战 - 单页面网站

    一.介绍 单页面结构简单.布局清晰,常常用来做手机 App 或者某个产品的下载介绍页面.现在,展示型网页整体趋向于单页网站设计,这样一次性把核心信息展现出来,对于用户来说更加直观和简单,能够快速了解一 ...

  8. struts2.0简单页面 (不带拦截器和带拦截器案例)

    做一个不带拦截器的struts 登录验证             不带拦截器,用户能直接访问action页面访问到数据,起不到登录验证的作用 1.使用c3p0  连接 mysql  ,数据库字段与 j ...

  9. vue---axios拦截器处理登录失效跳转登录页

    axios拦截器(Interceptors)主要分为: (1)请求拦截器:在发送请求前进行拦截,可以根据发送的请求参数做一些发送参数的调整,例如设置headers (2)响应拦截器:在后台返回响应时进 ...

最新文章

  1. 配置Vim的显示样式
  2. DeepReID (2014 CVPR)
  3. HDU 2128 Tempter of the Bone II BFS
  4. 微信小程序 对request方法二次封装
  5. 计算机视觉领域还有那些坑,深度学习/计算机视觉常见的8个错误总结及避坑指南...
  6. ERROR: Start Page at 'www/index.html' was not found
  7. 信息学奥赛一本通 1152:最大数max(x,y,z)
  8. 承包你所有壁纸需求,高图网图片,美到窒息
  9. linux睡眠进程,linux一个进程如何睡眠
  10. TCP三次握手及原理
  11. 图片无损放大器有什么软件推荐?这个不要错过
  12. python数据分析与可视化答案学堂云_智慧树知到_Python数据分析与数据可视化_最新答案...
  13. 用户行为分析(Python)
  14. 英语翻译作业(十二)
  15. 团队作业1——团队展示
  16. 彩色rgb图像拆分为rgb三个通道,并重新合并为彩色图像
  17. java中separator_JAVA中file.separator ,path.separator,line.separator
  18. 什么软件支持什么格式
  19. 怎么剪辑视频,这几个技巧必须学会
  20. 【有奖评论】HMS Core.Sparkle创新沙龙来啦,参与互动赢华为三脚架自拍杆(无线版)

热门文章

  1. 【Vue2.0】—Vue监视数据的原理(五)
  2. SyntaxError: Identifier ‘XXX‘ has already been declared
  3. 2021年POS机费率上调了吗?
  4. 24期分期免息可以提前还吗?
  5. 人为什么总感觉莫名的心烦?
  6. 三年级小孩近视150度需要戴近视眼镜么?
  7. 我有一张1996年版一元钱,值多少钱?
  8. 选什么专业?找什么样的工作?教你四个维度
  9. MATLAB产生线性等分量函数 linspace
  10. 关于web项目跨域问题详解