当前位置:网站首页>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 
边栏推荐
- Sword finger offer jz10 Fibonacci sequence
- Hololens 2 development 101: create the first hololens 2 Application
- IP笔记(11)
- Leetcode sword finger offer jz25 merges two sorted linked lists
- IP notes (6)
- How to build a website full of ritual sense and publish it on the public website 2-2
- Unity2d game let characters move - Part 1
- IA笔记 1
- Use and principle of spark broadcast variable and accumulator
- [214] what is an automation framework
猜你喜欢
![Quickly and simply set up FTP server, and achieve public network access through intranet [no need for public IP]](/img/2a/43ba2839b842e0901a550d2883b883.png)
Quickly and simply set up FTP server, and achieve public network access through intranet [no need for public IP]

Hololens 2 Chinese development document MRTK V2

IP笔记(9)

UE4 reload system 1. basic principle of reload system

Hololens 2 development: development environment deployment

Leetcode剑指offer JZ9 双栈实现队列

Jenkins自动化无人值守运行(上/下)
![Configure a fixed remote desktop address [intranet penetration, no need for public IP]](/img/17/4e119be86189d80b100eb000254a86.png)
Configure a fixed remote desktop address [intranet penetration, no need for public IP]

Luckyframeweb testing platform (a full latitude free open source testing platform that supports interface automation, Web UI automation, APP automation, and distributed testing)

公网使用Microsoft Remote Desktop远程桌面,随时远程办公
随机推荐
Simple three-step fast intranet penetration
第二周作业
UE4 aiming offset
IA笔记 1
leetcode剑指offer JZ73 翻转单词序列
Do not rent servers, build your own personal business website (2)
MySQL from basic to entry to high availability
IP笔记(9)
Quickly and simply set up FTP server, and achieve public network access through intranet [no need for public IP]
Process and planned task management
【217】#!/usr/bin/env 的意义
将内网映射到公网【无需公网IP】
IP笔记(7)
IP课总结(3)
Jenkins自动化无人值守运行(上/下)
Hololens 2 development: development environment deployment
测试经理/测试组长/测试主管面试题
进程和计划任务管理
IP notes (6)
[226] instructions for Wireshark parameters