目录
一.foreach标签
编写助手类
编写tld
测试数据辅助类
在页面上使用标签
二.select
编写助手类
编写tld
在页面上使用标签
public class ForeachTag extends BodyTagSupport {
//存放数据源
private List> items;
//每次循环获取的对象放入pageContext中,并以var属性的值为key进行保存
//示例: 页面上标签var属性指定为item,则每次循环取出的对象(obj)将执行如下:
//pageContext.setAttribute("item", obj);
//页面中可以使用EL表达式取出对象中的属性, 如: ${item.name}
private String var;
public List> getItems() {
return items;
}
public void setItems(List> items) {
this.items = items;
}
public String getVar() {
return var;
}
public void setVar(String var) {
this.var = var;
}
@Override
public int doStartTag() {
if(Objects.isNull(this.items) || this.items.size() == 0) {
return SKIP_BODY;
}
Iterator> iterator = this.items.iterator();
Object obj = iterator.next();
this.pageContext.setAttribute(var, obj);
this.pageContext.setAttribute("iterator", iterator);
return EVAL_BODY_INCLUDE;
}
@Override
public int doAfterBody() {
Iterator> it = (Iterator>)this.pageContext.getAttribute("iterator");
if(it.hasNext()) {
this.pageContext.setAttribute(var, it.next());
return EVAL_BODY_AGAIN;
}
return SKIP_BODY;
}
}
1.0
1.2
Simple Tags
/zking
foreach
com.zking.tagdemo.tag.ForeachTag
jsp
items
true
true
out标签test属性,用于输出test的值
var
true
true
out标签test属性,用于输出test的值
public class Book {
private int id;
private String name;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
public class TestData {
public static List getBooks() {
List books = new ArrayList<>();
Book b1 = new Book();
b1.setId(1);
b1.setName("水浒传");
Book b2 = new Book();
b2.setId(2);
b2.setName("红楼梦");
Book b3 = new Book();
b3.setId(3);
b3.setName("西游记");
books.add(b1);
books.add(b2);
books.add(b3);
return books;
}
}
<%@page import="com.zking.mvc.test.*,java.util.List" %>
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<%@taglib prefix="z" uri="/zking" %>
Insert title here
<%=System.currentTimeMillis()%>
<%
request.setAttribute("name", null);
%>
测试if(100 == 100)
测试if(100 == 1)
<%
//获取测试数据
List books = TestData.getBooks();
//放入request对象中
request.setAttribute("books", books);
%>
${book.id } - ${book.name }
package com.zking.tagdemo.tag;
import java.util.Map;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.BodyTagSupport;
public class SelectTag extends BodyTagSupport{
private String name;
private Map options;
private String selectedValue;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Map getOptions() {
return options;
}
public void setOptions(Map options) {
this.options = options;
}
public String getSelectedValue() {
return selectedValue;
}
public void setSelectedValue(String selectedValue) {
this.selectedValue = selectedValue;
}
@Override
public int doStartTag() throws JspException {
JspWriter out = this.pageContext.getOut();
try {
out.print("");
} catch (Exception e) {
e.printStackTrace();
}
return SKIP_BODY;
}
}
1.0
1.2
Simple Tags
/zking
selectTag
com.zking.tagdemo.tag.SelectTag
empty
name
true
true
out标签test属性,用于输出test的值
options
true
true
out标签test属性,用于输出test的值
selectedValue
false
true
out标签test属性,用于输出test的值
<%@page import="java.util.HashMap"%>
<%@page import="java.util.Map"%>
<%@page import="java.util.List"%>
<%@page import="com.zking.tagdemo.tag.Book"%>
<%@page import="com.zking.tagdemo.tag.BookTest"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@taglib prefix="z" uri="/zking" %>
Insert title here
sname}-${book.tid}
<%
request.setAttribute("name", "瑶姐");
Map map = new HashMap<>();
map.put("1", "宝宝");
map.put("2", "辉辉");
map.put("3", "杰少");
request.setAttribute("options", map);
%>