欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

jstl自定义标签

程序员文章站 2022-06-08 18:04:44
...

1、引入jstl.jar

<!-- JSTL标签类 -->
		<dependency>
			<groupId>jstl</groupId>
			<artifactId>jstl</artifactId>
			<version>1.2</version>
		</dependency>

2、自定义标签处理类

package com.loan.security.web.taglib;

import java.util.List;

import org.springframework.web.bind.annotation.ResponseBody;

import com.loan.security.entity.Dictionary;
import com.loan.security.service.DictionaryService;
import com.loan.security.spring.SpringUtils;
public class DictionaryFunction {
	private static DictionaryService dictionaryService;
	
	public static @ResponseBody List<Dictionary> getDictionarysByCode(String code) {
		Dictionary dic=new Dictionary();
		dic.setCode(code);
		return getDictionaryService().find(dic);
	}
	public static DictionaryService getDictionaryService() {
		if(dictionaryService == null) {
			dictionaryService = SpringUtils.getBean(DictionaryService.class);
		}
		return dictionaryService;
	}
}

3、标签库描述文件(TLD)

<?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>functions</description>
    <tlib-version>3.0</tlib-version>
    <short-name>fn</short-name>
    <function>
        <description>根据code查询字典列表</description>
        <name>getDictionarysByCode</name>
        <function-class>com.loan.security.web.taglib.DictionaryFunction</function-class>
        <function-signature>java.util.List getDictionarysByCode(java.lang.String)</function-signature>
    </function>

</taglib>
4、jsp中引入并使用自定义标签

<%@ page contentType="text/html;charset=UTF-8" language="java"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@ taglib prefix="dictionary"
	uri="/WEB-INF/tld/dictionary_function.tld"%>
<%@taglib prefix="shiro" uri="http://shiro.apache.org/tags"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<title></title>
<script type="text/javascript"
	src="${pageContext.request.contextPath}/static/common/easy-ui/js/jquery.min.js"></script>
</head>
<body style="margin: 0px; padding: 0px; height: 100%; font-family: 微软雅黑;">
	<div style="padding: 10px;">
		<label style="width: 15%">类型:</label> 
		<select style="width: 30%" name="type" class="easyui-combobox" required="true">
			<c:forEach items="${dictionary:getDictionarysByCode('0003')}" var="dic">
				<option value="${dic.name}">${dic.name}</option>
			</c:forEach>
		</select> 
	</div>
</body>
</html>