自定义标签处理配置文件
<?xml version="1.0" encoding="ISO-8859-1" ?>
<!DOCTYPE taglib
PUBLIC "-//Sun Microsystems, Inc.//DTD JSP Tag Library 1.2//EN"
"http://java.sun.com/dtd/web-jsptaglibrary_1_2.dtd">
<taglib>
<tlib-version>1.0</tlib-version>
<jsp-version>1.2</jsp-version>
<short-name>self</short-name>
<uri>http://bonc.com/group-tag</uri>
<display-name>SELF core</display-name>
<description>Self 1.0 core library</description>
<tag>
<name>groupNavigate</name>
<tag-class>com.bonc.tag.GroupTag</tag-class>
<body-content>empty</body-content>
<description> </description>
<attribute>
<name>groupselect</name>
<required>false</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
</tag>
</taglib>
自定义标签处理类:
package com.bonc.tag;
import java.io.IOException;
import java.util.List;
import javax.servlet.ServletRequest;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.TagSupport;
import com.bonc.dao.TempDimGroupinfo;
import com.bonc.dao.TempDimGroupinfoDAO;
public class GroupTag extends TagSupport {
String groupselect="";
public String getGroupselect() {
return groupselect;
}
public void setGroupselect(String groupselect) {
this.groupselect = groupselect;
}
public int doEndTag() throws JspException {
return super.EVAL_BODY_INCLUDE;
}
public int doStartTag() throws JspException {
ServletRequest request = pageContext.getRequest();
//String groupsel = (String) request.getAttribute("groupsel");
String groupid = (String) request.getAttribute("groupid");
String html="";
TempDimGroupinfoDAO dao = new TempDimGroupinfoDAO();
TempDimGroupinfo groupinfo = new TempDimGroupinfo();
List list = dao.findAll();
html="<select name=\"groupidsel\">";
for(int i=0;i<list.size();i++)
{
groupinfo = (TempDimGroupinfo) list.get(i);
if(groupid==null){
html+="<option value=\""+groupinfo.getGroupId()+"\">"+groupinfo.getGroupName()+"</option>";
}else
{
if(groupid.equals(groupinfo.getGroupId())){
html+="<option value=\""+groupinfo.getGroupId()+"\" selected>"+groupinfo.getGroupName()+"</option>";
}
else{
html+="<option value=\""+groupinfo.getGroupId()+"\">"+groupinfo.getGroupName()+"</option>";
}
}
}
html+="</select>";
html+="<input type=\"hidden\" name=\"groupid\" value=\""+groupid+"\">";
try {
pageContext.getOut().write(html);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return super.EVAL_PAGE;
}
}
<?xml version="1.0" encoding="ISO-8859-1" ?>
<!DOCTYPE taglib
PUBLIC "-//Sun Microsystems, Inc.//DTD JSP Tag Library 1.2//EN"
"http://java.sun.com/dtd/web-jsptaglibrary_1_2.dtd">
<taglib>
<tlib-version>1.0</tlib-version>
<jsp-version>1.2</jsp-version>
<short-name>self</short-name>
<uri>http://bonc.com/group-tag</uri>
<display-name>SELF core</display-name>
<description>Self 1.0 core library</description>
<tag>
<name>groupNavigate</name>
<tag-class>com.bonc.tag.GroupTag</tag-class>
<body-content>empty</body-content>
<description> </description>
<attribute>
<name>groupselect</name>
<required>false</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
</tag>
</taglib>
自定义标签处理类:
package com.bonc.tag;
import java.io.IOException;
import java.util.List;
import javax.servlet.ServletRequest;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.TagSupport;
import com.bonc.dao.TempDimGroupinfo;
import com.bonc.dao.TempDimGroupinfoDAO;
public class GroupTag extends TagSupport {
String groupselect="";
public String getGroupselect() {
return groupselect;
}
public void setGroupselect(String groupselect) {
this.groupselect = groupselect;
}
public int doEndTag() throws JspException {
return super.EVAL_BODY_INCLUDE;
}
public int doStartTag() throws JspException {
ServletRequest request = pageContext.getRequest();
//String groupsel = (String) request.getAttribute("groupsel");
String groupid = (String) request.getAttribute("groupid");
String html="";
TempDimGroupinfoDAO dao = new TempDimGroupinfoDAO();
TempDimGroupinfo groupinfo = new TempDimGroupinfo();
List list = dao.findAll();
html="<select name=\"groupidsel\">";
for(int i=0;i<list.size();i++)
{
groupinfo = (TempDimGroupinfo) list.get(i);
if(groupid==null){
html+="<option value=\""+groupinfo.getGroupId()+"\">"+groupinfo.getGroupName()+"</option>";
}else
{
if(groupid.equals(groupinfo.getGroupId())){
html+="<option value=\""+groupinfo.getGroupId()+"\" selected>"+groupinfo.getGroupName()+"</option>";
}
else{
html+="<option value=\""+groupinfo.getGroupId()+"\">"+groupinfo.getGroupName()+"</option>";
}
}
}
html+="</select>";
html+="<input type=\"hidden\" name=\"groupid\" value=\""+groupid+"\">";
try {
pageContext.getOut().write(html);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return super.EVAL_PAGE;
}
}