1 thymeleaf中的onclick
如果需要使用th:onclick,这个时候我们需要把代码用||包裹起来,之后在||里面就可以添加表达式了如${},或者@{}。
1.1 获取地址参数、number或者boolean类型参数
th:οnclick="|xadmin.open('用户编辑','@{/user/edit(id=${item.id})}',650,650)|">
1.2 获取其他类型参数
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>
1.3 data-*
每个html标签上可以通过data-*设置标签自定义参数,如果我们希望给标签添加一个title指定参数,可以data-title=""进行设置
获取data-参数:
- dom对象 .dataset.参数名
- dom对象.getAttrbute(“data-参数名”)
- jquery对象.data(“参数名”)
2 添加拦截器
2.1 编写拦截器类
/**
* @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