当前位置:网站首页>jsp标签02
jsp标签02
2022-07-24 05:18:00 【zsm030616】
一、z:foreach标签
属性:
①var
var:String
②items
items里面放的是集合所以数据类型为list
分析线路:
第一条:eval_body_include
第二条:eval_body_again
<代码演示>
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()是集合中的某一个对象
//pageContext.setAttribute("c", items.get(0));
pageContext.setAttribute(var,it.next());
pageContext.setAttribute("it",it);//为了迭代时指针现有的位置
return EVAL_BODY_INCLUDE;
}
@Override
public int doAfterBody() throws JspException {
Iterator<Object> it = (Iterator<Object>)pageContext.getAttribute("it");
//判断迭代器里是否还有下一个,有就进入循环,没有则停止循环
if(it.hasNext()) {
pageContext.setAttribute(var, it.next());
pageContext.setAttribute("it", it);//为了保留迭代时指针现有的位置
return EVAL_BODY_AGAIN;
}
else {
return EVAL_PAGE;
}
}
}
建立一个实体类
package com.zsm.entity;
import java.io.Serializable;
/**
* 实体类:教师类
* @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文件里自定义for标签
<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界面
<%@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>
将定义的属性放到jsp界面里的集合中(加入集合的过程中增加属性值)然后将集合存起来
效果图
二、z:select标签
属性
(1)id
(2)name
(3)items
(4)textKey
(5)textVal
(6)headerTextKey
(7)headerTextVal
(8)selectedVal
代码思路:
1.后台要遍历—>数据源–>items
2.需要一个对象的属性代表下拉框对应的展示内容–>textval
3.需要一个对象的属性代表下拉框对应的value值—>textKey
4.默认的头部选项展示内容—>headerTextVal
5.默认的头部选项选值–>headTextVal
6.数据中存储的值,为了方便做数据回显–>selectedVal
*
- 以下三个条件可以省略不写
- 7.id
- 8.name
- 9.cssStyle
PropertyUtils.getProperty(obj, textVal)为工具类,一条代码定三条代码,即(Field textKeyFild = obj.getClass().getDeclaredField(textKey);
textKeyFild.setAccessible(true);
Object value = textKeyFild.get(obj);这三条代码
以上代码都来自于commons-beanutils-1.8.0jar包
tld文件
<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界面
<%@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="===请选择==" textVal="name" items="${ls}" textKey="tid"></z:select>
</body>
</html>
SelectTag实体类
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;
/**
* 目标:
* 1.省略遍历的过程
* <z:select></selcet>
* 2.当数据回显时,无序增加if判断,无需增加新代码
*
* @author zjjt
*/
public class SelectTag extends BodyTagSupport{
private List<Object> items;
private String textVal;//相当于名称
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+"'>");
//判断当headTextVal不等于空并且不为空字符串的情况下
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);//真正展示的值
if(selectedVal !=null && !"".equals(selectedVal) && selectedVal.equals(value)) {
//当值匹配上后加上selected就可以实现回滚效果
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;
}
}
效果图
边栏推荐
- 程序员工具合集!(转载)
- special effects - 鼠标点击,自定义 DOM 跟随移动
- 利用二分法从数组中寻找具体数值
- 输入10个人的名字,按从大到小排序输出
- 还原ui设计稿
- Some experience of using D2L package and related environment configuration
- A collation of the basic usage of numpy
- JS - 计算直角三角形的边长及角度
- Learn AI linear regression from Li Mu. The code implementation from scratch is super detailed
- VS 调试
猜你喜欢
随机推荐
Redis的使用
赶紧进来!!轻松掌握C语言“顺序”、“分支”、“循环”三大结构
面向 对象
JS链表中的快慢指针
Promise
yocs_velocity_smoother源码编译
Implementation and comparison of nine sorting (ten thousand words summary)
Promise_ Async and await
Opengl在屏幕上绘制一个锥体,该锥体有四个面,每个面都是三角形。为该锥体添加光照和纹理效果
递归还能这么玩?递归实现扫雷游戏
Solutions to MySQL remote connection errors
c语言中的变量与常量
C语言从入门到入土——数组
新语法01_Es6新语法
模板数据的二次加工
day(0~6)代表每月第一天起始位置,stop代表每月天数,每天之间空两个空格。输入不同的day和stop,输出每月日历的样子。假设day为2,stop为31,则输出样式为
Tabs标签页(el-tabs)_造成页面卡死问题
special effects - 鼠标点击,出现烟花炸裂效果
umi之define属性
C语言从入门到入土(二)








![JS:为什么 [] == ![] 返回 true ?](/img/36/94839bf4ce6bd06d2cbe989828c791.png)
