本文介绍了Spring Boot + Thymeleaf + Dandelion配置不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在将Spring Boot与Thymeleaf一起使用,现在我想添加Dandelion数据表,但这是行不通的.

I'm using Spring Boot with Thymeleaf and now I want to add Dandelion datatables, but it doesn't work.

这是我的Maven依赖项:

Here is my maven dependencies:

<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:

I'm following this guide http://dandelion.github.io/dandelion/docs/installation/thymeleaf.html and configured the following beans:

@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进行了测试:

I have made this HTML for testing:

<!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>

我认为未调用Dandelion的servlet.命名空间未处理.

I think Dandelion's servlet is not called.The namespace is not processed.

推荐答案

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

There are several mistakes. Most of them are just same as what I did when i first used dandelion data tables. :)

我正在为下面的每个代码编写完整的简单示例,以供将来任何人参考.因此,请确保仅将丢失的项目添加到您的项目中

首先将这两个依赖项添加到您的Maven中. (您已经有了第一个.因此添加后一个.)

First add both these dependencies to your maven. (You already have the first. So add the later.)

<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.我想你错过了..

Then add these configurations. You have to create Beans for the dialects. I think you missed it..

@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

Here you are using server-side processing. This requires your controller to have a mapping on /clientes which returns 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是您要传递给蒲公英数据表的对象

MyObject is the object you are passing to dandelion data tables

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;
    }
}

这篇关于Spring Boot + Thymeleaf + Dandelion配置不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

05-21 19:04