step 1 定义静态工具体类
package com.tianxia.sms.helper;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
import java.util.concurrent.locks.ReadWriteLock;
import java.util.concurrent.locks.ReentrantReadWriteLock;
import javax.annotation.PostConstruct;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
@Component
public class DictHelper {private static final Logger logger = LoggerFactory.getLogger(DictHelper.class);
@Autowired
private SysDictService sysDictService;
private static final ConcurrentMap<String, List<SysDict>> dictMap = new ConcurrentHashMap<String, List<SysDict>>();
private static final ReadWriteLock lock = new ReentrantReadWriteLock();
public staticList<SysDict> getDictList(String dictType){
lock.readLock().lock();
try {
return dictMap.get(dictType);
}finally{
lock.readLock().unlock();
}
}
public static List<SysDict> getDictListExcludeKeys(String dictType, String... excludes){
lock.readLock().lock();
try {
List<SysDict> allDicts = dictMap.get(dictType);
List<SysDict> dicts = new ArrayList<SysDict>();
for(SysDict d : allDicts) {
boolean addFlag = true;
for(String ex : excludes) {
if(d.getDictKey().equals(ex)) {
addFlag = false;
break ;
}
}
if(addFlag) {
dicts.add(d);
}
}
return dicts;
}finally{
lock.readLock().unlock();
}
}
public static String getDictValue(String dictType, String dictKey){
if(StringUtils.isBlank(dictKey)) {
return null;
}
String dictValue = null;
List<SysDict> dictlist = getDictList(dictType);
for(SysDict dict:dictlist){
if(dictKey.equals(dict.getDictKey())){
dictValue = dict.getDictValue();
break;
}
}
return dictValue;
}
public static String getDictKey(String dictType, String dictValue){
if(StringUtils.isBlank(dictValue)) {
return null;
}
String dictKey = null;
List<SysDict> dictlist = getDictList(dictType);
for(SysDict dict:dictlist){
if(dictValue.equalsIgnoreCase(dict.getDictValue())){
dictKey = dict.getDictKey();
break;
}
}
return dictKey;
}
@PostConstruct
public void init(){
List<SysDict> list = sysDictService.getAllDict();
lock.writeLock().lock();
try {
dictMap.clear();
for(SysDict dict:list){
if(dictMap.containsKey(dict.getDictType())){
dictMap.get(dict.getDictType()).add(dict);
}else{
List<SysDict> dicts = new ArrayList<SysDict>();
dicts.add(dict);
dictMap.put(dict.getDictType(), dicts);
}
}
}finally{
lock.writeLock().unlock();
}
}
/**
*
* 每隔30分钟,重新加载一次数据
*
* @author yangyulei
* @version
*/
@Scheduled(cron="0 0/30 * * * ?")
public void reload(){
logger.info("======================================reload dict data start================================");
this.init();
logger.info("======================================reload dict data end================================");
}
}
step 2 freeMarker util工具类
import org.springframework.ui.Model;
import freemarker.ext.beans.BeansWrapperBuilder;
import freemarker.template.Configuration;
import freemarker.template.TemplateHashModel;
/**
*
* Title: Class BeansWrapperUtil.java
* Description: 用于在 freemarker 模板中使用静态方法、常量、或枚举
*
* @author GuoYF
* @version 0.0.1
*/
public class BeansWrapperUtil {
public static TemplateHashModel statics = new BeansWrapperBuilder(Configuration.VERSION_2_3_22).build().getStaticModels();
public static TemplateHashModel enums = new BeansWrapperBuilder(Configuration.VERSION_2_3_22).build().getEnumModels();
public static void enabledStatics(Model model) {
model.addAttribute("statics", statics);
}
public static void enabledEnums(Model model) {
model.addAttribute("enums", enums);
}
}
step 3 Action设置静态方法 freeMarker页面调用
@Controller
@RequestMapping("/admin")
public class ProductionOrderPlanController {
@RequestMapping("/toAdd")
public String toAdd(Model model){
BeansWrapperUtil.enabledStatics(model);//关键点
return "production/add";
}
}
step 4 html freeMarker页面调用
add.html
<label class="control-label x105">优先级别:</label>
<select name="priorityLevel" class="required" data-toggle="selectpicker" data-rule="优先级别:required">
<#list statics["com.zhushang.cims.helper.DictHelper"].getDictList("ProductPlanLevel")as dict>
<option value ="${dict.dictKey}">${dict.dictValue}</option>
</#list>
</select>