J2EE自定义标签2

jar包提取链接: https://pan.baidu.com/s/1q3piAsbjkOx-RriWtrL31Q
提取码: zsbb

  1. 自定义标签开发步骤
    1.1 助手类
    1.2 tld
    1.3 taglib
  2. UI标签
    z:select //比较麻烦的
  3. 数据标签
    z:set
    tid文件
<?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>liubiao 1.1 core library</description>
	<display-name>liubiao core</display-name>
	<tlib-version>1.1</tlib-version>
	<short-name>z</short-name>
	<uri>/liubiao</uri>
	
</taglib>

set标签配置

<tag>
	    <name>set</name>//标签名
	    <tag-class>com.liubiao.tag.SetTag</tag-class>
	    <body-content>empty</body-content>
	    <attribute>
	        <name>value</name>//属性值
	        <required>true</required>//是否必填
	        <rtexprvalue>false</rtexprvalue>//是否支持表达式
	    </attribute>
	</tag>

set标签助手类

package com.liubiao.tag;

import java.util.List;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.BodyTagSupport;

import com.liubiao.dao.DeptDao;
import com.liubiao.entity.Dept;

public class SetTag extends BodyTagSupport {
	
	private String value;//保存数据的key
	private DeptDao deptDao=new DeptDao();//数据库获取方法类
	
	public SetTag() {
		super();
	}

	public String getValue() {
		return value;
	}

	public void setValue(String value) {
		this.value = value;
	}

	public DeptDao getDeptDao() {
		return deptDao;
	}

	public void setDeptDao(DeptDao deptDao) {
		this.deptDao = deptDao;
	}
	
	//为了方便为连接数据库获取数据
	@Override
	public int doStartTag() throws JspException {
		List<Dept> list = deptDao.list();
		pageContext.setAttribute(value, list);
		return super.doStartTag();
	}
	
}

dao方法类

package com.liubiao.dao;

import java.util.ArrayList;
import java.util.List;

import com.liubiao.entity.Dept;

public class DeptDao {
	
	public List<Dept> list(){
		//访问数据库
		List<Dept> list=new ArrayList<Dept>();
		list.add(new Dept(1,"教质部"));
		list.add(new Dept(2,"教学部"));
		list.add(new Dept(3,"财务部"));
		list.add(new Dept(4,"就业部"));
		return list;
	}
	
}

select自定义标签配置

<tag>
	    <name>select</name>
	    <tag-class>com.liubiao.tag.SelectTag</tag-class>
	    <body-content>empty</body-content>
	    <attribute>
	        <name>items</name>
	        <required>true</required>
	        <rtexprvalue>true</rtexprvalue>
	    </attribute>
	    <attribute>
	        <name>name</name>
	        <required>false</required>
	        <rtexprvalue>false</rtexprvalue>
	    </attribute>
	    <attribute>
	        <name>valueKey</name>
	        <required>true</required>
	        <rtexprvalue>false</rtexprvalue>
	    </attribute>
	    <attribute>
	        <name>textKey</name>
	        <required>true</required>
	        <rtexprvalue>false</rtexprvalue>
	    </attribute>
	    <attribute>
	        <name>cssStyle</name>
	        <required>false</required>
	        <rtexprvalue>false</rtexprvalue>
	    </attribute>
	    <attribute>
	        <name>headKey</name>
	        <required>false</required>
	        <rtexprvalue>false</rtexprvalue>
	    </attribute>
	    <attribute>
	        <name>headValue</name>
	        <required>false</required>
	        <rtexprvalue>false</rtexprvalue>
	    </attribute>
	    <attribute>
	        <name>selectedOptionValue</name>
	        <required>false</required>
	        <rtexprvalue>false</rtexprvalue>
	    </attribute>
	</tag>

select下拉框自定义标签助手类

package com.liubiao.tag;

import java.util.List;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.BodyTagSupport;

import org.apache.commons.beanutils.PropertyUtils;

public class SelectTag extends BodyTagSupport {
	
	
	private static final Object Object = null;
	private List<Object> items;//下拉框内容集合
	private String name;//name值
	private String valueKey;//保存option中的value的key
	private String textKey;//保存option中text文本的key
	private String cssStyle;//css样式
	private String headKey;//option请选择的value值
	private String headValue;//option的文本值(自己设置)
	private String selectedOptionValue;//默认选中
	
	public SelectTag() {
		super();
	}

	public List<Object> getItems() {
		return items;
	}

	public void setItems(List<Object> items) {
		this.items = items;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getValueKey() {
		return valueKey;
	}

	public void setValueKey(String valueKey) {
		this.valueKey = valueKey;
	}

	public String getTextKey() {
		return textKey;
	}

	public void setTextKey(String textKey) {
		this.textKey = textKey;
	}
	
	public String getCssStyle() {
		return cssStyle;
	}

	public void setCssStyle(String cssStyle) {
		this.cssStyle = cssStyle;
	}
	
	public String getHeadKey() {
		return headKey;
	}

	public void setHeadKey(String headKey) {
		this.headKey = headKey;
	}

	public String getHeadValue() {
		return headValue;
	}

	public void setHeadValue(String headValue) {
		this.headValue = headValue;
	}
	
	public String getSelectedOptionValue() {
		return selectedOptionValue;
	}

	public void setSelectedOptionValue(String selectedOptionValue) {
		this.selectedOptionValue = selectedOptionValue;
	}

	@Override
	public int doStartTag() throws JspException {
		JspWriter out = pageContext.getOut();
		try {
			//调用标签拼接方法
			String html = toHtml();
			//打印到jsp界面上
			out.println(html);
		} catch (Exception e) {
			throw new RuntimeException();
		}
		return super.doStartTag();
	}
	
	private String toHtml() throws Exception, NoSuchMethodException {
		//拼接用的
		StringBuffer sb=new StringBuffer();
		sb.append("<select name='"+name+"'");
		sb.append(" style='"+cssStyle+"'");
		sb.append(">");
		//如果集合有数据默认选中"请选择"列
		if(null!=items && 0!=items.size()) {
			if(null!=headKey && !"".equals(headKey.trim())) {
				sb.append("<option value='"+headKey+"'>"+headValue+"</option>");
			}
			//option的value值
			Object value=null;
			//option文本内容
			Object text=null;
			//遍历集合进行拼接
			for (Object object : items) {
				//可以写反射代码进行数据反射
				//我这里用了2个jar包能不用写反射代码
				value=PropertyUtils.getProperty(object, valueKey);
				text=PropertyUtils.getProperty(object, textKey);
				if(value.toString().equals(selectedOptionValue.trim())) {
					sb.append("<option selected value='"+value+"'>"+text+"</option>");
				}else {
					sb.append("<option value='"+value+"'>"+text+"</option>");
				}
			}
		}
		sb.append("</select>");
		return sb.toString();
	}
	
}

jsp界面代码

<%@page import="java.util.ArrayList"%>
<%@page import="java.util.List"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
    <%@taglib prefix="l" uri="/liubiao" %>>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>标签</title>
</head>
<body>

	<h2>自定义标签set</h2>
	<l:set value="deptList"/>
	<l:out value="${deptList }"/>
	
	<h2>自定义标签select</h2>
	<l:select selectedOptionValue="3" headKey="-1" headValue="----请选择----" cssStyle="width:150px" items="${deptList }" valueKey="deptId" textKey="deptName"/>
	
</body>
</html>

jsp界面结果图
在这里插入图片描述

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值