hello,家人们,今天我们再来更一节自定义标签的小课堂(●'◡'●)
目录
首先,我们还是得先上处理图:
常见自定义标签主要步骤:
- 编写助手类
- 编写标签库描述文件(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<Cat> getCat(){
-
- List<Cat> list=new ArrayList<Cat>();
-
- 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" %>
- html>
- <html>
- <head>
- <meta charset="UTF-8">
- <title>Insert title heretitle>
- head>
- <body>
-
-
- <%
- List<Cat> list=Test.getCat();
- request.setAttribute("list",list);
- System.out.print(list);
- %>
- <p:foeach list="${list}" var="l">
-
- ${l.age}----${l.name}---${l.info}<br>
-
-
- p:foeach>
-
- body>
- html>
运行结果:
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("+id+'"'+"name="+'"'+name+'"');
- if(!StringUtils.isNullOrEmpty(clazz)) {
- sb.append("class="+'"'+clazz+'"');
- }
- if(!StringUtils.isNullOrEmpty(Style)) {
- sb.append("style="+'"'+Style+'"');
- }
- sb.append(">");
-
- for (Object optain :this.list) {
-
- String v=BeanUtils.getProperty(optain,this.value);
- String text=BeanUtils.getProperty(optain, this.text);
-
- if(v.equals(this.SelectValue)) {
- sb.append("+v+'"'+"selected"+text+"");
- }else {
- sb.append("");
- }
- }
-
- sb.append("");
-
- return sb.toString();
- }
- }
2.在后缀名为.tld的文件中增加select标签
注:tld文件必须保存到WEB-INF目录或其子目录
-
- <tag>
- <name>selectname>
- <tag-class>com.zking.y10.dao.SelectTagtag-class>
- <body-content>jspbody-content>
- <description>select标签description>
-
- <attribute>
- <name>idname>
- <required>truerequired>
- <rtexprvalue>falsertexprvalue>
- attribute>
- <attribute>
- <name>namename>
- <required>truerequired>
- <rtexprvalue>falsertexprvalue>
- attribute>
- <attribute>
- <name>listname>
- <required>truerequired>
- <rtexprvalue>truertexprvalue>
- attribute>
- <attribute>
- <name>clazzname>
- <required>falserequired>
- <rtexprvalue>falsertexprvalue>
- attribute>
- <attribute>
- <name>Stylename>
- <required>falserequired>
- <rtexprvalue>falsertexprvalue>
- attribute>
- <attribute>
- <name>valuename>
- <required>truerequired>
- <rtexprvalue>falsertexprvalue>
- attribute>
- <attribute>
- <name>textname>
- <required>truerequired>
- <rtexprvalue>falsertexprvalue>
- attribute>
- <attribute>
- <name>selectValuename>
- <required>falserequired>
- <rtexprvalue>falsertexprvalue>
- attribute>
-
- tag>
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" %>
- html>
- <html>
- <head>
- <meta charset="UTF-8">
- <title>Insert title heretitle>
- head>
- <body>
-
-
- <%
- List<Cat> list=Test.getCat();
- request.setAttribute("list",list);
-
- %>
- <p:foeach list="${list}" var="l">
-
- ${l.age}----${l.name}---${l.info}<br>
- p:foeach>
-
-
- <select id="test" name="test" class="" style="width:100px;">
- <option value=0>已付款option>
- <option value= selected>已发货option>
- <option value=2>已签收option>
- select>
-
- <p:select name="test" value="age" list="${list}" text="name" id="test" selectValue="3">p:select>
-
-
- body>
- html>
4.运行结果:
今天的简单举例到这里就结束啦~
今天也要记得微笑呀.