2015-02-24 69 views
1

我使用Thymeleaf的Spring Boot,现在我想添加蒲公英数据表,但它不起作用。Spring Boot + Thymeleaf +蒲公英配置不工作

这里是我的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并配置了以下豆:

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

我认为蒲公英的servlet没有被调用。 命名空间不处理。 enter image description here

回答

8

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

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

首先将这些依赖关系添加到您的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> 

然后添加这些配置。你必须为方言创造豆子。我想你错过了..

@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; 
    } 
} 
+0

你是对的!非常感谢。 – leonardoborges 2015-02-24 20:36:32

+0

我遵循这一个,几乎所有的工作,除了DataTables只是显示“处理”。我得到的数据没有问题,但它不反映在数据表中。它只显示“处理”。 – Incognito 2015-08-13 05:05:27

+0

你可以检查从ajax查询得到的服务器响应吗?可能是它给出了一个错误。加载页面时,请从Chrome控制台的网络选项卡中进行检查。 – 2015-08-13 12:28:44

相关问题