自定义forEach与Select标签
编码前环境
这里我导入了四个jar包,上面两个是反射所需jar包,下面两个是c标签所需jar包
jar包:
c标签所用jar包:c标签jar包 提取码:n3gc
反射jar包:反射jar 提取码:y65b
上篇jsp自定义标签:自定义jsp标签(1)
forEach自定义标签
1.创建标签助手类
package day02;
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() { //s.count
return this.index+1;
}
//increment 主键自增
void increment() {
this.index++;
}
}
public ForEachTag() {}
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 String getVarStatus() {
return varStatus;
}
public void setVarStatus(String varStatus) {
this.varStatus = varStatus;
}
@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();
}
}
标签库配置:
<tag>
<name>Foreach</name>
<tag-class>day02.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>false</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="d" uri="/she" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<%
List<String> list=new ArrayList<String>();
list.add("kk");
list.add("uu");
list.add("bb");
String name="zs";
pageContext.setAttribute("names",name);
request.setAttribute("name", list);
%>
<h2>------------仿写c标签的forEach---------</h2>
<ul>
<d:Foreach items="${name }" var="n" varStatus="u">
<li>${n },${u.index },${u.count }</li>
</d:Foreach>
</ul>
</body>
</html>
输出结果:
Select自定义标签
1.这里我用一个dao方法模拟数据库
Deptdao
package Dao;
import java.util.ArrayList;
import java.util.List;
import 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,"巴黎"));
list.add(new Dept(5,"香港"));
list.add(new Dept(6,"上海"));
return list;
}
}
Dept实体类
package entity;
import java.io.Serializable;
public class Dept implements Serializable {
private static final long serialVersionUID = -6977823780021900851L;
private Integer deptId;
private String deptName;
public Dept() {}
public Dept(Integer deptId, String deptName) {
this.deptId = deptId;
this.deptName = deptName;
}
public Integer getDeptId() {
return deptId;
}
public void setDeptId(Integer deptId) {
this.deptId = deptId;
}
public String getDeptName() {
return deptName;
}
public void setDeptName(String deptName) {
this.deptName = deptName;
}
@Override
public String toString() {
return "Dept [deptId=" + deptId + ", deptName=" + deptName + "]";
}
}
Select标签助手类
package day02;
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<Object> items;
private String name;
private String valueKey;//保存option中value的key
private String textKey;//保存option中text文本key
private String cssStyle;//样式
private String headKey;//默认展示的key
private String headVale;默认展示文本
private String sov;//默认选中的key
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 getHeadVale() {
return headVale;
}
public void setHeadVale(String headVale) {
this.headVale = headVale;
}
public String getSov() {
return sov;
}
public void setSov(String sov) {
this.sov = sov;
}
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 SelectTag() {}
@Override
public int doStartTag() throws JspException {
JspWriter out = pageContext.getOut();
try {
String html = toHtml();
out.println(html);
} catch (Exception e) {
throw new RuntimeException(e);
}
return super.doStartTag();
}
private String toHtml() throws Exception {
StringBuffer sb=new StringBuffer();
//拼接<select name="" style="">
sb.append("<select name='"+name+"' ");
sb.append("<style='"+cssStyle+"' ");
sb.append(">");
//拼接<option value=""></option>
if(null!=items && 0!=items.size()) {
if(null!=headKey && !"".equals(headKey.trim())) {
sb.append("<option value='"+headKey+"'>"+headVale+"</option>");
}
Object value=null;
Object text=null;
for (Object object : items) {
//使用反射拿到他的值
value=PropertyUtils.getProperty(object, valueKey);
text=PropertyUtils.getProperty(object, textKey);
if(value.toString().equals(sov.trim())) {
sb.append("<option selected value='"+value+"'>"+text+"</option>");
}else {
sb.append("<option value='"+value+"'>"+text+"</option>");
}
}
}
sb.append("</select>");
return sb.toString();
}
}
表情库的配置:
<tag>
<name>select</name>
<tag-class>day02.SelectTag</tag-class>
<body-content>JSP</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>headVale</name>
<required>false</required>
<rtexprvalue>false</rtexprvalue>
</attribute>
<attribute>
<name>sov</name>
<required>false</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="d" uri="/she" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<h2>------------仿写标签select---------</h2>
<d:select items="${List }" valueKey="deptId" textKey="deptName"
cssStyle="width:150px" headKey="-1" headVale="-----请选择--------" sov="2"></d:select>
<select name="" style="">
<option value="1">dfadfa</option>
<option value="2">dfadfb</option>
<option value="3">dfadfc</option>
<option value="4">dfadfd</option>
</select>
</body>
</html>