hello,家人们,今天我们再来更一节自定义标签的小课堂(●'◡'●)
目录
foeach标签
select标签
首先,我们还是得先上处理图:
常见自定义标签主要步骤:
- 编写助手类
- 编写标签库描述文件(tld)
- 在页面上引入标签使用
1.先new一个cat类
package com.zking.y10.pojo;
import java.io.Serializable;
public class Cat implements Serializable{
private Integer age;
private String name;
private String info;
public Cat() {
// TODO Auto-generated constructor stub
}
public Cat(Integer age, String name, String info) {
super();
this.age = age;
this.name = name;
this.info = info;
}
@Override
public String toString() {
return "Cat [age=" + age + ", name=" + name + ", info=" + info + "]";
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getInfo() {
return info;
}
public void setInfo(String info) {
this.info = info;
}
}
2.定义一个普通类,然后继承BodyTagSupport
package com.zking.y10.dao;
import java.util.Iterator;
import java.util.List;
import java.util.Objects;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.BodyTagSupport;
public class FoeachTag extends BodyTagSupport{
private List list;
private String var;
public void setList(List list) {
this.list = list;
}
public void setVar(String var) {
this.var = var;
}
@Override
public int doStartTag() throws JspException {
if(Objects.isNull(this.list) || this.list.size()==0) {
return SKIP_BODY;
}
Iterator it=this.list.iterator();
Object next = it.next();
this.pageContext.setAttribute(var, next);
this.pageContext.setAttribute("it",it);
return EVAL_BODY_INCLUDE;
}
@Override
public int doAfterBody() throws JspException {
Iterator a = (Iterator)this.pageContext.getAttribute("it");
if(a.hasNext()) {
this.pageContext.setAttribute(var,a.next());
return EVAL_BODY_AGAIN;
}
return SKIP_BODY;
}
}
3.写一个类,把数据填进去
package com.zking.y10.dao;
import java.util.ArrayList;
import java.util.List;
import com.zking.y10.pojo.Cat;
public class Test {
public static List getCat(){
List list=new ArrayList();
Cat c=new Cat();
c.setAge(12);
c.setName("金渐层");
c.setInfo("超可爱噢");
Cat c1=new Cat();
c1.setAge(1);
c1.setName("银渐层");
c1.setInfo("肥肥胖胖,肉嘟嘟的");
Cat c2=new Cat();
c2.setAge(10);
c2.setName("布偶");
c2.setInfo("长毛猫,毛软软的,抱着很舒服");
list.add(c);
list.add(c1);
list.add(c2);
return list;
}
}
4.最后再来个index.jsp测试一下
<%@page import="com.zking.y10.dao.Test,java.util.List" %>
<%@page import="com.zking.y10.pojo.Cat"%>
<%@page import="java.util.List"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@taglib prefix="p" uri="/zkings" %>
Insert title here
<%
List list=Test.getCat();
request.setAttribute("list",list);
System.out.print(list);
%>
${l.age}----${l.name}---${l.info}
运行结果:
1.照常写一个普通类,然后继承BodyTagSupport
package com.zking.y10.dao;
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.BeanUtils;
import com.mysql.jdbc.StringUtils;
public class SelectTag extends BodyTagSupport{
private Integer id;
private String name;
private String clazz;
private String Style;
private List list;
private String value;
private String text;
private String SelectValue;
public SelectTag() {
// TODO Auto-generated constructor stub
}
public SelectTag(Integer id, String name, String clazz, String style, List list, String value, String text,
String selectValue) {
super();
this.id = id;
this.name = name;
this.clazz = clazz;
Style = style;
this.list = list;
this.value = value;
this.text = text;
SelectValue = selectValue;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getClazz() {
return clazz;
}
public void setClazz(String clazz) {
this.clazz = clazz;
}
public String getStyle() {
return Style;
}
public void setStyle(String style) {
Style = style;
}
public List getList() {
return list;
}
public void setList(List list) {
this.list = list;
}
public String getValue() {
return value;
}
public void setValue(String value) {
this.value = value;
}
public String getText() {
return text;
}
public void setText(String text) {
this.text = text;
}
public String getSelectValue() {
return SelectValue;
}
public void setSelectValue(String selectValue) {
SelectValue = selectValue;
}
@Override
public String toString() {
return "SelectTag [id=" + id + ", name=" + name + ", clazz=" + clazz + ", Style=" + Style + ", list=" + list
+ ", value=" + value + ", text=" + text + ", SelectValue=" + SelectValue + "]";
}
@Override
public int doStartTag() throws JspException {
JspWriter out = this.pageContext.getOut();
try {
String html=getHtml();
out.print(html);
} catch (Exception e) {
e.printStackTrace();
}
return SKIP_BODY;
}
public String getHtml() throws Exception {
StringBuilder sb=new StringBuilder();
sb.append("");
return sb.toString();
}
}
2.在后缀名为.tld的文件中增加select标签
注:tld文件必须保存到WEB-INF目录或其子目录
select
com.zking.y10.dao.SelectTag
jsp
select标签
id
true
false
name
true
false
list
true
true
clazz
false
false
Style
false
false
value
true
false
text
true
false
selectValue
false
false
3.测试
<%@page import="com.zking.y10.dao.Test,java.util.List" %>
<%@page import="com.zking.y10.pojo.Cat"%>
<%@page import="java.util.List"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@taglib prefix="p" uri="/zkings" %>
Insert title here
<%
List list=Test.getCat();
request.setAttribute("list",list);
%>
${l.age}----${l.name}---${l.info}
4.运行结果:
今天的简单举例到这里就结束啦~
今天也要记得微笑呀.