案例如下:
目的:为了在jsp页面里面根据我传入的值来显示相应的文本信息
第一步:新建一个erp.tld
<?xml version="1.0" encoding="UTF-8" ?>
<taglib xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-jsptaglibrary_2_0.xsd"
version="2.0">
<description>erp library</description>
<display-name>erp core</display-name>
<tlib-version>1.1</tlib-version>
<short-name>e</short-name>
<uri>http://txjava/jsp/erp/core</uri>
<tag>
<name>orderTypetext</name>
<tag-class>cn.tx.tag.ERPOrderTypeTag</tag-class>
<body-content>JSP</body-content>
<attribute>
<name>orderType</name>
<required>true</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
</tag>
<tag>
<name>orderStatetext</name>
<tag-class>cn.tx.tag.ERPOrderStateTag</tag-class>
<body-content>JSP</body-content>
<attribute>
<name>orderType</name>
<required>true</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
<attribute>
<name>orderState</name>
<required>true</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
</tag>
</taglib>
1.解析:
接下来把标签类写出来:
第一个标签类:
解析:从该类中可以看出只有一个属性
package cn.tx.tag;
import java.io.IOException;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.TagSupport;
import cn.tx.utils.ERPConstants;
public class ERPOrderTypeTag extends TagSupport {
//设置订单类型值
private String orderType;
public String getOrderType() {
return orderType;
}
public void setOrderType(String orderType) {
this.orderType = orderType;
}
@Override
public int doStartTag() throws JspException {
//获得JSPWriter对象向jsp页面写文本
JspWriter out = pageContext.getOut();
String text = "";
switch(orderType){
case ERPConstants.ORDER_TYPE_BUY:
text = ERPConstants.ORDER_TYPE_BUY_TEXT;
break;
case ERPConstants.ORDER_TYPE_TRANS:
text = ERPConstants.ORDER_TYPE_TRANS_TEXT;
break;
case ERPConstants.ORDER_TYPE_INSTOCK:
text = ERPConstants.ORDER_TYPE_INSTOCK_TEXT;
break;
case ERPConstants.ORDER_TYPE_SALES:
text = ERPConstants.ORDER_TYPE_SALES_TEXT;
break;
}
try {
out.write(text);
} catch (IOException e) {
e.printStackTrace();
}
return super.doStartTag();
}
}
第二个标签类:
解析:从该类中可以看出该标签类中有2个属性
package cn.tx.tag;
import java.io.IOException;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.TagSupport;
import cn.tx.utils.ERPConstants;
public class ERPOrderStateTag extends TagSupport {
//设置订单类型值
private String orderType;
private String orderState;
public String getOrderState() {
return orderState;
}
public void setOrderState(String orderState) {
this.orderState = orderState;
}
public String getOrderType() {
return orderType;
}
public void setOrderType(String orderType) {
this.orderType = orderType;
}
@Override
public int doStartTag() throws JspException {
//获得JSPWriter对象向jsp页面写文本
JspWriter out = pageContext.getOut();
String text = "";
switch(orderType){
case ERPConstants.ORDER_TYPE_BUY:
switch(orderState){
case ERPConstants.ORDER_TYPE_BUY_AUDIT:
text = ERPConstants.ORDER_TYPE_BUY_AUDIT_TEXT;
break;
case ERPConstants.ORDER_TYPE_BUY_AUDIT_REJECT:
text = ERPConstants.ORDER_TYPE_BUY_AUDIT_REJECT_TEXT;
break;
case ERPConstants.ORDER_TYPE_BUY_AUDIT_PASS:
text = ERPConstants.ORDER_TYPE_BUY_AUDIT_PASS_TEXT;
break;
}
break;
case ERPConstants.ORDER_TYPE_TRANS:
switch(orderState){
case ERPConstants.ORDER_TYPE_TRANS_BUY:
text = ERPConstants.ORDER_TYPE_TRANS_BUY_TEXT;
break;
case ERPConstants.ORDER_TYPE_TRANS_ASSGIN:
text = ERPConstants.ORDER_TYPE_TRANS_ASSGIN_TEXT;
break;
case ERPConstants.ORDER_TYPE_TRANS_BUYING:
text = ERPConstants.ORDER_TYPE_TRANS_BUYING_TEXT;
break;
}
break;
case ERPConstants.ORDER_TYPE_INSTOCK:
switch(orderState){
case ERPConstants.ORDER_TYPE_INSTOCK_WAIT:
text = ERPConstants.ORDER_TYPE_INSTOCK_WAIT_TEXT;
break;
case ERPConstants.ORDER_TYPE_INSTOCK_INING:
text = ERPConstants.ORDER_TYPE_INSTOCK_INING_TEXT;
break;
case ERPConstants.ORDER_TYPE_INSTOCK_FINISH:
text = ERPConstants.ORDER_TYPE_INSTOCK_FINISH_TEXT;
break;
}
break;
case ERPConstants.ORDER_TYPE_SALES:
text = ERPConstants.ORDER_TYPE_SALES_TEXT;
break;
}
try {
out.write(text);
} catch (IOException e) {
e.printStackTrace();
}
return super.doStartTag();
}
}
解析:
实际在jsp中使用如下: