目录
一,z:forEach标签
二,z:select标签
大纲:
自定义标签库:
二,select标签的使用:
for zhoujuan.com.tag.ForeachTag JSP itmes true true var true true select zhoujuan.com.tag.SelectTag JSP itmes true false textVal true false textKey true false headerTextVal false false headerTextKey true true selectedVal false true id false false name false false
var itmes:
* 分析有两个属性
* itmes:List
package zhoujuan.com.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 String var;
private List
二,select标签的使用:
<%@page import="java.util.ArrayList"%>
<%@page import="zhoujan.entity.Teavher"%>
<%@page import="java.util.List"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib uri="http://jsp.veryedu.ruojuan" prefix="r" %>
Insert title here
<%-- zhouj
true
false
--%>
<%
List list=new ArrayList<>();
list.add(new Teavher("t001","周一"));
list.add(new Teavher("t002","周二"));
list.add(new Teavher("t003","周三"));
list.add(new Teavher("t004","周四"));
request.setAttribute("list", list);
%>
${t.tid}:${t.name}
实例化:id和name
package zhoujan.entity;
public class Teavher {
private String tid;
private String name;
public String getTid() {
return tid;
}
public void setTid(String tid) {
this.tid = tid;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Teavher(String name) {
super();
this.name = name;
}
public Teavher(String tid, String name) {
super();
this.tid = tid;
this.name = name;
}
public Teavher() {
super();
}
}
编写代码进行运行
* 分析:
* 1.后台要遍历->数据源->itmes
* 2.需要一个对象的属性代表下拉框对应展示的内容->textval
* 3.需要一个对象的属性代表下拉框对应的value值->textval
* 4.默认的头部选项展示内容->headerTextVal
* 5.默认的头部选项值->headerTextVal
* 6.数据库中存贮的值,为了方便数据回显->selectedVal
* 7.id
* 8.name
* 9.cssStyle...
package zhoujuan.com.tag;
import java.lang.reflect.Field;
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 textVal;//teacherdename
private String textKey;
private String headerTextVal;
private String headerTextKey;
private String selectedVal;
private String id;
private String name;
@Override
public int doStartTag() throws JspException {
JspWriter out = pageContext.getOut();
try {
out.print(toHTML());
} catch (Exception e) {
e.printStackTrace();
}
return super.doStartTag();
}
private String toHTML() throws Exception {
StringBuffer sb=new StringBuffer();
sb.append("");
return sb.toString();
}
return headerTextKey;
}
public List getItems() {
return items;
}
public void setItems(List items) {
this.items = items;
}
public String getTextVal() {
return textVal;
}
public void setTextVal(String textVal) {
this.textVal = textVal;
}
public String getTextKey() {
return textKey;
}
public void setTextKey(String textKey) {
this.textKey = textKey;
}
public String getHeaderTextVal() {
return headerTextVal;
}
public void setHeaderTextVal(String headerTextVal) {
this.headerTextVal = headerTextVal;
}
public String getHeaderTextKey() {
return headerTextKey;
}
public void setHeaderTextKey(String headerTextKey) {
this.headerTextKey = headerTextKey;
}
public String getSelectedVal() {
return selectedVal;
}
public void setSelectedVal(String selectedVal) {
this.selectedVal = selectedVal;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}



