jsp标签(forEach select)
案例:
(一)标签库源码
select:
package com.jiangjiayan.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 javax.swing.plaf.SliderUI;
import org.apache.commons.beanutils.PropertyUtils;
public class SelectTag extends BodyTagSupport {
private List<Object> items;
private String name;
private String valueKey;//保存option中value的key
private String textKey;//保存option中text文本的key
private String Style;
private String headKey;
private String headValue;
private String selectOptionValue;//默认选中
public String getHeadValue() {
return headValue;
}
public void setHeadValue(String headValue) {
this.headValue = headValue;
}
public String getSelectOptionValue() {
return selectOptionValue;
}
public void setSelectOptionValue(String selectOptionValue) {
this.selectOptionValue = selectOptionValue;
}
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 getStyle() {
return Style;
}
public void setStyle(String style) {
Style = style;
}
public SelectTag() {
// TODO Auto-generated constructor stub
}
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;
}
@Override
public int doStartTag() throws JspException {
JspWriter out = pageContext.getOut();
try {
String html=toHTML();
System.out.println(html);
out.println(html);
} catch (Exception e) {
// TODO: handle exception
throw new RuntimeException();
}
return super.doStartTag();
}
private String toHTML() throws Exception{
StringBuffer sb=new StringBuffer();
//拼接<select name="" style="">
sb.append("<select name='"+name+"'");
sb.append("style='"+Style+"'");
sb.append(">");
//拼接<option value=""></option>
if(null!=items&&0!=items.size()) {
/*if(null!=headKey&&"".equals(headKey.trim())) {
sb.append("<option value='"+headKey+"'>"+headValue+"</option>");
}*/
Object value=null;
Object text=null;
for(Object obj:items) {
//反射
value=PropertyUtils.getProperty(obj,valueKey);
text=PropertyUtils.getProperty(obj,textKey);
System.out.println(selectOptionValue);
System.out.println(value);
/*if(value.toString().equals(selectOptionValue.trim())) {
sb.append("<option selected value='"+value+"'>"+text+"</option>");
}else {*/
sb.append("<option value='"+value+"'>"+text+"</option>");
/*}*/
}
}
sb.append("</select>");
return sb.toString();
}
}
forEach:
package com.jiangjiayan.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<Object> items;
private String var;//保存集合中元素的key
private String varStatus;//保存index,count属性的对象的key
public static class Status{
public int index=0;
public int getIndex() {
return index;
}
public void setIndex(int index) {
this.index = index;
}
public int getCount() {
return this.index+1;
}
void increment() {
this.index++;
}
}
public String getVarStatus() {
return varStatus;
}
public void setVarStatus(String varStatus) {
this.varStatus = varStatus;
}
public List<Object> getItems() {
return items;
}
public void setItems(List<Object> items) {
this.items = items;
}
public String getVar() {
return var;
}
public void setVar(String var) {
this.var = var;
}
public ForEachTag() {
// TODO Auto-generated constructor stub
}
@Override
public int doAfterBody() throws JspException {
Iterator<Object> it =(Iterator<Object>) pageContext.getAttribute("it");
while(it.hasNext()) {
if(null!=varStatus&&"".equals(varStatus)) {
Status status =(Status) pageContext.getAttribute(varStatus);
status.increment();
pageContext.setAttribute(varStatus, status);
}
Object next = it.next();
pageContext.setAttribute(var, next);
return EVAL_BODY_AGAIN;
}
return super.doAfterBody();
}
@Override
public int doStartTag() throws JspException {
if(null!=varStatus&&!"".equals(varStatus)) {
pageContext.setAttribute(varStatus, new Status());
}
if(null!=items&&0!=items.size()) {
Iterator<Object> it = items.iterator();
Object next=it.next();
pageContext.setAttribute(var, next);
pageContext.setAttribute("it", it);
return EVAL_BODY_INCLUDE;
}
return super.doStartTag();
}
}
(二)编写标签库描写文件
<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>JSTL 1.1 core library</description>
<display-name>JSTL core</display-name>
<tlib-version>1.1</tlib-version>
<short-name>z</short-name>
<uri>/zking</uri>
<tag>
<name>forEach</name>
<tag-class>com.jiangjiayan.tag.ForEachTag</tag-class>
<body-content>JSP</body-content>
<attribute>
<name>items</name>
<required>true</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
<attribute>
<name>var</name>
<required>true</required>
<rtexprvalue>false</rtexprvalue>
</attribute>
<attribute>
<name>varStatus</name>
<required>true</required>
<rtexprvalue>false</rtexprvalue>
</attribute>
</tag>
<tag>
<name>select</name>
<tag-class>com.jiangjiayan.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>
</tag>
(三)在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="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@taglib prefix="z" uri="/zking" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<%
List<String> list=new ArrayList<String>();
list.add("zs");
list.add("ls");
list.add("ww");
String names="zs";
pageContext.setAttribute("names", names);
request.setAttribute("name", list);
%>
<h2>自定义标签之out标签 </h2>
<z:out value="${name }"/>
<h2>自定义标签之if标签 </h2>
<z:if test="${names eq 'zs' }">
hahhha
</z:if>
<h2>自定义标签之forEach标签 </h2>
<%-- <ul>
<c:forEach items="${name }" var="s" varStatus="n">
<li>${s },${n.index },${n.count }</li>
</c:forEach>
</ul> --%>
<ul>
<z:forEach items="${name }" var="s" varStatus="n">
<li>${s },${n.index },${n.count }</li>
</z:forEach>
</ul>
<h2>自定义标签之set标签 </h2>
<z:set value="deptList"/>
<br>
<z:out value="${deptList }"/>
<h2>自定义标签之select标签 </h2>
<z:select items="${deptList }" valueKey="deptId" textKey="deptName" />
<br/>
<!-- <select name="" style="">
<option value="-1">---请选择---</option>
<option value="1">长沙</option>
<option selected="selected" value="2">永州</option>
<option value="3">娄底</option>
<option value="4">岳阳</option>
</select> -->
</body>
</html>
运行结果:
属性问题:java.lang.NoSuchMethodException
解决方法:在实体类添加一个无参的构造方法