上一篇我们讲了自定义标签的通常情况的实现,这次记录一下其他场景:如果业务中,自定义标签渲染的时候,需要与service接口进行交互,而service接口由spring进行管理,我们该怎么实现(此时继承TagSupport就不好使了),需要使用到spring提供的自定义标签类RequestContextAwareTag。
此标签的业务是通过页面传入的值来判断自定义标签内的,内容是否渲染。
首先依然是先建立.tld文件:
attribute内定义传参信息
<?xml version="1.0" encoding="UTF-8" ?>
<taglib xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-jsptaglibrary_2_0.xsd"
version="2.0">
<description>信用平台通用设置</description>
<display-name>permission core</display-name>
<tlib-version>1.0</tlib-version>
<short-name>system</short-name>
<uri>http://www.lnlic.com</uri>
<tag>
<description>双公示对象标签</description>
<name>dataquerySetting</name>
<tag-class>com.lnlic.credit.setting.DataqueryTag</tag-class>
<body-content>JSP</body-content>
<attribute>
<name>value</name>
<required>true</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
</tag>
</taglib>
然后,继承spring提供的RequestContextAwareTag类,重写doStartTagInternal方法:
package com.lnlic.credit.setting;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import org.hlj.framework.core.page.Page;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.servlet.tags.RequestContextAwareTag;
import com.alibaba.fastjson.JSONObject;
import com.lnlic.credit.common.core.constant.LabelinfoConstant;
import com.lnlic.credit.setting.facade.provider.DataqueryTagFacade;
import com.lnlic.credit.setting.facade.provider.LabeldatarelationFacade;
import com.lnlic.credit.setting.vo.query.LabeldatarelationQuery;
/**
* <p>双公示标签</p>
* @ClassName: DataqueryTag
*/
@SuppressWarnings({"rawtypes","unchecked"})
public class DataqueryTag extends RequestContextAwareTag implements Serializable {
private static final long serialVersionUID = 6958512489709325566L;
private final static Logger logger = LoggerFactory.getLogger(DataqueryTag.class);
private LabeldatarelationFacade labeldatarelationFacade;
private DataqueryTagFacade dataqueryTagFacade;
// 对象名
private String value;
public String getValue() {
return value;
}
public void setValue(String value) {
this.value = value;
}
@Override
protected int doStartTagInternal() throws Exception {
logger.debug("DataqueryTag doStartTag 双公示标签 开始");
logger.debug("DataqueryTag doStartTag 双公示标签 value:{}", value);
// 真:返回EVAL_BODY_INCLUDE(执行标签);假:返回SKIP_BODY(跳过标签不执行)
boolean result = false;
// 接收页面传参
JSONObject jsonObject = JSONObject.parseObject(value);
if (jsonObject!=null) {
/*获取bean*/
// WebApplicationContext webApplicationContext = ContextLoader.getCurrentWebApplicationContext();
if(labeldatarelationFacade==null){
labeldatarelationFacade = (LabeldatarelationFacade)this.getRequestContext().getWebApplicationContext().getBean("labeldatarelationFacade");
// labeldatarelationFacade = (LabeldatarelationFacade) webApplicationContext.getBean("labeldatarelationFacade");
}
if(dataqueryTagFacade==null){
dataqueryTagFacade = (DataqueryTagFacade)this.getRequestContext().getWebApplicationContext().getBean("dataqueryTagFacade");
}
String xybsm = jsonObject.getString("xybsm");
String labelname = jsonObject.getString("labelcode");
// 根据标签类型查询来源表集合
LabeldatarelationQuery labeldatarelationQuery = new LabeldatarelationQuery();
// 守信激励SXJL、失信惩戒SXCJ
labeldatarelationQuery.setLabelname(labelname);
labeldatarelationQuery.setIsenable(LabelinfoConstant.ISENABLE_YES);
Page page = labeldatarelationFacade.findPage(labeldatarelationQuery);
// 根据信用标识码和来源表查询是否存在记录
List<Map<String, Object>> list = page.getResult();
List<String> tablenames = new ArrayList<>();
for (Map<String, Object> map : list) {
tablenames.add((String) map.get("etablename"));
}
result = dataqueryTagFacade.isExistCreditIndex(xybsm, tablenames);
}
logger.debug("DataqueryTag doStartTag 双公示标签 result:{}", result);
logger.debug("DataqueryTag doStartTag 双公示标签 结束");
return result?EVAL_BODY_INCLUDE:SKIP_BODY;
}
}
注意,其中LabeldatarelationFacade、DataqueryTagFacade是service层的业务接口,引入的时候我们无法通过@Autowired注入。
返回EVAL_BODY_INCLUDE(执行标签);返回SKIP_BODY(跳过标签不执行)
jsp页面使用,先引入tld文件
然后在页面中使用标签:
最终效果就是根据自定义标签的实现类的返回值,来控制页面中自定义标签内的内容是否被渲染出来。