1,通过spring bean 配置文件将 表头模板放到xml 文件里面
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean name="userListName" class="com.ali.pims.template.Columns">
<property name="field" value="name"></property>
<property name="title" value="名称"></property>
</bean>
<bean name="userListLogin" class="com.ali.pims.template.Columns">
<property name="field" value="loginName"></property>
<property name="title" value="登录名"></property>
</bean>
<bean name="userMenu" class="com.ali.pims.template.Columns">
<property name="field" value="loginName"></property>
<property name="title" value="登录名"></property>
</bean>
<bean name="userListTemplate" class="com.ali.pims.template.ListTemplate">
<property name="columns">
<list>
<ref bean="userListName" />
<ref bean="userListLogin" />
</list>
</property>
<property name="name" value="用户"/>
<property name="resourcesUrl" value="user"></property>
<property name="imgResources">
<map>
<entry key="pechorin" value="pechorin@hero.org"/>
<entry key="raskolnikov" value="raskolnikov@slums.org"/>
<entry key="stavrogin" value="stavrogin@gov.org"/>
<entry key="porfiry" value="porfiry@gov.org"/>
</map>
</property>
</bean>
</beans>
public class ListTemplate {
private List<Columns> columns;
private String title;
/**
* 资源路径
*/
private String resourcesUrl;
/**
* 名称
* @return
*/
private String name;
/**
* 图片
*/
private Map<String,TemplateResources> imgResources;
public Map<String, TemplateResources> getImgResources() {
return imgResources;
}
public void setImgResources(Map<String, TemplateResources> imgResources) {
this.imgResources = imgResources;
}
public String getResourcesUrl() {
return resourcesUrl;
}
public void setResourcesUrl(String resourcesUrl) {
this.resourcesUrl = resourcesUrl;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public List<Columns> getColumns() {
return columns;
}
public void setColumns(List<Columns> columns) {
this.columns = columns;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
}
public class Columns {
private String field;
private String title;
public String getField() {
return field;
}
public void setField(String field) {
this.field = field;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
}
实例化bean 的方式 读取模板
ListTemplate template = BeansUtils.getBean("userListTemplate");
model.addAttribute("columns", JSON.toJSON(template.getColumns()));
<!-- Latest compiled and minified JavaScript -->
<script src="https://unpkg.com/bootstrap-table@1.15.3/dist/bootstrap-table.min.js"></script>
<!-- Latest compiled and minified Locales -->
<script src="https://unpkg.com/bootstrap-table@1.15.3/dist/locale/bootstrap-table-zh-CN.min.js"></script>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<table id="table"></table>
</body>
<script type="text/javascript">
$('#table').bootstrapTable({
columns: JSON.parse('${columns}')
})
</script>
</html>