自定义标签foreach与select标签使用

jsp标签作用

链接: https://blog.csdn.net/licmi/article/details/106360922.

案例

ForEach标签案例

  <tag>
    <name>ForEach</name>
    <tag-class>com.liuchunming.ForEachTag</tag-class>
    <body-content>JSP</body-content>
    <attribute>
        <name>items</name>
        <required>true</required>
        <rtexprvalue>true</rtexprvalue>
    </attribute> 
    <attribute>
        <name>var</name>
        <required>true</required>
        <rtexprvalue>false</rtexprvalue>
    </attribute>  
  </tag>
  

ForEach助手类

package com.liuchunming;

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 List<Object> items;
	private String var;
	
	
	public List<Object> getItems() {
		return items;
	}
	public void setItems(List<Object> items) {
		this.items = items;
	}
	public String getVar() {
		return var;
	}
	public void setVar(String var) {
		this.var = var;
	}
	
	@Override
	public int doStartTag() throws JspException {
		
		if(null!=items&&items.size()!=0) {
			Iterator<Object> it = items.iterator();
			Object next = it.next();
			pageContext.setAttribute(var, next);
			pageContext.setAttribute("it", it);
			System.out.println(items);
			return EVAL_BODY_INCLUDE;
		}
		return SKIP_BODY;
	}
	@Override
	public int doAfterBody() throws JspException {
		Iterator<Object> it = 
				(Iterator<Object>) pageContext.getAttribute("it");
		while(it.hasNext()) {
			Object next = it.next();
			pageContext.setAttribute(var, next);
			return EVAL_BODY_AGAIN;
		}
		return SKIP_BODY;
	}
}

测试代码

<% 
		 List<String> n=new ArrayList<String>();
		n.add("张三");
		n.add("李四");
		n.add("王五");
		request.setAttribute("n", n); 
		%>
		<ul>
		<l:ForEach items="${n }" var="l">
		<li>${l }</li>
		</l:ForEach>
	</ul>

效果图
在这里插入图片描述
select标签

<tag>
    <name>select</name>
    <tag-class>com.liuchunming.SelectTag</tag-class>
    <body-content>JSP</body-content>
    <attribute>
        <name>items</name>
        <required>true</required>
        <rtexprvalue>true</rtexprvalue>
    </attribute>  
    <attribute>
        <name>name</name>
        <required>false</required>
        <rtexprvalue>false</rtexprvalue>
    </attribute> 
    <attribute>
        <name>value</name>
        <required>true</required>
        <rtexprvalue>false</rtexprvalue>
    </attribute> 
    <attribute>
        <name>text</name>
        <required>true</required>
        <rtexprvalue>false</rtexprvalue>
    </attribute> 
     <attribute>
        <name>style</name>
        <required>false</required>
        <rtexprvalue>false</rtexprvalue>
    </attribute> 
     <attribute>
        <name>headkey</name>
        <required>false</required>
        <rtexprvalue>false</rtexprvalue>
    </attribute> 
     <attribute>
        <name>headvalue</name>
        <required>false</required>
        <rtexprvalue>false</rtexprvalue>
    </attribute>
    <attribute>
        <name>selected</name>
        <required>false</required>
        <rtexprvalue>false</rtexprvalue>
    </attribute>  
    
  </tag>

select助手类

package com.liuchunming;

import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
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;

import sun.net.NetworkServer;

public class SelectTag extends BodyTagSupport {
	private List<Object> items;
	private String name;
	
	private String value;//保存value的key
	private String text;//保存option的文本
	
	private String headkey;
	private String headvalue;
	
	private String selected;
	
	
	private String style;


	public List<Object> getItems() {
		return items;
	}


	public String getSelected() {
		return selected;
	}


	public void setSelected(String selected) {
		this.selected = selected;
	}


	public String getHeadkey() {
		return headkey;
	}


	public void setHeadkey(String headkey) {
		this.headkey = headkey;
	}


	public String getHeadvalue() {
		return headvalue;
	}


	public void setHeadvalue(String headvalue) {
		this.headvalue = headvalue;
	}


	public void setItems(List<Object> items) {
		this.items = items;
	}


	public String getName() {
		return name;
	}


	public void setName(String name) {
		this.name = name;
	}


	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 getStyle() {
		return style;
	}


	public void setStyle(String style) {
		this.style = style;
	}
	
	@Override
	public int doStartTag() throws JspException {
		JspWriter out = pageContext.getOut();
		try {
			out.println(toHtml());
		} catch (Exception e) {
			throw new RuntimeException(e);
		}
		return SKIP_BODY;
	}
	private String toHtml() throws Exception{
		StringBuffer sb =new StringBuffer();
		sb.append("<select name='"+name+"'");
		sb.append(" style='"+style+"'");
		sb.append(">");
		if(null!=items&&0!=items.size()) {
			if(null!=headkey&&!"".equals(headkey.trim())) {
				sb.append("<option value='"+headkey+"'>"+headvalue+"</option>");
			}
			
			Object valuea =null;
			Object texta =null;
			for (Object object : items) {
				//反射
				valuea= PropertyUtils.getProperty(object, value);
				texta= PropertyUtils.getProperty(object, text);
				//判断传过来的id是否一样,如果一样就选中
				if(valuea.toString().equals(selected.trim())) {
					sb.append("<option selected value='"+valuea+"'>"+texta+"</option>");
				}else {
					sb.append("<option value='"+valuea+"'>"+texta+"</option>");	
				}
			}
			sb.append("</select>");
		}
		
		return sb.toString();
	}
}

效果图
在这里插入图片描述
在这里插入图片描述
因为我测试界面传的参数为2,所以默认选中id为2的

<l:select items="${l }" value="deptId" text="deptName" 
style="width:150px;" headkey="-1" selected="2" headvalue="---请选择---">
</l:select> 

总结

今天的分享就到这里,如果有什么不对的地方欢迎大家提出来改进。

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值