栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

j2ee:jsp标签02

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

j2ee:jsp标签02

今天讲自定义jsp标签:foreach标签跟select标签

1、foreach标签

首先我们先写个foreach的助手类

package com.zsx.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 items;
    public String getVar() {
        return var;
    }
    public void setVar(String var) {
        this.var = var;
    }
    public List getItems() {
        return items;
    }
    public void setItems(List items) {
        this.items = items;
    }
    
    @Override
    public int doStartTag() throws JspException {
        Iterator it = items.iterator();
        //
        //var =c,it.next()是集合中的某一个对象
        pageContext.setAttribute(var, it.next());
        pageContext.setAttribute("it", it);
        return EVAL_BODY_INCLUDE;
    }
    @SuppressWarnings("unchecked")
    @Override
    public int doAfterBody() throws JspException {
        Iterator it=(Iterator)pageContext.getAttribute("it");
        if(it.hasNext()) {
            pageContext.setAttribute(var, it.next());
            pageContext.setAttribute("it", it);//为了保留迭代器指针现有位置
            return EVAL_BODY_AGAIN;
        }else {
            return EVAL_PAGE;
        }
    }
    
} 

接下来取tld文件中做配置




    
  JSTL 1.1 core library
  JSTL core
  1.1
  c
  http://jsp.zsx.com

  
    
        Provides core validation features for JSTL tags.
    
    
        org.apache.taglibs.standard.tlv.JstlCoreTLV
    
  
  
  
 

    for
    
    com.zsx.tag.ForeachTag
    
    JSP
    
     
        items
         
        true
         
        true
    
    
    
        var
          
        true
        
        false
    
  

配置完成后来jsp页面调用

<%@ taglib uri="http://jsp.zsx.com" prefix="z" %>


 

在使用标签前,先写一个实体类

package com.zsx.entity;


public class Teacher {
    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 Teacher() {
        // TODO Auto-generated constructor stub
    }
    public Teacher(String tid, String name) {
        super();
        this.tid = tid;
        this.name = name;
    }    
}


 

然后再去页面调用标签进行遍历

<%@page import="java.util.ArrayList"%>
<%@page import="com.zsx.entity.Teacher"%>
<%@page import="java.util.List"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib uri="http://jsp.zsx.com" prefix="z" %>




Insert title here



<%
    List ls=new ArrayList<>();
    ls.add(new Teacher("t001","zs"));
    ls.add(new Teacher("t002","ls"));
    ls.add(new Teacher("t003","we"));
    request.setAttribute("ls", ls);        
%>


    ${t.tid }:${t.name }


之后再运行,效果图如下:

 


2、select标签

老样子,先写个助手类:```

package com.zsx.tag;

import java.io.IOException;
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;//文本
    private String textKey;//键
    private String headerTextVal;
    private String selectValue;
    private String headerTextKey;
    private String id;
    private String name;
    
    public String getSelectValue() {
        return selectValue;
    }

    public void setSelectValue(String selectValue) {
        this.selectValue = selectValue;
    }

    @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();
    }
 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 getId() {
        return id;
    }
    public void setId(String id) {
        this.id = id;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public SelectTag() {
        // TODO Auto-generated constructor stub
    }


    public SelectTag(List items,String selectValue, String textVal, String textKey, String headerTextVal, String headerTextKey,
            String id, String name) {
        super();
        this.items = items;
        this.textVal = textVal;
        this.textKey = textKey;
        this.selectValue = selectValue;
        this.headerTextVal = headerTextVal;
        this.headerTextKey = headerTextKey;
        this.id = id;
        this.name = name;
    }
    
} 


    
    
  

```

然后去tld文件做配置,在上一个基础上添加如下代码:

```


 
 select
    com.zsx.tag.SelectTag
    JSP
    
        items
        true
        true
    
    
        textVal
        true
        false
    
    
        textKey
        true
        false
    
    
        headerTextVal
        false
        false
    
    
        headerTextKey
        false
        false
    
    
        selectValue
        false
        true
    
     
        id
        false
        false
    
    
        name
        false
        false
        
  


```

然后去jsp页面使用标签:```



```

界面展示(前者为jsp自带的select标签,后者为自定义select标签):

 

 

 

在自定义标签里添加如下代码:

```

selectValue="t002" id="mySelect"


```

就可以进行数据回显以及设置模板(此处放源代码):

```
<%@page import="java.util.ArrayList"%>
<%@page import="com.zsx.entity.Teacher"%>
<%@page import="java.util.List"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib uri="http://jsp.zsx.com" prefix="z" %>




Insert title here



<%-- true
false

 --%>

<%
    List ls=new ArrayList<>();
    ls.add(new Teacher("t001","zs"));
    ls.add(new Teacher("t002","ls"));
    ls.add(new Teacher("t003","we"));
    request.setAttribute("ls", ls);        
%>
<%-- 
    ${t.tid }:${t.name }
 --%>
 






效果展示:

 

转载请注明:文章转载自 www.mshxw.com
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号