JSP自定义标签
1、UI标签:用ui标签也就是不需要写标签体的标签又能展示内容的就是ui标签
2、控制标签:是会控制文本,标签等
3、数据标签:简单的来说数据标签就是用来存储数据的
首先我们创建一个Set标签和Out标签
package com.wangshaoyang;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.BodyTagSupport;
public class SetTag extends BodyTagSupport {
private static final long serialVersionUID = 1566469460010276784L;
private String var;
private Object value;
@Override
public int doStartTag() throws JspException {
pageContext.setAttribute(var, value);
return SKIP_BODY;
}
public String getVar() {
return var;
}
public void setVar(String var) {
this.var = var;
}
public Object getValue() {
return value;
}
public void setValue(Object value) {
this.value = value;
}
public SetTag(String var, Object value) {
super();
this.var = var;
this.value = value;
}
public SetTag() {
super();
}
}
Out标签
package com.wangshaoyang;
import java.io.IOException;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.BodyTagSupport;
public class OutTag extends BodyTagSupport {
private static final long serialVersionUID = 2125806651604491287L;
private Object value;
public Object getValue() {
return value;
}
public void setValue(Object value) {
this.value = value;
}
@Override
public int doStartTag() throws JspException {
JspWriter out = pageContext.getOut();
try {
out.print(value.toString());
} catch (IOException e) {
e.printStackTrace();
}
return SKIP_BODY;
}
}
第二步:配置tld文件
<tag>
<name>set</name>
<tag-class>com.wangshaoyang.SetTag</tag-class>
<body-content>JSP</body-content>
<attribute>
<name>var</name>
<required>true</required>
<rtexprvalue>false</rtexprvalue>
</attribute>
<attribute>
<name>value</name>
<required>true</required>
<rtexprvalue>false</rtexprvalue>
</attribute>
</tag>
<tag>
<name>out</name>
<tag-class>com.wangshaoyang.OutTag</tag-class>
<body-content>JSP</body-content>
<attribute>
<name>value</name>
<required>true</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
</tag>
实现set和out的代码
<a:set var="name" value="zhangsan"></a:set>
<a:out value="${name }"></a:out>
运行可得
Foreach标签
package com.wangshaoyang;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.BodyTagSupport;
/**
* 执行完这个方法的时候,var所代表的指针一定要向下移动一位
* @author wsy
*
*/
public class ForeachTag extends BodyTagSupport {
private static final long serialVersionUID = 8824611322233305024L;
private String var;
private List<Object> items=new ArrayList<>();
@Override
public int doStartTag() throws JspException {
if(items.size()==0) {
return SKIP_BODY;
}else {
Iterator<Object> it=items.iterator();
pageContext.setAttribute(var, it.next());
pageContext.setAttribute("it",it);
return EVAL_BODY_INCLUDE;
}
}
@Override
public int doAfterBody() throws JspException {
Iterator<Object> it=(Iterator<Object>) pageContext.getAttribute("it");
if(it.hasNext()) {
pageContext.setAttribute(var, it.next());
pageContext.setAttribute("it",it);
return EVAL_BODY_AGAIN;
}
return EVAL_PAGE;
}
public String getVar() {
return var;
}
public void setVar(String var) {
this.var = var;
}
public List<Object> getItems() {
return items;
}
public void setItems(List<Object> items) {
this.items = items;
}
}
配置tld文件
<tag>
<name>foreach</name>
<tag-class>com.wangshaoyang.ForeachTag </tag-class>
<body-content>JSP</body-content>
<attribute>
<name>var</name>
<required>true</required>
<rtexprvalue>false</rtexprvalue>
</attribute>
<attribute>
<name>items</name>
<required>true</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
</tag>
在jsp界面遍历出来
<%
List list=new ArrayList();
list.add(new Student("001","laowang"));
list.add(new Student("002","zhima"));
list.add(new Student("003","zhizhang"));
list.add(new Student("004","xiaoyaojing"));
request.setAttribute("stus", list);
%>
<a:foreach items="${stus }" var="stu">
${stu.id },${stu.name }<br>
</a:foreach>
运行代码可得
Select标签
package com.wangshaoyang;
import java.io.IOException;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
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;
/**
* 1.值的传递 id,name (可不写)
* 2.数据源 items (可不写)
* 3.展示列与数据存储列与实体类的对应关系 textKey textVal
* <option value="1">湖南</option>
* 4.数据回显 selectedVal (可不写)
* 5.可能下拉框有默认值(头标签) headerTextKey headerTextVal (可不写)
* @author Administrator
*
*/
public class SelectTag extends BodyTagSupport {
private static final long serialVersionUID = -2399172761217694229L;
private String id;
private String name;
private List<Object> items=new ArrayList<>();
private String textKey;
private String textVal;
private String selectedVal;
private String headerTextKey;
private String headerTextVal;
@Override
public int doStartTag() throws JspException {
JspWriter out = pageContext.getOut();
try {
out.print(toHTML());
} catch (IOException e) {
e.printStackTrace();
} catch (NoSuchFieldException e) {
e.printStackTrace();
} catch (SecurityException e) {
e.printStackTrace();
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
} catch (NoSuchMethodException e) {
e.printStackTrace();
}
return super.doStartTag();
}
//将值都用字符串拼接起来
private String toHTML() throws NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException, InvocationTargetException, NoSuchMethodException {
StringBuffer sb=new StringBuffer();
sb.append("<select id='"+id+"' name='"+name+"'>");
if(!(headerTextKey==null||"".equals(headerTextKey)||headerTextVal==null||"".equals(headerTextVal))) {//如果头标签有值,就选中它
sb.append("<option selected value='"+headerTextKey+"'>"+headerTextVal+"</option>");
}
String value;
String html;
//遍历集合,将值加入option中
for (Object obj: items) {
//第一种方式 反射
// Field textKeyField = obj.getClass().getDeclaredField(textKey);
// textKeyField.setAccessible(true);
// value=(String) textKeyField.get(obj);
//第二种 导jar包
value=(String) PropertyUtils.getProperty(obj, textKey);
html=(String) PropertyUtils.getProperty(obj, textVal);
if(value.equals(selectedVal)) {
sb.append("<option selected value='"+value+"'>"+html+"</option>");
}
sb.append("<option value='"+value+"'>"+html+"</option>");
}
sb.append("</select>");
return sb.toString();
}
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 List<Object> getItems() {
return items;
}
public void setItems(List<Object> items) {
this.items = items;
}
public String getTextKey() {
return textKey;
}
public void setTextKey(String textKey) {
this.textKey = textKey;
}
public String getTextVal() {
return textVal;
}
public void setTextVal(String textVal) {
this.textVal = textVal;
}
public String getSelectedVal() {
return selectedVal;
}
public void setSelectedVal(String selectedVal) {
this.selectedVal = selectedVal;
}
public String getHeaderTextKey() {
return headerTextKey;
}
public void setHeaderTextKey(String headerTextKey) {
this.headerTextKey = headerTextKey;
}
public String getHeaderTextVal() {
return headerTextVal;
}
public void setHeaderTextVal(String headerTextVal) {
this.headerTextVal = headerTextVal;
}
}
配置tld文件
<tag>
<name>select</name>
<tag-class>com.wangshaoyang.SelectTag</tag-class>
<body-content>JSP</body-content>
<attribute>
<name>id</name>
<required>false</required>
<rtexprvalue>false</rtexprvalue>
</attribute>
<attribute>
<name>name</name>
<required>false</required>
<rtexprvalue>false</rtexprvalue>
</attribute>
<attribute>
<name>items</name>
<required>true</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
<attribute>
<name>textKey</name>
<required>true</required>
<rtexprvalue>false</rtexprvalue>
</attribute>
<attribute>
<name>textVal</name>
<required>true</required>
<rtexprvalue>false</rtexprvalue>
</attribute>
<attribute>
<name>selectedVal</name>
<required>false</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
<attribute>
<name>headerTextKey</name>
<required>false</required>
<rtexprvalue>false</rtexprvalue>
</attribute>
<attribute>
<name>headerTextVal</name>
<required>false</required>
<rtexprvalue>false</rtexprvalue>
</attribute>
</tag>
最后我们就来了解下多选框 首先我们建立一个CheckeboxTag
package com.wangshaoyang;
import java.io.IOException;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.BodyTagSupport;
public class CheckeboxTag extends BodyTagSupport {
private static final long serialVersionUID = 512032277217021774L;
private String textKey;//传入值
private String textVal;//显示值
private List<Object> checkedVal=new ArrayList<>();//回显数据集合
private List<Object> item=new ArrayList<>();//数据集合
public List<Object> getItem() {
return item;
}
public void setItem(List<Object> item) {
this.item = item;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getTextKey() {
return textKey;
}
public List<Object> getCheckedVal() {
return checkedVal;
}
public void setCheckedVal(List<Object> checkedVal) {
this.checkedVal = checkedVal;
}
public void setTextKey(String textKey) {
this.textKey = textKey;
}
public String getTextVal() {
return textVal;
}
public void setTextVal(String textVal) {
this.textVal = textVal;
}
public CheckeboxTag() {
super();
}
public CheckeboxTag(String textKey, String textVal, List<Object> checkedVal, List<Object> item) {
super();
this.textKey = textKey;
this.textVal = textVal;
this.checkedVal = checkedVal;
this.item = item;
}
@Override
public int doStartTag() throws JspException {
JspWriter out = pageContext.getOut();
try {
out.print(toHTML());
} catch (IOException e) {
e.printStackTrace();
} catch (NoSuchFieldException e) {
e.printStackTrace();
} catch (SecurityException e) {
e.printStackTrace();
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return super.doStartTag();
}
private String toHTML() throws Exception {
StringBuffer sb=new StringBuffer();
String value;
String html;
for (Object obj : item) {
//一样利用放射取属性值
Field textKeyfield = obj.getClass().getDeclaredField(textKey);
textKeyfield.setAccessible(true);
value=(String)textKeyfield.get(obj);
Field textValfield = obj.getClass().getDeclaredField(textVal);
textValfield.setAccessible(true);
html=(String)textValfield.get(obj);
if(checkedVal.contains(value)) {//判断回显集合里是否包含这个value,包含就设置选择
sb.append("<input checked type='checkbox' value='"+value+"' />"+html+"");
}
else {
sb.append("<input type='checkbox' value='"+value+"' />"+html+"");
}
}
return sb.toString();
}
public static void main(String[] args) {
}
}
配置tld文件
<tag>
<name>checkbox</name>
<tag-class>com.wangshaoyang.CheckeboxTag</tag-class>
<body-content>JSP</body-content>
<attribute>
<name>textKey</name>
<required>true</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
<attribute>
<name>textVal</name>
<required>true</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
<attribute>
<name>item</name>
<required>true</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
<attribute>
<name>checkedVal</name>
<required>false</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
</tag>
然后运用代码对自己写的代码进行测试
<%
List ls=new ArrayList();
ls.add(new Student("1","玩游戏"));
ls.add(new Student("2","撩妹"));
ls.add(new Student("3","蹦迪"));
//设置选中的项
List lsa=new ArrayList();
//ls1.add("1");
lsa.add("2");
lsa.add("3");
request.setAttribute("ls", ls);
request.setAttribute("lsa", lsa);
%>
<z:checkbox textVal="name" checkedVal="${lsa}" item="${ls}" textKey="id"></z:checkbox>
运行可得
今天就更新到这里
喜欢的可以关注我
不定时更新