当前位置:网站首页>JSP tag case
JSP tag case
2022-07-28 23:30:00 【I love coriander TVT】
Catalog
3、 ... and 、 expand : Customize checkbox label
One 、 Customize foreach label
foreach Tags can be used to verify what I shared yesterday Jsp The second and third routes in the tag article
Let's review these three routes again :
JSP There are three routes in the tag life cycle :
1、 Route one :doStartTag---> The return value is (SkipBody)--->doEndTag
2、 Route two : Route two :doStartTag---> The return value is (EAVL_BODY_INCLUDE)--- >doAfterBody---> The return value is (EAVL_PAGE)--->doEndTag
3、 Route three :doStartTag---> The return value is (EAVL_BODY_INCLUDE)--->doAfterbody---> Return value (EAVL_Body_Again)--->doAfterBody( loop )--->doEndTag
1、 Create helper class
package com.zjy.tag;
import java.util.Iterator;
import java.util.List;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.BodyTagSupport;
/**
* foreach Label helper class
* @author Zhu Jiayin
*
*/
public class ForeachTag extends BodyTagSupport{
private List<Object> items;
private String var;
public List<Object> getItems() {
return items;
}
public void setItems(List<Object> items) {
this.items = items;
}
public String getVar() {
return var;
}
public void setVar(String var) {
this.var = var;
}
@Override
public int doStartTag() throws JspException {
Iterator<Object> it = items.iterator();
pageContext.setAttribute(var, it.next());
pageContext.setAttribute("it", it);// Get the current position of the iterator
return EVAL_BODY_INCLUDE;
}
@Override
public int doAfterBody() throws JspException {
Iterator<Object> it = items.iterator();
if(it.hasNext()) {
pageContext.setAttribute(var, it.next());
pageContext.setAttribute("it", it);
return EVAL_BODY_AGAIN;
}
else {
return EVAL_PAGE;
}
}
}
Route 2 and route 3 have different return values in the label body ,foreach The reason why the tag can verify Route 2 and route 3 at the same time is that when we use iterators to traverse , Make a judgment on the data ,it.hasNext Is there a next value , If so, we will continue to get its value and store it , To perform , The return value is return EVAL_BODY_AGAIN; If not , Then execute the subsequent code, that is, return EVAL_PAGE;

How iterators work : stay foreach Ergodic time , There is a pointer down one by one to find things , When used for the first time , Save the iterator first , Every time the pointer traverses , To save the position of the pointer
2. establish tld class
<name>foreach</name>
<tag-class>com.zjy.tag.ForeachTag</tag-class>
<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>3、jsp Interface reference
<%
List<Theaher> ls = new ArrayList<>();
ls.add(new Theaher(1," Big bear "));
ls.add(new Theaher(2," Two bears "));
request.setAttribute("ls", ls);
%>
<z:foreach items="${ls}" var="a">
${a.tid}:${a.tname}
</z:foreach>Running effect :

Two 、 Customize select label
effect : It can reduce the amount of code added, deleted, modified and checked
1、 Helper
The helper class has a total of six properties ,items、textval、textkey、headertextval、headertextkey、selectedval Corresponding to the data source respectively 、option Content displayed in 、option Medium value、 The default selection 、 Selected by default value And data echo
package com.zjy.tag;
import java.io.IOException;
import java.lang.reflect.Field;
import java.util.List;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.BodyTagSupport;
import org.apache.commons.beanutils.PropertyUtils;
/**
* 1. Omit the process of traversal
* <z:select></select>
*
* 2. As data echo , No need to add if Judge , No need to add new code
* @author Administrator
*
*
* analysis :1. Background to traverse --》 data source items
*2. You need an object attribute to represent the corresponding display content of the drop-down box --》textValue
*3. You need an object attribute to represent the corresponding... Of the drop-down box value value --》textKey
*4. The default header option shows the content --》headtextval
*5. Default header option value --》headtextKey
*6. Values stored in the database , To facilitate data echo --》selectedVal
*7.id
*8.name
*9.Cssstyle
*/
public class SelectTag extends BodyTagSupport{
private List<Object> items;
private String textVal;//teacher Medium name
private String textKey;
private String headtextval;
private String headtextKey;
private String selectedVal;
// Beautification interface
private String id;
private String name;
private String cssStyle;
@Override
public int doStartTag() throws JspException {
JspWriter out = pageContext.getOut();
try {
out.print(toHTML());
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return super.doStartTag();
}
private String toHTML() throws Exception, Exception {
StringBuffer sb = new StringBuffer();
sb.append("<select id='"+id+"' name='"+name+"'>");
if(headtextval!=null && !"".equals(headtextval)) {
sb.append("<option value='"+headtextKey+"'>"+headtextKey+"</option>");
}
for (Object object : items) {
Field textKeyfield = object.getClass().getDeclaredField(textKey);
textKeyfield.setAccessible(true);
Object value = textKeyfield.get(object);// Really show the value of the drop-down box
if(selectedVal!=null&&!"".equals(selectedVal)&&selectedVal.equals(value)) {
sb.append("<option selected value='"+value+"'>"+PropertyUtils.getProperty(object, textVal)+"</option>");
}
sb.append("<option value='"+value+"'>"+PropertyUtils.getProperty(object, textVal)+"</option>");
}
sb.append("</select>");
return sb.toString();
}
public List<Object> getItems() {
return items;
}
public void setItems(List<Object> items) {
this.items = items;
}
public String getTextVal() {
return textVal;
}
public void setTextVal(String textVal) {
this.textVal = textVal;
}
public String getTextKey() {
return textKey;
}
public void setTextKey(String textKey) {
this.textKey = textKey;
}
public String getHeadtextval() {
return headtextval;
}
public void setHeadtextval(String headtextval) {
this.headtextval = headtextval;
}
public String getHeadtextKey() {
return headtextKey;
}
public void setHeadtextKey(String headtextKey) {
this.headtextKey = headtextKey;
}
public String getSelectedVal() {
return selectedVal;
}
public void setSelectedVal(String selectedVal) {
this.selectedVal = selectedVal;
}
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 SelectTag() {
// TODO Auto-generated constructor stub
}
public SelectTag(List<Object> items, String textVal, String textKey, String headtextval, String headtextKey,
String selectedVal, String id, String name) {
super();
this.items = items;
this.textVal = textVal;
this.textKey = textKey;
this.headtextval = headtextval;
this.headtextKey = headtextKey;
this.selectedVal = selectedVal;
this.id = id;
this.name = name;
}
@Override
public String toString() {
return "SelectTag [items=" + items + ", textVal=" + textVal + ", textKey=" + textKey + ", headtextval="
+ headtextval + ", headtextKey=" + headtextKey + ", selectedVal=" + selectedVal + ", id=" + id
+ ", name=" + name + "]";
}
}
2、tld class
<name>select</name>
<tag-class>com.zjy.tag.SelectTag</tag-class>
<body-content>JSP</body-content>
<attribute>
<name>items</name>
<required>true</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
<attribute>
<name>textVal</name>
<required>true</required>
<rtexprvalue>false</rtexprvalue>
</attribute>
<attribute>
<name>textKey</name>
<required>true</required>
<rtexprvalue>false</rtexprvalue>
</attribute>
<attribute>
<name>headerTextVal</name>
<required>false</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
<attribute>
<name>headerTextKey</name>
<required>false</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
<attribute>
<name>selectedVal</name>
<required>false</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
<attribute>
<name>id</name>
<required>false</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
<attribute>
<name>selectedVal</name>
<required>false</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
<attribute>
<name>id</name>
<required>false</required>
<rtexprvalue>false</rtexprvalue>
</attribute>
<attribute>
<name>name</name>
<required>false</required>
<rtexprvalue>false</rtexprvalue>
</attribute>
</tag>3、jsp Interface reference
<z:select textVal="name" items="${ls}" textKey="value">You can customize the selector to beautify the label

3、 ... and 、 expand : Customize checkbox label
In fact, it's the same as the one above select The idea of label implementation is similar , Only when echoing, you have to use the collection to receive , Because we may get more than one value . It can also reduce the amount of code
1. Helper
There are six properties of the helper class items、checknr、checkvalue、checked、id、name Corresponding to the data source respectively 、 The contents of the check box and the value Value and a data echo 、id and input Inside the label name value
package com.zjy.tag;
import java.io.IOException;
import java.io.PrintWriter;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.util.List;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.BodyTagSupport;
import org.apache.commons.beanutils.PropertyUtils;
/**
* Checkbox helper class
* <input type="checkbox" name="hid" value="1" > dance
* <input type="checkbox" name="hid" value="2" > Sing a song
* <input type="checkbox" name="hid" value="3" > read
* @author Zhu Jiayin
*
*/
public class CheckedTag extends BodyTagSupport{
private List<Object> items;
private String checknr;
private String checkvalue;
private List<Object> checked;
// Beautify the selector of the label
private String id;
private String name;
private Object obj;
public List<Object> getItems() {
return items;
}
public void setItems(List<Object> items) {
this.items = items;
}
public String getChecknr() {
return checknr;
}
public void setChecknr(String checknr) {
this.checknr = checknr;
}
public String getCheckvalue() {
return checkvalue;
}
public void setCheckvalue(String checkvalue) {
this.checkvalue = checkvalue;
}
public List<Object> getChecked() {
return checked;
}
public void setChecked( List<Object> checked) {
this.checked = checked;
}
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 CheckedTag() {
// TODO Auto-generated constructor stub
}
public CheckedTag(List<Object> items, String checknr, String checkvalue, List<Object> checked, String id,
String name, Object obj) {
this.items = items;
this.checknr = checknr;
this.checkvalue = checkvalue;
this.checked = checked;
this.id = id;
this.name = name;
this.obj = obj;
}
@Override
public int doStartTag() throws JspException {
JspWriter out = pageContext.getOut();
try {
out.print(toHTML());
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return super.doStartTag();
}
private String toHTML() throws Exception{
StringBuffer sb = new StringBuffer();
for (Object obj : items) {
Field Field = obj.getClass().getDeclaredField(checkvalue);
Field.setAccessible(true);
Object value = Field.get(obj);
if(checked.contains(value)) {
sb.append("<input checked type='checkbox' id='"+id+"' name='"+name+"' value='"+value+"'>"+PropertyUtils.getProperty(obj, checknr));
}
else {
sb.append("<input type='checkbox' id='"+id+"' name='"+name+"' value='"+value+"'>"+PropertyUtils.getProperty(obj, checknr));
}
}
return sb.toString();
}
}
2、tld class
<tag>
<!-- Name of tag library -->
<name>checkbox</name>
<tag-class>com.zjy.tag.CheckedTag</tag-class>
<body-content>JSP</body-content>
<attribute>
<name>items</name>
<required>true</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
<attribute>
<name>checknr</name>
<required>true</required>
<rtexprvalue>false</rtexprvalue>
</attribute>
<attribute>
<name>checkvalue</name>
<required>true</required>
<rtexprvalue>false</rtexprvalue>
</attribute>
<attribute>
<name>checked</name>
<required>true</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
<attribute>
<name>id</name>
<required>false</required>
<rtexprvalue>false</rtexprvalue>
</attribute>
<attribute>
<name>name</name>
<required>false</required>
<rtexprvalue>false</rtexprvalue>
</attribute>
</tag>
3、jsp Page references
Because the data is echoed with sets , So in addition to defining a set of teachers , You also need to define a set
<%
List<Theaher> ls = new ArrayList<>();
ls.add(new Theaher(1," Big bear "));
ls.add(new Theaher(2," Two bears "));
request.setAttribute("ls", ls);
List<Object> list = new ArrayList<>();
list.add(1);
request.setAttribute("list", list);
%>
<z:checkbox items="${ls}" checked="${list}" checkvalue="tid" checknr="tname"></z:checkbox>Effect display :

Well, let's share it here first bye~~~~
边栏推荐
- 1314_串口技术_RS232通信基础的信息
- A new MPLS note from quigo, which must be read when taking the IE exam ---- quigo of Shangwen network
- [physical application] Wake induced dynamic simulation of underwater floating wind turbine wind field with matlab code
- Failure [INSTALL_FAILED_TEST_ONLY: installPackageLI]
- 这款全网热评的无线路由器,到底有什么特别?
- 【MongoDB】MongoDB数据库的基础使用,特殊情况以及Mongoose的安装和创建流程(含有Mongoose固定版本安装)
- 以流量为主导的数字零售的发展模式,仅仅只是一个开始
- 【C语言】三子棋小游戏实现
- 行泊一体迎爆发期,抢量产还是修技术护城河?
- 字节8年女测试总监工作感悟—写给想转行或即将进入测试行业的女生们...
猜你喜欢

MySQL数据库的基本概念以及MySQL8.0版本的部署(一)

Messages from students participating in the competition: memories of the 17th session

How to embed AI digital human function in VR panorama? Create a cloud experience

Thesis reading (2) - vggnet of classification

A new MPLS note from quigo, which must be read when taking the IE exam ---- quigo of Shangwen network

《MySQL数据库进阶实战》读后感(SQL 小虚竹)

Date time functions commonly used in MySQL

Advanced C language: pointer (3)

6 open source tutorials of super conscience!

集火全屋智能“后装市场”,真正玩得转的没几个
随机推荐
终端输出g_debug()信息
A new MPLS note from quigo, which must be read when taking the IE exam ---- quigo of Shangwen network
High quality programming
Solve the exception that all control files are damaged
The development mode of digital retail dominated by traffic is only the beginning
18 diagrams, intuitive understanding of neural networks, manifolds and topologies
Applet, JS, transfer object jump transfer parameter problem
Mgr.exe virus caused the startup program to fail
Retrofit Usage Summary
【物理应用】大气吸收损耗附matlab代码
Assembly analysis swift polymorphism principle
Advanced C language: pointer (3)
解决控制文件全部损坏的异常
trivy【3】自定义扫描策略
Meet the outbreak period with the integration of transportation and parking, rush for mass production or build a technical moat?
In order for digital retail to continue to play its role, we need to give new connotation and significance to digital retail
recursion and iteration
智能电视与小程序的结合
Terminal output G_ Debug() information
被忽视的智能电视小程序领域