Spring boot+Thymeleaf+easyui集成:js创建组件页面报错

开发工具:Ideal

       使用场景:Demo

前提:

       环境:Spring boot +Thymeleaf+easyui

        引入thymeleaf模板引擎

<html lang="en" xmlns:th="http://www.thymeleaf.org">

         Html页面引入easyui需要的文件

<link href="/js/jquery-easyui-1.5.3/themes/gray/easyui.css" rel="stylesheet"/>

<link href="/js/jquery-easyui-1.5.3/themes/icon.css" rel="stylesheet"/>

<script src="/js/jquery-easyui-1.5.3/jquery.min.js"></script>

<script src="/js/jquery-easyui-1.5.3/jquery.easyui.min.js"></script>

<script src="/js/jquery-easyui-1.5.3/locale/easyui-lang-zh_CN.js"></script>

当以标签属性创建easyui组件时,页面正常显示。

以标签属性创建easyui组件:

<table class="easyui-datagrid" title="Basic DataGrid" style="width:700px;height:250px"

       data-options="singleSelect:true,collapsible:true,url:'/getUsers',method:'get'">

    <thead>

        <tr>

            <th data-options="field:'id',width:80">Item ID</th>

            <th data-options="field:'name',width:100">姓名</th>

            <th data-options="field:'tel',width:80,align:'right'">电话</th>

        </tr>

    </thead>

</table>

       页面效果:

当以js形式创建组件时出现问题

以js创建easyui组件

<table id="dg"></table>

<script type="text/javascript">

    $(function(){

        $('#dg').datagrid({

            url: '/getUsers',

            method: 'get',

            title: '用户表',

            iconCls: 'icon-save',

            width: 800,

            height: 250,

            fitColumns: true,

            singleSelect: true,

            columns:[[

                {field:'id',title:'Item ID',width:80},

                {field:'name',title:'姓名',width:80},

                {field:'tel',title:'电话',width:80}

            ]]

        });

    });

</script>

效果:

后台报以下错误:

       [THYMELEAF][http-nio-8080-exec-1] Exception processing template "user": Could not parse as expression: "

                    {field:'id',title:'Item ID',width:80},

                    {field:'name',title:'姓名',width:80},

                    {field:'tel',title:'电话',width:80}

                " (template: "user" - line 26, col 27)

解决方式:

<script type="text/javascript" >

改为

<script type="text/javascript" th:inline="none">

 

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 4
    评论
好的,下面是使用Spring BootThymeleaf实现拦截器的步骤: 1.创建一个拦截器类,该类需要实现HandlerInterceptor接口。在该类中,您可以实现三个方法:preHandle,postHandle和afterCompletion。这些方法分别在处理程序执行之前,之后和完成之后调用。 ```java import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.springframework.stereotype.Component; import org.springframework.web.servlet.HandlerInterceptor; import org.springframework.web.servlet.ModelAndView; @Component public class MyInterceptor implements HandlerInterceptor { @Override public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception { // 在请求处理之前进行调用(Controller方法调用之前) System.out.println("请求处理之前调用……"); return true; // 如果返回false,则请求中断 } @Override public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception { // 请求处理之后进行调用,但是在视图被渲染之前(Controller方法调用之后) System.out.println("请求处理之后调用……"); } @Override public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception { // 在视图被渲染之后调用(主要用于资源清理工作) System.out.println("视图渲染之后调用……"); } } ``` 2.将拦截器注册到应用程序中。在Spring Boot应用程序中,您可以通过使用WebMvcConfigurerAdapter类来注册拦截器。在该类中,您可以重写addInterceptors方法,并将您的拦截器添加到拦截器链中。 ```java import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.annotation.Configuration; import org.springframework.web.servlet.config.annotation.InterceptorRegistry; import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter; @Configuration public class WebAppConfiguration extends WebMvcConfigurerAdapter { @Autowired private MyInterceptor myInterceptor; @Override public void addInterceptors(InterceptorRegistry registry) { registry.addInterceptor(myInterceptor); } } ``` 3.使用Thymeleaf进行视图渲染。在您的控制器方法中,您可以返回一个包含视图名称和模型对象的ModelAndView对象。在视图中,您可以使用Thymeleaf模板来渲染HTML内容。在下面的示例中,我们将使用layout.html作为页面布局,并使用th:replace指令将内容注入到该布局中。 ```html <!DOCTYPE html> <html xmlns:th="http://www.thymeleaf.org"> <head> <title>My Page</title> </head> <body> <div th:replace="layout :: content"> <h1 th:text="${message}">Hello, World!</h1> </div> </body> </html> ``` 4.创建一个包含布局的Thymeleaf模板。在该模板中,您可以定义页面的基本结构和样式,并使用th:fragment指令定义页面的各个部分。在下面的示例中,我们将使用layout.html作为页面布局,并使用th:fragment指令定义页面的内容部分。 ```html <!DOCTYPE html> <html xmlns:th="http://www.thymeleaf.org"> <head> <title th:text="${pageTitle}">My Site</title> </head> <body> <div th:fragment="content"></div> </body> </html> ``` 以上就是使用Spring BootThymeleaf实现拦截器的步骤。
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值