SpringBoot-ThreeDay

13 篇文章 0 订阅

模板引擎

th:onclick

<button class="layui-btn" th:onclick="|xadmin.open('添加用户','@{/user/add}',600,400)|">
  添加
</button>

 对于其他类型的参数

        thymeleaf为了防止js脚本注入,对于非number、bool类型的参数是不信任的,这里如果我们希望加载这类型的参数可以通过data-*来设置和获取

<!--
    data-* : 作用是为了设置标签上自定参数    this触发点击事件的dom对象,this.getAttribute("data-title")
-->
<button id="addBtn" th:data-title="${title}" data-id="主键" class="layui-btn"    th:onclick="|xadmin.open(this.dataset.title,'@{/user/add}',600,400)|">
<i    class="layui-icon"></i>添加
</button>
 <span class="current" id="current" th:utext="${page}" th:data-page="${page}"></span>
console.log($("#current").data("page"))

上面是jquery访问data-*参数

一共有三种访问方式

  1. dom对象 .dataset.参数名

  2. dom对象.getAttrbute("data-参数名")

  3. jquery对象.data("参数名")

拦截器

目录如下

 

package com.wzx.bootmybatisone.constants;

public class CommonConstants {

    public static final String LOGIN_USER_SESSION_KEY = "loginUser";
}
package com.wzx.bootmybatisone.interceptor;

import com.wzx.bootmybatisone.constants.CommonConstants;
import com.wzx.bootmybatisone.entity.User;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

public class AuthInterceptor implements HandlerInterceptor {
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        // true代表如果没有session就会创建一个session,false没有session就会返回null
        HttpSession session = request.getSession(true);
        User user = (User) session.getAttribute(CommonConstants.LOGIN_USER_SESSION_KEY);
        if (user == null) {
            // 代表没有登录
            response.sendRedirect(request.getContextPath() + "/user/login");
            // 终止后续请求
            return false;
        }
        return HandlerInterceptor.super.preHandle(request, response, handler);
    }
}
package com.wzx.bootmybatisone.interceptor;

import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

@Configuration
public class WebAppConfig implements WebMvcConfigurer {
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        //注册自己的拦截器并设置拦截的请求路径
        registry.addInterceptor(new AuthInterceptor())
                .addPathPatterns("/**")
                .excludePathPatterns("/user/login")
                .excludePathPatterns("/user/loginAjax")
                .excludePathPatterns("/css/**","/js/**","/lib/**","/images/**");
//        addPathPatterns 配置需要拦截的路径
//        excludePathPatterns 配置白名单
    }
}

模板引擎实现引用

通过th:fragment="header" 定义待引用的模块

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>include</title>
</head>
<body>
<th:block th:fragment="mate_link">
    <meta name="renderer" content="webkit|ie-comp|ie-stand">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width,user-scalable=yes, minimum-scale=0.4, initial-scale=0.8,target-densitydpi=low-dpi" />
    <meta http-equiv="Cache-Control" content="no-siteapp" />
    <link rel="stylesheet" th:href="@{/css/font.css}">
    <link rel="stylesheet" th:href="@{/css/xadmin.css}">
    <link rel="stylesheet" th:href="@{/css/login.css}">
    <!-- <link rel="stylesheet" href="./css/theme5.css"> -->
    <script th:src="@{/lib/layui/layui.js}" charset="utf-8"></script>
    <script type="text/javascript" th:src="@{/js/xadmin.js}"></script>
    <script type="text/javascript" src="https://cdn.bootcss.com/jquery/3.2.1/jquery.min.js"></script>
    <!-- 让IE8/9支持媒体查询,从而兼容栅格 -->
    <!--[if lt IE 9]>
    <script src="https://cdn.staticfile.org/html5shiv/r29/html5.min.js"></script>
    <script src="https://cdn.staticfile.org/respond.js/1.4.2/respond.min.js"></script>
    <![endif]-->
</th:block>
</body>
</html>

通过th:replace 和 th:include进行引用

 <head>
        <meta charset="UTF-8">
        <title>后台</title>
        <th:block th:include="include::mate_link"/>
        <![endif]-->
        <script>
            // 是否开启刷新记忆tab功能
            // var is_remember = false;
        </script>
    </head>

replace和include的区别

replace

会用<span>标签替换掉当前的标签,最终渲染出来内容如下

<div th:replace="include/include::content">
    13123
</div>

<span>
    13123
</span>

include


<div th:include="include/include::content">
    13123
</div>
<div>
    13123
</div>

th:block

th:block是模板引擎提供的一个标签,这个标签本身没有任何含义,只是为了让我们写命令。最终渲染的时候会被删除掉

<th:block th:fragment="mate_link">
    <meta name="renderer" content="webkit|ie-comp|ie-stand">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width,user-scalable=yes, minimum-scale=0.4, initial-scale=0.8,target-densitydpi=low-dpi" />
    <meta http-equiv="Cache-Control" content="no-siteapp" />
    <link rel="stylesheet" th:href="@{/css/font.css}">
    <link rel="stylesheet" th:href="@{/css/xadmin.css}">
    <link rel="stylesheet" th:href="@{/css/login.css}">
    <!-- <link rel="stylesheet" href="./css/theme5.css"> -->
    <script th:src="@{/lib/layui/layui.js}" charset="utf-8"></script>
    <script type="text/javascript" th:src="@{/js/xadmin.js}"></script>
    <script type="text/javascript" src="https://cdn.bootcss.com/jquery/3.2.1/jquery.min.js"></script>
    <!-- 让IE8/9支持媒体查询,从而兼容栅格 -->
    <!--[if lt IE 9]>
    <script src="https://cdn.staticfile.org/html5shiv/r29/html5.min.js"></script>
    <script src="https://cdn.staticfile.org/respond.js/1.4.2/respond.min.js"></script>
    <![endif]-->
</th:block>
<head>
        <meta charset="UTF-8">
        <title>后台</title>
        <th:block th:include="include::mate_link"/>
        <![endif]-->
        <script>
            // 是否开启刷新记忆tab功能
            // var is_remember = false;
        </script>
    </head>

js中获取上下文

  var ctx = [[${#httpServletRequest.getContextPath()}]];
    /*<![CDATA[*/
        var ctx = /*[[@{/}]]*/ +'';
    /*]]>*/

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值