当前位置:网站首页>JSP custom tag library -- select tag
JSP custom tag library -- select tag
2022-07-24 16:52:00 【A Nara Senyu】
Catalog
1. Write helper classes ( Need to splice ):
3. Entity class and collection class ( You can refer to my last chapter foreach label ):
1. Write helper classes ( Need to splice ):
package com.zking.mymvc.tag; import java.io.IOException; import java.lang.reflect.InvocationTargetException; import java.util.List; import javax.servlet.jsp.JspWriter; import javax.servlet.jsp.tagext.BodyTagSupport; import org.apache.commons.beanutils.BeanUtils; import com.mysql.jdbc.StringUtils; public class SelectTag extends BodyTagSupport { private String id; private String name; private String cssClass; private String cssStyle; private List<?> items; private String value; private String text; private String selectedVal; public void setId(String id) { this.id = id; } public void setItems(List<?> items) { this.items = items; } public void setName(String name) { this.name = name; } public void setCssClass(String cssClass) { this.cssClass = cssClass; } public void setCssStyle(String cssStyle) { this.cssStyle = cssStyle; } public void setValue(String value) { this.value = value; } public void setText(String text) { this.text = text; } public void setSelectedVal(String selectedVal) { this.selectedVal = selectedVal; } @Override public int doStartTag() { JspWriter out = this.pageContext.getOut(); try { out.print(toHTML()); } catch (Exception e) { e.printStackTrace(); } return SKIP_BODY; } private String toHTML() throws Exception { StringBuilder sb = new StringBuilder(); sb.append("<select id='"+this.id+"' name='"+this.name+"'"); if(!StringUtils.isNullOrEmpty(this.cssClass)) { sb.append(" class='"+this.cssClass+"'"); } if(!StringUtils.isNullOrEmpty(this.cssStyle)) { sb.append(" style='"+this.cssStyle+"'"); } sb.append(">"); for(Object obj: this.items) { Object val = BeanUtils.getProperty(obj, this.value); Object text = BeanUtils.getProperty(obj, this.text); if(this.selectedVal != null && this.selectedVal.equals(val)) { sb.append("<option value='"+val+"' selected>" + text + "</option>"); } else { sb.append("<option value='"+val+"'>" + text + "</option>"); } } sb.append("</select>"); return sb.toString(); } }
2. To write tld file :
<!DOCTYPE taglib PUBLIC "-//Sun Microsystems, Inc.//DTD JSP Tag Library 1.2//EN" "http://java.sun.com/dtd/web-jsptaglibrary_1_2.dtd"> <!-- Tag library descriptor --> <taglib xmlns="http://java.sun.com/JSP/TagLibraryDescriptor"> <tlib-version>1.0</tlib-version> <jsp-version>1.2</jsp-version> <short-name>Simple Tags</short-name> <uri>/zking</uri> <tag> <name>out</name> <tag-class>com.zking.mymvc.tag.OutTag</tag-class> <body-content>empty</body-content> <attribute> <name>val</name> <required>true</required> <rtexprvalue>true</rtexprvalue> <description>out label val attribute , For output val Value </description> </attribute> <attribute> <name>defaultVal</name> <required>false</required> <rtexprvalue>false</rtexprvalue> <description> Used to define default values </description> </attribute> </tag> <tag> <name>if</name> <tag-class>com.zking.mymvc.tag.IfTag</tag-class> <body-content>jsp</body-content> <attribute> <name>test</name> <required>true</required> <rtexprvalue>true</rtexprvalue> <description>if label </description> </attribute> </tag> <tag> <name>foreach</name> <tag-class>com.zking.mymvc.tag.ForeachTag</tag-class> <body-content>jsp</body-content> <attribute> <name>items</name> <required>true</required> <rtexprvalue>true</rtexprvalue> </attribute> <attribute> <name>var</name> <required>true</required> <rtexprvalue>false</rtexprvalue> </attribute> </tag> <tag> <name>select</name> <tag-class>com.zking.mymvc.tag.SelectTag</tag-class> <body-content>empty</body-content> <attribute> <name>id</name> <required>true</required> <rtexprvalue>false</rtexprvalue> </attribute> <attribute> <name>name</name> <required>true</required> <rtexprvalue>false</rtexprvalue> </attribute> <attribute> <name>cssClass</name> <required>false</required> <rtexprvalue>false</rtexprvalue> </attribute> <attribute> <name>cssStyle</name> <required>false</required> <rtexprvalue>false</rtexprvalue> </attribute> <attribute> <name>items</name> <required>true</required> <rtexprvalue>true</rtexprvalue> </attribute> <attribute> <name>value</name> <required>true</required> <rtexprvalue>false</rtexprvalue> </attribute> <attribute> <name>text</name> <required>true</required> <rtexprvalue>false</rtexprvalue> </attribute> <attribute> <name>selectedVal</name> <required>false</required> <rtexprvalue>false</rtexprvalue> </attribute> </tag> </taglib>
3. Entity class and collection class ( You can refer to my last chapter foreach label ):
4. Run on the page :
<%@page import="com.zking.mymvc.model.*,java.util.List" %> <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@taglib prefix="z" uri="/zking" %> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Insert title here</title> </head> <body> <% request.setAttribute("name", ""); List<Book> books = TestData.getBooks(); request.setAttribute("books", books); %> <!-- out label --> <z:out val="${name}" defaultVal="--"/> <z:if test="${100 == 100}"> test if(100 == 100) </z:if> <z:if test="${100 != 200}"> test if(100 != 200) </z:if> <hr> <z:foreach items="${books}" var="book"> ${book.id} -- ${book.name} <br> </z:foreach> <select id="test" name="test" class="" style="width:100%;"> <option value=id> Unpaid </option> <option value=2 selected> Paid </option> <option value=3> Shipped </option> </select> <z:select id="test02" name="test02" items="${books}" value="id" text="name" selectedVal="3"/> </body> </html>
边栏推荐
- Jenkins cli command details
- IP的概念,IP的分类,IP复用技术
- QT design simulation robot controller
- 荣耀CEO赵明:单一厂商很难实现全场景产品覆盖
- Analysis of double pointer sliding window method and solution of leetcode related problems
- CPU comparison
- Unity camera free movement control
- The third edition of New Horizon College English reading and Writing Tutorial 4 graduation examination site (units 1,2,3,5,6)
- 代码随想录笔记_链表_707设计链表
- 709. Convert to lowercase letters
猜你喜欢

剑指 Offer 48. 最长不含重复字符的子字符串

Sword finger offer 22. the penultimate node in the linked list

Win10 download address

自定义类型:枚举

【南京农业大学】考研初试复试资料分享

CPU comparison

Axi protocol (3): handshake mechanism and implementation details of Axi architecture

EMQ Yingyun technology was listed on the 2022 "cutting edge 100" list of Chinese entrepreneurs

QT generation connection Library

ArcGIS layer annotation display
随机推荐
Codeforces round 690 (Div. 3) B. last year's substring conventional solution
Explain Apache Hudi schema evolution in detail
JS, call in the for loop is asynchronously converted to synchronous execution
IP的概念,IP的分类,IP复用技术
[zero basis] fully understand webgl (VIII)
Hping3 installation and use
小端格式和大端格式(Little-Endian&Big-Endian)
Notebook computer purchase guide (specific brand and model are not recommended)
File browser? QT can also be achieved!
Implementation of side list menu (side menu) of wechat applet
Codeforces round 690 (Div. 3) C. unique number conventional solution
Qsqldatabase: solution of qmmysql driver not loaded
EF data migration
Buffer overflow vulnerability lab experiment record
Concept of IP, classification of IP, IP multiplexing technology
Pull and load more on wechat applet list rendering
JVM class loading subsystem
【技术】uniapp 之 在线选座 demo
804. Unique Morse code word
ZCMU--5083: ly的数对(C语言)