JSP自定义标签使用详解
程序员文章站
2022-06-08 14:02:15
...
JSP自定义标签使用详解
简单写一个可以从数据库中读取配置的单选按钮组件
1. Java部分
package com.pibgstar.demo.tags;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.SimpleTagSupport;
import java.io.IOException;
import java.util.ArrayList;
/**
* @author pibigstar
* @create 2018-12-02 17:16
* @desc 自定义radio组件
**/
public class RadioTagSupport extends SimpleTagSupport{
private String code;
private String tip;
private String key;
private String value;
private ArrayList<RadioVo> list;
@Override
public void doTag() throws JspException, IOException {
getData();
StringBuilder sp = new StringBuilder();
sp.append("<div class='radio'>");
sp.append("<span class='tip'>");
sp.append(this.getTip());
sp.append("</span>");
for (RadioVo r: list) {
sp.append("<input type='radio' ");
sp.append("name='");
sp.append(r.getName());
sp.append("' value='");
sp.append(r.getValue());
if (r.getChecked()) {
sp.append("' checked />");
}else {
sp.append("' />");
}
sp.append(r.getShowValue());
}
sp.append("</div>");
JspWriter out = getJspContext().getOut();
out.write(sp.toString());
// 从body中获取值
//StringWriter sw = new StringWriter();
//getJspBody().invoke(sw);
//getJspContext().getOut().write(sw.toString());
}
private void getData() {
list = new ArrayList();
// 这里可以通过code值,拿到数据库中自己配置的值,这里只是简单的封装几个固定的值
RadioVo r1 = new RadioVo();
r1.setName("sex");
r1.setValue("1");
r1.setShowValue("男");
RadioVo r2= new RadioVo();
r2.setName("sex");
r2.setValue("2");
r2.setShowValue("女");
}
public String getCode() {
return code;
}
public void setCode(String code) {
this.code = code;
}
public String getTip() {
return tip;
}
public void setTip(String tip) {
this.tip = tip;
}
public String getKey() {
return key;
}
public void setKey(String key) {
this.key = key;
}
public String getValue() {
return value;
}
public void setValue(String value) {
this.value = value;
}
public ArrayList<RadioVo> getList() {
return list;
}
public void setList(ArrayList<RadioVo> list) {
this.list = list;
}
}
class RadioVo {
private String name;
private String value;
private String showValue;
private Boolean checked;
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 getShowValue() {
return showValue;
}
public void setShowValue(String showValue) {
this.showValue = showValue;
}
public Boolean getChecked() {
return checked;
}
public void setChecked(Boolean checked) {
this.checked = checked;
}
}
2. tld编写
<?xml version="1.0" encoding="UTF-8" ?>
<taglib xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-jsptaglibrary_2_1.xsd"
version="2.1">
<description>JSP 自定义标签</description>
<display-name>pibigstar core</display-name>
<tlib-version>1.0</tlib-version>
<short-name>pi</short-name>
<tag>
<description>myself radio</description>
<name>radio</name>
<tag-class>com.pibgstar.demo.tags.RadioTagSupport</tag-class>
<body-content>JSP</body-content>
<attribute>
<name>code</name>
<required>true</required>
<rtexprvalue>false</rtexprvalue>
</attribute>
<attribute>
<name>tip</name>
<required>false</required>
<rtexprvalue>false</rtexprvalue>
</attribute>
</tag>
</taglib>
3. JSP中使用
<%@taglib prefix="pi" uri="WEB-INFO/tlds/pibigstar-pi.tld" %>
<html>
<head>
<title>自定义radio标签使用</title>
</head>
<body>
<pi:radio code="SEX" tip="性别"></pi:radio>
</body>
</html>
下一篇: iOS史诗级漏洞:苹果除非更换硬件