模板引擎中onclick的使用:(th:onclick)
如果需要使用th:onclick,这个时候我们需要把代码用||包裹起来,之后在||里面就可以添加表达式了如${},或者@{}
获取地址参数、number类型或者bool类型参数
这类参数可以直接通过表达式获取
<button class="layui-btn" th:οnclick="|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:οnclick="|xadmin.open(this.dataset.title,'@{/user/add}',600,400)|"><i class="layui-icon"></i>添加 </button>
data-*
每个html标签上可以通过data-*设置标签自定义参数,如果我们希望给标签添加一个title指定参数,可以data-title=""进行设置
获取data-参数:
- dom对象 .dataset.参数名
- dom对象.getAttrbute("data-参数名")
- jquery对象.data("参数名")
添加拦截器
编写拦截器
/** * @author cyrus */ public class AuthInterceptor implements HandlerInterceptor { @Override public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception { // hanlder就是Controller和对应method HandlerMethod handlerMethod = (HandlerMethod) handler; // 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() + "/login"); // 终止后续请求 return false; } return super.preHandle(request, response, handler); } }
注册拦截器
@Configuration public class WebAppConfig implements WebMvcConfigurer { @Override public void addInterceptors(InterceptorRegistry registry) { //注册自己的拦截器并设置拦截的请求路径 registry.addInterceptor(new AuthInterceptor()) .addPathPatterns("/**") .excludePathPatterns("/login"); } }
thymeleaf模板引擎实现引用
通过th:fragment="header" 定义待引用的模块
<!DOCTYPE html> <html lang="en" xmlns:th="http://www.thymeleaf.org"> <head> <meta charset="UTF-8"> <title>包含</title> </head> <body> <div th:fragment="header"> <div>头部</div> </div> </body> </html>
通过th:replace 和 th:include进行引用
<div th:replace="include::header"></div> <div th:include="include::header"></div>
replace和include的区别
<span th:fragment="content"> 一行代码 </span>
Spring boot 整合mybatis
引入依赖
java: 程序包com.tledu.springboot01.vo不存在
1.首先确认在pom.xml中正确导入maven依赖
2.找到IDEA下右边的Maven面板下的lifecycle下的clean与install,然后点击运行(确认是否是因为项目依赖未正常导入便停止)