标签助手类:
package com.zking.tag;
import java.io.IOException;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.BodyTagSupport;
public class OutTag extends BodyTagSupport{
private Object value;
public Object getValue() {
return value;
}
public void setValue(Object value) {
this.value = value;
}
@Override
public int doStartTag() throws JspException {
JspWriter out = pageContext.getOut();
try {
out.print(this.value);
} catch (IOException e) {
throw new RuntimeException(e);
}
return super.doStartTag();
}
}
if标签
标签助手类:
package com.zking.tag;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.BodyTagSupport;
public class IfTag extends BodyTagSupport{
private Boolean test;
public Boolean getTest() {
return test;
}
public void setTest(Boolean test) {
this.test = test;
}
@Override
public int doStartTag() throws JspException {
if(test) { //如果前端表达式为true,执行标签体并且输出
return EVAL_BODY_INCLUDE;
}else { //为false,跳过标签体
return SKIP_BODY;
}
}
}
foreach标签
标签助手类:
package com.zking.tag;
import java.util.Iterator;
import java.util.List;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.BodyTagSupport;
public class ForEachTag extends BodyTagSupport{
private List
Select标签:
实体类:
package com.zking.tag;
public class Dept {
private Integer did;
private String dname;
public Integer getDid() {
return did;
}
public void setDid(Integer did) {
this.did = did;
}
public String getDname() {
return dname;
}
public void setDname(String dname) {
this.dname = dname;
}
public Dept() {
// TODO Auto-generated constructor stub
}
public Dept(Integer did, String dname) {
this.did = did;
this.dname = dname;
}
@Override
public String toString() {
return "Dept [did=" + did + ", dname=" + dname + "]";
}
}
标签助手类:
package com.zking.tag;
import java.lang.reflect.InvocationTargetException;
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 List items;//数据源
private String valueName;//数据源中写入到select标签option中value值对应的属性名
private String textName;//数据源中写入到select标签option中文本值对应的属性名
private String name;
private String headText;
private String selectOptionValue; //默认选中的value值
private String cssStyle; //样式
public List getItems() {
return items;
}
public void setItems(List items) {
this.items = items;
}
public String getValueName() {
return valueName;
}
public void setValueName(String valueName) {
this.valueName = valueName;
}
public String getTextName() {
return textName;
}
public void setTextName(String textName) {
this.textName = textName;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getHeadText() {
return headText;
}
public void setHeadText(String headText) {
this.headText = headText;
}
public String getSelectOptionValue() {
return selectOptionValue;
}
public void setSelectOptionValue(String selectOptionValue) {
this.selectOptionValue = selectOptionValue;
}
public String getCssStyle() {
return cssStyle;
}
public void setCssStyle(String cssStyle) {
this.cssStyle = cssStyle;
}
@Override
public int doStartTag() throws JspException {
JspWriter out = pageContext.getOut();
try {
out.println(toHtml());
} catch (Exception e) {
throw new RuntimeException(e);
}
return super.doStartTag();
}
public String toHtml() throws IllegalAccessException, InvocationTargetException, NoSuchMethodException {
StringBuffer sb = new StringBuffer();
if(null==items || 0==items.size()) {//如果数据源为空,或者长度为0
//就直接返回一个空字符串
return "";
}
sb.append("
这里为了方便是通过反射工具类来反射对象获取属性值,既然是工具类就要导jar包,如图所示:
工具类的两个反射获取属性方法
PropertyUtils.getProperty(bean, name);返回Object
BeanUtils.getProperty(bean, name);返回String
配置mytag.tld文件
1.0
1.2
z
/zking
out
com.zking.tag.OutTag
jsp
value
true
true
if
com.zking.tag.IfTag
jsp
test
true
true
forEach
com.zking.tag.ForEachTag
jsp
items
true
true
var
true
false
varStatus
false
false
select
com.zking.tag.SelectTag
empty
items
true
true
name
false
false
valueName
true
false
textName
true
false
headText
false
false
selectOptionValue
false
false
cssStyle
false
false
index.jsp
<%@page import="com.zking.tag.Dept"%>
<%@page import="java.util.ArrayList"%>
<%@page import="java.util.List"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@taglib prefix="z" uri="/zking" %>
Insert title here
<%
String name = "大黄";
request.setAttribute("name",name);
List list = new ArrayList();
list.add("A");
list.add("B");
list.add("C");
list.add("D");
list.add("E");
request.setAttribute("list", list);
List deptlist = new ArrayList();
deptlist.add(new Dept(1,"财务部"));
deptlist.add(new Dept(2,"教学部"));
deptlist.add(new Dept(3,"就业部"));
deptlist.add(new Dept(4,"教质部"));
request.setAttribute("deptlist", deptlist);
%>
来了老弟
走了老弟
${s },${vs.index },${vs.count }
${s },${vs.index },${vs.count }
效果图:



