当前位置:网站首页>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~~~~
边栏推荐
- pgbench基准测试《postgresql》
- ACM SIGIR 2022 | 美团技术团队精选论文解读
- Win11快捷复制粘贴不能用怎么办?Win11快捷复制粘贴不能用
- Basic concept of MySQL database and deployment of MySQL version 8.0 (I)
- VR全景创业如何开拓市场?如何让创业之路更加顺畅?
- [mongodb] basic use of mongodb database, special cases, and the installation and creation process of mongoose (including the installation of mongoose fixed version)
- Apk signature.Apk version information
- 华为无线设备配置利用WDS技术部署WLAN业务
- Arduino框架下STM32F103C系列单片机引脚映射关系
- Mycms we media mall V3.6 release, compatible with micro engine application development (laravel framework)
猜你喜欢

Media query adaptation

RouYi-Cloud平台 ---项目的启动、登录功能是怎么实现的、怎么样创建新模块

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

The industry's first cloud native security detection dual model! Safety dog heavyweight report appears at the digital China Construction Summit
![[C language] implementation of three piece chess games](/img/53/7ee14e604c06fd77d65af29d6d92b8.png)
[C language] implementation of three piece chess games

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

Rouyi cloud platform - how to realize the launch and login functions of the project and how to create new modules

Several common methods of SQL optimization

如何开一家盈利的健身房?我用1年回本的经验告诉你,别谈恋爱

WebView optimization
随机推荐
Typescript防止基类被实例化
参加竞赛同学们的留言 : 第十七届的记忆
华为无线设备配置利用WDS技术部署WLAN业务
sql优化常用的几种方法
Assembly analysis swift polymorphism principle
[mongodb] basic use of mongodb database, special cases, and the installation and creation process of mongoose (including the installation of mongoose fixed version)
Applet, JS, transfer object jump transfer parameter problem
Rouyi cloud platform - how to realize the launch and login functions of the project and how to create new modules
Kotlin JVM annotation
Media query adaptation
安全狗入选《云安全全景图2.0》多个细项
(important) first knowledge of C language -- function
c语言进阶篇:指针(三)
【物理应用】水下浮动风力涡轮机的尾流诱导动态模拟风场附matlab代码
这款全网热评的无线路由器,到底有什么特别?
pgbench基准测试《postgresql》
Anr questions often asked in Android interviews
Sqlilabs-3 (entry notes)
A new paradigm of distributed deep learning programming: Global tensor
Routeros limited DNS hijacking and check