我们有一个使用 springboot(ver 1.5) 和 maven 的基本 java web 应用程序。我们使用百里香和蒲公英作为表示层。添加了蒲公英,因此我们可以使用数据表。数据表工作正常,可排序、可搜索等。唯一的问题是导出到 excel 功能不起作用(单击 xslx 链接时为空指针)。我已按照此处找到的教程进行操作。
https://dandelion.github.io/components/datatables/1.1.0/docs/html/#6-4-activating-export
示例代码和我的实现的一个区别是我使用的是 springboot 并且没有 web.xml 文件。我认为我的问题可能出在设置过滤器的配置文件中。任何人都认为这有任何问题。另外,如果在 springboot 应用程序中使用蒲公英 excel 导出,请分享您的配置文件。谢谢!
蒲公英配置.java 文件:
package com.gatewaysvc.configuration;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import com.github.dandelion.core.web.DandelionFilter;
import com.github.dandelion.core.web.DandelionServlet;
import com.github.dandelion.datatables.core.web.filter.DatatablesFilter;
import com.github.dandelion.datatables.thymeleaf.dialect.DataTablesDialect;
import com.github.dandelion.thymeleaf.dialect.DandelionDialect;
@Configuration
public class DandelionConfiguration {
@Bean
public DandelionDialect dandelionDialect()
{
return new DandelionDialect();
}
@Bean
public DataTablesDialect dataTablesDialect()
{
return new DataTablesDialect();
}
// Dandelion filter definition and mapping
@Bean
public FilterRegistrationBean filterRegistrationBean()
{
FilterRegistrationBean filterRegistrationBean = new FilterRegistrationBean();
filterRegistrationBean.setFilter(new DandelionFilter());
filterRegistrationBean.addUrlPatterns("/*");
return filterRegistrationBean;
}
//Dandelion servlet definition and mapping
@Bean
public ServletRegistrationBean servletRegistrationBean()
{
ServletRegistrationBean servletRegistrationBean = new ServletRegistrationBean();
servletRegistrationBean.setServlet(new DandelionServlet());
servletRegistrationBean.addUrlMappings("/dandelion-assets/*");
servletRegistrationBean.setName("dandelionServlet");
return servletRegistrationBean;
}
@Bean
public DatatablesFilter dataTablesFilter () {
return new DatatablesFilter();
}
}
html页面片段:
<div class="container" id="dataTables">
<div th:if="${not #lists.isEmpty(deviceActivityFormBean.inbound) and #lists.isEmpty(deviceActivityFormBean.outbound) and #lists.isEmpty(deviceActivityFormBean.both)}" class="panel panel-primary">
<div class="panel-body">
<table class="table display table-striped table-hover cell-border" dt:table="true" id="deviceInboundTable" dt:export="xlsx">
<thead>
<tr>
<th>Device Id 000</th>
<th>Create TMSTP</th>
<th>Message</th>
<th>IP Address</th>
<th>HTTP Resp Code</th>
<th>Server</th>
<th>Duration (ms)</th>
<th>Protocol</th>
</tr>
</thead>
<tbody>
<tr th:each="dvc : ${deviceActivityFormBean.inbound}">
<td dt:xlsx="${dvc.deviceId}" th:text="${dvc.deviceId}"></td>
<td th:text="${dvc.createTime}"></td>
<td id ="mssgInbound" th:text="${dvc.mssg}"></td>
<td th:text="${dvc.ipMssg}"></td>
<td th:text="${dvc.httpResp}"></td>
<td th:text="${dvc.server}"></td>
<td th:text="${dvc.duration}"></td>
<td th:text="${dvc.protocol}"></td>
</tr>
</tbody>
</table>
</div>
<div>