【问题标题】:Spring Boot + Thymeleaf + Dandelion configuration not workingSpring Boot + Thymeleaf + Dandelion 配置不起作用
【发布时间】:2015-04-26 11:24:39
【问题描述】:

我正在使用带有 Thymeleaf 的 Spring Boot,现在我想添加蒲公英数据表,但它不起作用。

这是我的 Maven 依赖项:

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>1.2.1.RELEASE</version>
    <relativePath /> <!-- lookup parent from repository -->
</parent>
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-websocket</artifactId>
</dependency>
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-tomcat</artifactId>
    <scope>provided</scope>
</dependency>
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-test</artifactId>
    <scope>test</scope>
</dependency>

<!-- Dandelion -->
<dependency>
    <groupId>com.github.dandelion</groupId>
    <artifactId>datatables-thymeleaf</artifactId>
    <version>0.10.1</version>
</dependency>

我正在关注本指南 http://dandelion.github.io/dandelion/docs/installation/thymeleaf.html 并配置了以下 bean:

@SpringBootApplication
public class Application {

    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }

    @Bean
    public FilterRegistrationBean dandelion() {
        FilterRegistrationBean registrationBean = new FilterRegistrationBean();
        registrationBean.setFilter(new DandelionFilter());
        registrationBean.addUrlPatterns("/*");
        return registrationBean;
    }

    @Bean
    public ServletRegistrationBean dandelionServlet() {
        ServletRegistrationBean registrationBean = new ServletRegistrationBean();
        registrationBean.setServlet(new DandelionServlet());
        registrationBean.addUrlMappings("/dandelion/*");
        return registrationBean;
    }

    @Bean
    public ServletContextTemplateResolver defaultTemplateResolver() {
        ServletContextTemplateResolver resolver = new ServletContextTemplateResolver();
        resolver.setTemplateMode("HTML5");
        resolver.setPrefix("/WEB-INF/templates/");
        resolver.setSuffix(".html");
        resolver.setCharacterEncoding("UTF-8");
        resolver.setCacheable(false);

        SpringTemplateEngine engine = new SpringTemplateEngine();
        engine.setTemplateResolver(resolver);
        engine.addDialect(new DataTablesDialect());

        return resolver;
    }

}

我已经制作了这个 HTML 用于测试:

<!doctype html>
<html 
	xmlns:th="http://www.thymeleaf.org" 
	xmlns:ddl="http://github.com/dandelion">
<head>
	<link type="text/css" href="/stylesheets/dataTables.css" media="screen" rel="stylesheet" />
	<script src="/javascripts/vendor/jquery191.js" type="text/javascript"></script>
	<script src="/javascripts/vendor/dataTables.js" type="text/javascript"></script>
</head>
<body>
	<br/>
	<table id="myTableId" ddl:table="true" ddl:url="@{/clientes}">
	   <thead>
	      <tr>
	         <th ddl:property="telefone">Telefone</th>
	         <th ddl:property="nome">Nome</th>
	      </tr>
	   </thead>
	</table>
</body>
</html>

我认为蒲公英的 servlet 没有被调用。 未处理命名空间。

【问题讨论】:

    标签: java spring spring-boot thymeleaf dandelion


    【解决方案1】:

    有几个错误。它们中的大多数与我第一次使用蒲公英数据表时所做的相同。 :)

    我正在为下面的每个代码编写完整的简单示例,以供将来任何人参考。所以请确保只将缺少的添加到您的项目中

    首先将这两个依赖项添加到您的 maven。 (你已经有了第一个。所以添加后者。)

    <dependency>
        <groupId>com.github.dandelion</groupId>
        <artifactId>datatables-thymeleaf</artifactId>
        <version>0.10.1</version>
    </dependency>
    <dependency>
        <groupId>com.github.dandelion</groupId>
        <artifactId>datatables-spring3</artifactId>
        <version>0.10.1</version>
    </dependency>
    

    然后添加这些配置。您必须为方言创建 Bean。我想你错过了..

    @Configuration
    public class DandelionConfig {
    
        @Bean
        public DandelionDialect dandelionDialect() {
            return new DandelionDialect();
        }
    
        @Bean
        public DataTablesDialect dataTablesDialect(){
            return new DataTablesDialect();
        }
    
        @Bean
        public Filter dandelionFilter() {
            return new DandelionFilter();
        }
    
        @Bean
        public ServletRegistrationBean dandelionServletRegistrationBean() {
            return new ServletRegistrationBean(new DandelionServlet(), "/dandelion-assets/*");
        }
    }
    

    视图可以是这样的

    <!DOCTYPE html>
    <html xmlns:th="http://www.thymeleaf.org"
          xmlns:ddl="http://www.thymeleaf.org/dandelion"
          xmlns:dt="http://www.thymeleaf.org/dandelion/datatables">
    <head lang="en"></head>
    <body>
        <table id="myTableId"
            dt:table="true"
            dt:url="@{/clientes}"
            dt:serverside="true"
            dt:processing="true">
              <thead>
                <tr>
                  <th dt:property="telefone">Telefone</th>
                  <th dt:property="nome">Nome</th>
                </tr>
              </thead>
         </table>
    </body>
    </html>
    

    这里您使用的是服务器端处理。这要求您的控制器在 /clientes 上进行映射,该映射返回 DatatablesResponse

    @Override
    @RequestMapping(value = "/clientes")
    @ResponseBody
    public DatatablesResponse<MyObject> data(HttpServletRequest request){
        List<MyObject> myObjectList = ... //logic to fetch a list of objects
    
        DatatablesCriterias criterias = DatatablesCriterias.getFromRequest(request);
        DataSet<MyObject> dataSet = new DataSet<>(myObjectList, (long)myObjectList.size(), (long)myObjectList.size());
        return DatatablesResponse.build(dataSet, criterias);
    }
    

    MyObject 是您传递给蒲公英数据表的对象

    public class MyObject {
        private String telefone;
        private String nome;
    
        public String getTelefone() {
            return telefone;
        }
    
        public void setTelefone(String telefone) {
            this.telefone = telefone;
        }
    
        public String getNome() {
            return nome;
        }
    
        public void setNome(String nome) {
            this.nome = nome;
        }
    }
    

    【讨论】:

    • 我跟着这个,几乎所有东西都可以工作,除了 DataTables 只显示“处理”。我得到的数据没有问题,但它没有反映在数据表中。它只显示“处理中”。
    • 您能否检查您从 ajax 查询中从服务器获得的响应。可能是它给出了一个错误。加载页面时,从 chrome 控制台的网络选项卡中检查它。
    • 面临与@ReyLibutan 相同的问题。我得到了预期的响应,即我可以看到从我的控制器发送的数据,但数据表是空白的。我还尝试了使用文档中提到的 thymeleaf、spring 控制器的默认配置,但仍然遇到同样的问题。
    • @BhaskerYadav,您一直收到“正在处理”文本的原因是因为标准中的所有属性都是空的。要解决此问题,请将函数参数更改为 HttpServletRequest 请求。然后你可以创建条件对象: DatatablesCriterias criterias = DatatablesCriterias.getFromRequest(request);更改返回行以返回 DatatablesResponse.build(dataSet, criteria);现在您应该可以正确看到表格了。我已经编辑了答案以反映这些更正。
    猜你喜欢
    • 1970-01-01
    • 2020-11-05
    • 1970-01-01
    • 1970-01-01
    • 2014-07-04
    • 2021-08-30
    • 2017-10-08
    • 1970-01-01
    • 2017-02-28
    相关资源
    最近更新 更多