1.自定义标签开发步骤
1.1 助手类
1.2 tld
1.3 taglib
2.UI标签
z:out
public class OutTag extends BodyTagSupport{
private static final long serialVersionUID = -1796097667303750163L;
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) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return SKIP_BODY;
}
<z:dataDirc datasource="数据源"></z:dataDirc>
fore
<option></option>
<h1 value="xxx"></h1>
注1:JspWriter writer = pageContext.getOut();
}
tld中:
<tag>
<!--标签库中的标签名 -->
<name>out</name>
<!--标签对应的助手类的全路径名 -->
<tag-class>com.ylt.jsp.OutTag</tag-class>
<!-- JSP -->
<body-content>JSP</body-content>
<attribute>
<!-- 属性 -->
<name>value</name>
<!-- 属性值是否必填 -->
<required>true</required>
<!-- 是否支持表达式 -->
<rtexprvalue>true</rtexprvalue>
</attribute>
</tag>
m:select
select标签多遇到的问题
1.下拉框数据加载的问题
2. 数据字典由来(什么是数据字典):
数据字典是一种通用的程序设计方法。可以认为,不论什么程序,都是为了处理一定的主体,这里的主体可能是人员、商品(超子)、网页、接口、数据库表、甚至需求分析等等。当主体有很多的属性,每种属性有很多的取值,而且属性的数量和属性取值的数量是不断变化的,特别是当这些数量的变化很快时,就应该考虑引入数据字典的设计方法。
必须导入2个包 commons-beanutils-1.8.0 与 commons-logging
public class SelectTag extends BodyTagSupport{
private String id;
private String name;
private List<Object> items = new ArrayList<>();
private String textKey;
private String textVal;
private String selectVal;
private String headerTextKey;
private String headerTextVal;
@Override
public int doStartTag() throws JspException {
JspWriter out = pageContext.getOut();
try {
out.print(toHTML1());
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return super.doStartTag();
}
private String toHTML1() throws Exception {
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;
for (Object obj : items) {
Field textKeyfield = obj.getClass().getDeclaredField(textKey);
textKeyfield.setAccessible(true);
value = (String) textKeyfield.get(obj);
html = (String) PropertyUtils.getProperty(obj, textVal);
if(value.equals(selectVal)) {
sb.append("<option selected value='"+value+"'>"+html+"</option>");
}else {
sb.append("<option 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 getSelectVal() {
return selectVal;
}
public void setSelectVal(String selectVal) {
this.selectVal = selectVal;
}
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.jsp.day2.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>selectVal</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>
jsp页面展示出结果:
3. 控制标签
m:if:
public class IfTag extends BodyTagSupport{
private static final long serialVersionUID = 3685656095974659666L;
private boolean test;
public boolean isTest() {
return test;
}
public void setTest(boolean test) {
this.test = test;
}
//3目运算符
@Override
public int doStartTag() throws JspException {
// TODO Auto-generated method stub
return test ? EVAL_BODY_INCLUDE : SKIP_BODY;
}
}
tild中:
<tag>
<!--标签库中的标签名 -->
<name>if</name>
<!--标签对应的助手类的全路径名 -->
<tag-class>com.ylt.jsp.IfTag</tag-class>
<!-- JSP -->
<body-content>JSP</body-content>
<attribute>
<!-- 属性 -->
<name>test</name>
<!-- 属性值是否必填 -->
<required>true</required>
<!-- 是否支持表达式 -->
<rtexprvalue>true</rtexprvalue>
</attribute>
</tag>
m:forEach
ps:page(pageContext)|request(…)|session(…)|application(…)
存储和交换数据
public class ForeachTag extends BodyTagSupport{
private static final long serialVersionUID = -6036340720476536662L;
private String var;
private List<Object> items=new ArrayList<>();
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;
}
/**
* 执行完这个方法的时候,var所代表的指针一定要向下移动一位;
*/
@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;
}
}
tid中:
<tag>
<!--标签库中的标签名 -->
<name>foreach</name>
<!--标签对应的助手类的全路径名 -->
<tag-class>com.ylt.jsp.ForeachTag</tag-class>
<!-- JSP -->
<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>
4. 数据标签
m:set
数据标签就是用来存储数据的
列:
c:set
<h1></h1>
ehcache
public class SetTag extends BodyTagSupport{
private static final long serialVersionUID = -2585963718262517673L;
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;
}
}
tid中:
<tag>
<!--标签库中的标签名 -->
<name>set</name>
<!--标签对应的助手类的全路径名 -->
<tag-class>com.ylt.jsp.SetTag</tag-class>
<!-- JSP -->
<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>
展示到jsp页面:
checkbox标签
编辑助手类:
public class CheckboxTag extends BodyTagSupport{
private String xs;//显示值
private String cr;//传入值
private List<Object> item=new ArrayList<>();//数据集合
private List<Object> checkxs=new ArrayList<>();//回显数据集合
public CheckboxTag() {}
public String getXs() {
return xs;
}
public void setXs(String xs) {
this.xs = xs;
}
public String getCr() {
return cr;
}
public void setCr(String cr) {
this.cr = cr;
}
public List<Object> getItem() {
return item;
}
public void setItem(List<Object> item) {
this.item = item;
}
public List<Object> getCheckxs() {
return checkxs;
}
public void setCheckxs(List<Object> checkxs) {
this.checkxs = checkxs;
}
public CheckboxTag(String xs, String cr, List<Object> item, List<Object> checkxs) {
this.xs = xs;
this.cr = cr;
this.item = item;
this.checkxs = checkxs;
}
}
@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();
}
return super.doStartTag();
}
private String toHTML() throws NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException {
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)) {
sb.append("<input checked type='checkbox' value='"+value+"' />"+html+"");
}
else {
sb.append("<input type='checkbox' value='"+value+"' />"+html+"");
}
}
return sb.toString();
}
}
checkbox是一个集合是需所选多个标签
配置tld:
**<tag>
<name>checkbox</name>
<tag-class>com.ylt.taglib.CheckboxTag</tag-class>
<body-content>JSP</body-content>
<attribute>
<name>xs</name>
<required>true</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
<attribute>
<name>cr</name>
<required>true</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
<attribute>
<name>checkxs</name>
<required>false</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
<attribute>
<name>item</name>
<required>true</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
</tag>
在jsp页面进行查看