[英]Spring Boot + Thymeleaf + Dandelion configuration not working
我正在將Spring Boot與Thymeleaf一起使用,現在我想添加Dandelion數據表,但是它不起作用。
這是我的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>
我認為未調用Dandelion的servlet。 命名空間未處理。
有幾個錯誤。 它們中的大多數與我第一次使用蒲公英數據表時所做的相同。 :)
我正在為下面的每個代碼編寫完整的簡單示例,以供將來任何人參考。 因此,請確保僅將丟失的項目添加到項目中
首先將這兩個依賴項添加到您的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;
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.