首页 技术 正文
技术 2022年11月17日
0 收藏 879 点赞 3,145 浏览 3860 个字

1.

实现 forEach 标签: 两个属性: items(集合类型, Collection), var(String 类型)

doTag:
遍历 items 对应的集合
 把正在遍历的对象放入到 pageContext 中, 键: var, 值: 正在遍历的对象.
把标签体的内容直接输出到页面上.

(1)首先建立一个customer类:

package com.lanqiao.javatest;public class Customer {
private Integer id;
private String name;
private int age;
public Customer() {
super();
// TODO Auto-generated constructor stub
}
public Customer(Integer id, String name, int age) {
super();
this.id = id;
this.name = name;
this.age = age;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
@Override
public String toString() {
return "Customer [id=" + id + ", name=" + name + ", age=" + age + "]";
}}

(2)ForEachTag类,继承于父类: SimpleTagSupport

package com.atguigu.javaweb;import java.io.IOException;
import java.util.Collection;import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.SimpleTagSupport;public class ForEachTag extends SimpleTagSupport{ private Collection<?> items; public void setItems(Collection<?> items) {
this.items = items;
} private String var; public void setVar(String var) {
this.var = var;
} @Override
public void doTag() throws JspException, IOException {
// * 遍历 items 对应的集合
if(items != null){
for(Object obj: items){
// * 把正在遍历的对象放入到 pageContext 中, 键: var, 值: 正在遍历的对象.
getJspContext().setAttribute(var, obj); //把标签体的内容直接输出到页面上.
getJspBody().invoke(null); }
} }}

(3)在lib下建立一个mytag.tld的xml文件,

<?xml version="1.0" encoding="UTF-8"?><taglib xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-jsptaglibrary_2_0.xsd"
version="2.0"> <description>MyTag 1.2 core library</description>
<display-name>MyTag core</display-name>
<tlib-version>1.2</tlib-version>
<short-name>lxn</short-name>
<uri>http://lxn.com/myTag/core</uri> <tag>
<name>forEach</name>
<tag-class>com.lanqiao.javatest.ForEachTag</tag-class>
<body-content>scriptless</body-content> <attribute>
<name>items</name>
<required>true</required>
<rtexprvalue>true</rtexprvalue>
</attribute> <attribute>
<name>var</name>
<required>true</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
</tag></taglib>

(4)建立一个test.jsp页面:

<%@page import="java.util.HashMap"%>
<%@page import="java.util.Map"%>
<%@page import="com.atguigu.javaweb.Customer"%>
<%@page import="java.util.ArrayList"%>
<%@page import="java.util.List"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib prefix="lxn" uri="http://lxn.com/myTag/core" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body> <br><br>
<%
List<Customer> customers = new ArrayList<Customer>(); customers.add(new Customer(1, "AAA"));
customers.add(new Customer(2, "BBB"));
customers.add(new Customer(3, "CCC"));
customers.add(new Customer(4, "DDD"));
customers.add(new Customer(5, "EEE")); request.setAttribute("customers", customers);
%> <lxn:forEach items="${requestScope.custoemrs }" var="cust">
-- ${cust.id } -- ${cust.name } <br>
</lxn:forEach> <br><br>
</body>
</html>

2.使用标签处理器类,其中有一些方法:

package com.atguigu.javaweb;import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.io.StringWriter;import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.JspFragment;
import javax.servlet.jsp.tagext.SimpleTagSupport;
//标签处理器类
public class TestJspFragment extends SimpleTagSupport { @Override
public void doTag() throws JspException, IOException {    //JspFragment,是标签体,getJspBody()是获取标签体
JspFragment bodyContent = getJspBody();
//JspFragment.invoke(Witer): Writer 即为标签体内容输出的字符流, 若为 null, 则
//输出到 getJspContext().getOut(), 即输出到页面上. //1. 利用 StringWriter 得到标签体的内容.
StringWriter sw = new StringWriter();
bodyContent.invoke(sw); //2. 把标签体的内容都变为大写
String content = sw.toString().toUpperCase(); //3. 获取 JSP 页面的 out 隐含对象, 输出到页面上
getJspContext().getOut().print(content);
}}
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,111
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,584
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,431
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,203
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,838
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,922