当前位置:网站首页>JSP tag 02
JSP tag 02
2022-07-24 06:27:00 【zsm030616】
One 、z:foreach label
attribute :
①var
var:String
②items
items There are collections in it, so the data type is list
Analyze the circuit :
Article 1 with a :eval_body_include
Second :eval_body_again
< Code demonstration >
package com.zsm.tag;
/**
* @author zjjt
*/
import java.util.Iterator;
import java.util.List;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.BodyTagSupport;
public class ForeachTag extends BodyTagSupport {
private String var;
private List<Object> items;
public String getVar() {
return var;
}
public void setVar(String var) {
this.var = var;
}
public List<Object> getItems() {
return items;
}
public void setItems(List<Object> items) {
this.items = items;
}
public ForeachTag() {
// TODO Auto-generated constructor stub
}
public ForeachTag(String var, List<Object> items) {
super();
this.var = var;
this.items = items;
}
@Override
public int doStartTag() throws JspException {
Iterator<Object> it = items.iterator();
//var = c,it.next() Is an object in the collection
//pageContext.setAttribute("c", items.get(0));
pageContext.setAttribute(var,it.next());
pageContext.setAttribute("it",it);// For the existing position of the pointer during iteration
return EVAL_BODY_INCLUDE;
}
@Override
public int doAfterBody() throws JspException {
Iterator<Object> it = (Iterator<Object>)pageContext.getAttribute("it");
// Determine whether there is another one in the iterator , If there is, it will enter the cycle , If not, stop the cycle
if(it.hasNext()) {
pageContext.setAttribute(var, it.next());
pageContext.setAttribute("it", it);// To preserve the existing position of the pointer during iteration
return EVAL_BODY_AGAIN;
}
else {
return EVAL_PAGE;
}
}
}
Create an entity class
package com.zsm.entity;
import java.io.Serializable;
/**
* Entity class : Teachers
* @author zjjt
*/
public class Teacher implements Serializable{
private static final long serialVersionUID = 1L;
private String tid;
private String name;
public String getTid() {
return tid;
}
public void setTid(String tid) {
this.tid = tid;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Teacher() {
// TODO Auto-generated constructor stub
}
public Teacher(String tid, String name) {
super();
this.tid = tid;
this.name = name;
}
}
tld Customized in the file for label
<tag>
<name>for</name>
<tag-class>com.zsm.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>
jsp Interface
<%@page import="java.util.ArrayList"%>
<%@page import="com.zsm.entity.Teacher"%>
<%@page import="java.util.List"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib uri="http://jsp.veryedu.cn" prefix="z" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<%
List<Teacher> ls = new ArrayList<Teacher>();
ls.add(new Teacher("t001","zs"));
ls.add(new Teacher("t002","ls"));
ls.add(new Teacher("t003","ww"));
request.setAttribute("ls", ls);
%>
<z:for items="${ls}" var="t">
${t.tid }:${t.name}
</z:for>
</body>
</html>
Put the defined attributes into jsp In the set in the interface ( Add attribute values in the process of adding to the set ) Then save the collection
design sketch 
Two 、z:select label
attribute
(1)id
(2)name
(3)items
(4)textKey
(5)textVal
(6)headerTextKey
(7)headerTextVal
(8)selectedVal
Code thinking :
1. Background to traverse —> data source –>items
2. You need an object attribute to represent the corresponding display content of the drop-down box –>textval
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 —>headerTextVal
5. The default header option is selected –>headTextVal
6. Values stored in data , To facilitate data echo –>selectedVal
*
- The following three conditions can be omitted
- 7.id
- 8.name
- 9.cssStyle
PropertyUtils.getProperty(obj, textVal) For tool class , One code makes three codes , namely (Field textKeyFild = obj.getClass().getDeclaredField(textKey);
textKeyFild.setAccessible(true);
Object value = textKeyFild.get(obj); These three codes
The above codes are from commons-beanutils-1.8.0jar package
tld file
<tag>
<name>select</name>
<tag-class>com.zsm.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>headTextVal</name>
<required>false</required>
<rtexprvalue>false</rtexprvalue>
</attribute>
<attribute>
<name>headTextKey</name>
<required>false</required>
<rtexprvalue>false</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>
jsp Interface
<%@page import="java.util.ArrayList"%>
<%@page import="com.dengxiyan.entity.Teacher"%>
<%@page import="java.util.List"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib uri="http://jsp.veryedu.cn" prefix="z" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
<style type="text/css">
#mySel{
color:blue;
}
</style>
</head>
<body>
<%
List<Teacher> ls = new ArrayList<Teacher>();
ls.add(new Teacher("t001","zs"));
ls.add(new Teacher("t002","ls"));
ls.add(new Teacher("t003","ww"));
request.setAttribute("ls", ls);
%>
<z:for items="${ls}" var="t">
${t.tid }:${t.name}
</z:for>
<select>
<option value="1">zs</option>
<option value="2">ls</option>
</select>
<z:select selectedVal="t002" id="mySel" headTextKey="-1" headTextVal="=== Please select ==" textVal="name" items="${ls}" textKey="tid"></z:select>
</body>
</html>
SelectTag Entity class
package com.zsm.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;
/**
* The goal is :
* 1. Omit the process of traversal
* <z:select></selcet>
* 2. When the data is echoed , Disorder increases if Judge , No need to add new code
*
* @author zjjt
*/
public class SelectTag extends BodyTagSupport{
private List<Object> items;
private String textVal;// Equivalent to name
private String textKey;
private String headTextVal;
private String headTextKey;
private String selectedVal;
private String id;
private String name;
@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();
sb.append("<select id='"+id+"' name='"+name+"'>");
// Judge when headTextVal Not equal to null and not an empty string
if(headTextVal!=null && !"".equals(headTextVal)) {
sb.append("<option value='"+headTextKey+"'>"+headTextVal+"</option>");
}
for (Object obj : items) {
Field textKeyFild = obj.getClass().getDeclaredField(textKey);
textKeyFild.setAccessible(true);
Object value = textKeyFild.get(obj);// The value really displayed
if(selectedVal !=null && !"".equals(selectedVal) && selectedVal.equals(value)) {
// When the value matches, add selected You can achieve the rollback effect
sb.append("<option selected value='"+value+"'>"+PropertyUtils.getProperty(obj, textVal)+"</option>");
}else {
sb.append("<option value='"+value+"'>"+PropertyUtils.getProperty(obj, 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;
}
}
design sketch 
边栏推荐
- 不租服务器,自建个人商业网站(4)
- 进行挂载永久挂载后无法开机
- Simple but easy to use: using keras 2 to realize multi-dimensional time series prediction based on LSTM
- IP notes (9)
- 利用内网穿透,实现公网访问内网
- 【251】常见的测试工具
- Interview questions for Test Manager / test team leader / Test Supervisor
- Simple three-step fast intranet penetration
- Summary of ten common vulnerabilities (principle, harm, defense)
- IP class notes (5)
猜你喜欢
![[218] what are the advantages and disadvantages of CS architecture and BS architecture and data on the server and client?](/img/d8/a367c26b51d9dbaf53bf4fe2a13917.png)
[218] what are the advantages and disadvantages of CS architecture and BS architecture and data on the server and client?

Hololens2 development: use MRTK and simulate eye tracking

Maximum value of jz47 gifts (dynamic planning ideas)

不租服务器,自建个人商业网站(4)

Using keras to realize LSTM time series prediction based on attention mechanism

Do not rent a server, build your own personal business website (how to buy a domain name)

服务器硬件及RAID配置实战

IP job (6)

本地搭建WordPress个人博客,并内网穿透发布上线 (22)

不租服务器,自建个人商业网站(2)
随机推荐
IP笔记(12)
【217】#!/usr/bin/env 的意义
Unity 3D frame rate statistics script
Flink function (2): checkpointedfunction
IP notes (8)
IP作业(6)
Hololens 2 development 101: create the first hololens 2 Application
Ia note 1
Tensorflow GPU installation -- 056
Getting started with Lunix commands - user and file permissions (Chmod details)
IP notes (9)
Leetcode does not add, subtract, multiply, divide, and calculate the number of 1 in binary
Quickly and simply set up FTP server, and achieve public network access through intranet [no need for public IP]
力扣986.区间列表的交集
将内网映射到公网【无需公网IP】
Do not rent servers, build your own personal business website (1)
公网使用Microsoft Remote Desktop远程桌面,随时远程办公
jz47 礼物的最大价值(动态规划思路)
Unity (III) three dimensional mathematics and coordinate system
Leetcode sword finger offer JZ9 dual stack implementation queue