jsp自定义标签,并调用spring管理的业务接口

上一篇我们讲了自定义标签的通常情况的实现,这次记录一下其他场景:如果业务中,自定义标签渲染的时候,需要与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文件

然后在页面中使用标签:

最终效果就是根据自定义标签的实现类的返回值,来控制页面中自定义标签内的内容是否被渲染出来。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值