1.什么是自定义jsp标签
目录:
1.什么是自定义jsp标签:
foreach:
Javajspforeach标签是JSP标准标签库(JSTL)中的一个标签,用于在JSP页面中迭代集合或数组。通过使用该标签,您可以轻松地遍历集合中的每个元素,并执行自定义的操作(例如输出元素的值或执行一些计算)。Javajspforeach标签非常实用且易于使用,是JSP开发中常用的工具之一。
select:
Javajspselect标签是Java JSP标准标签库(JSTL)中的一个标签,用于在JSP页面中创建下拉菜单(Select Box)。通过使用该标签,您可以很容易地创建一个下拉菜单,让用户从预定义的选项中选择一个值。
Javajspselect标签需要在标记中指定各个选项,可以使用JSTL的foreach标签遍历一个集合,为每个选项添加一个选项元素。为了方便用户,您还可以设置selected
属性来指定默认选中的选项。
2.foreach标签
建立jsp页面:代码如下
创建助手类:代码如下
package com.xiaoye.tag;
import java.util.ArrayList;
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 List<Object> items=new ArrayList<Object>();
//对象的指针:指向的是对象来源的哪一个对象
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 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;
}
return SKIP_BODY;
}
@Override
public int doStartTag() throws JspException {
if(items==null || items.size()==0)
return SKIP_BODY;
Iterator<Object> it = items.iterator();
//取的数据源中第一条数据
pageContext.setAttribute(var, it.next());
pageContext.setAttribute("it", it);
return EVAL_BODY_INCLUDE;
}
}
编写tld:
<?xml version="1.0" encoding="UTF-8" ?>
<taglib xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-jsptaglibrary_2_1.xsd"
version="2.1">
<description>JSTL 1.1 core library</description>
<display-name>JSTL core</display-name>
<tlib-version>1.1</tlib-version>
<short-name>x</short-name>
<uri>http://jsp.xiaoye.xy</uri>
<tag>
<name>foreach</name>
<tag-class>com.xiaoye.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>
输出结果:
3.Dept数字标签
建立jsp页面,代码如下:
<%@page import="com.xiaoye.entity.Dept"%>
<%@page import="java.util.ArrayList"%>
<%@page import="java.util.List"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@taglib uri="http://jsp.xiaoye.xy" prefix="x" %>
<!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>
<%
List<Dept> l=new ArrayList<Dept>();
l.add(new Dept(1,"晨曦组 "));
l.add(new Dept(2,"乘风破浪组"));
l.add(new Dept(3,"牵手盈翔组"));
request.setAttribute("L", l);
%>
</head>
<body>
<p>自定义dept标签</p>
作用:当某一些数据多个页面,或者多个地方频繁使用时,就可以做成标签,减少代码量
<x:dept var="xy"></x:dept>
商品模块使用部门数据:${xy }
</body>
</html>
创建助手类,代码如下:
package com.xiaoye.tag;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.BodyTagSupport;
import com.xiaoye.entity.Dept;
public class DeptTag extends BodyTagSupport{
//存放数据的名称
private String var;
public String getVar() {
return var;
}
public void setVar(String var) {
this.var = var;
}
@Override
public int doStartTag() throws JspException {
//模拟数据库访问过程
List<Dept> l=new ArrayList<Dept>();
l.add(new Dept(1,"晨曦组"));
l.add(new Dept(2,"乘风破浪组"));
l.add(new Dept(3,"牵手盈翔组"));
//将数据放入var中
pageContext.setAttribute(var, l);
return SKIP_BODY;
}
}
编写tld:
输出结果:
4.select标签
v 建立jsp页面,代码如下:
<%@page import="com.xiaoye.entity.Dept"%>
<%@page import="java.util.ArrayList"%>
<%@page import="java.util.List"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@taglib uri="http://jsp.xiaoye.xy" prefix="x" %>
<!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>
<%
List<Dept> l=new ArrayList<Dept>();
l.add(new Dept(1,"晨曦组 "));
l.add(new Dept(2,"乘风破浪组"));
l.add(new Dept(3,"牵手盈翔组"));
request.setAttribute("L", l);
%>
</head>
<body>
<p>自定义select标签</p>
<br>
<x:select optionval="id" optiontext="name" items="${L }" ></x:select>
<x:select optionval="id" optiontext="name" items="${L }" selected="l"></x:select>
<x:select optionval="id" optiontext="name" items="${L }" selected="l"></x:select>
</body>
</html>
创建助手类,代码如下:
package com.xiaoye.tag;
import java.io.IOException;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.BodyTagSupport;
public class SelectTag extends BodyTagSupport{
//下拉框的数据源
private List<Object> items=new ArrayList<Object>();
//下拉框option的value值
private String optionval;
//下拉框option的标签体内容
private String optiontext;
//初次进入选中option标签
private String selected;
@Override
public int doStartTag() throws JspException {
JspWriter out = pageContext.getOut();
try {
out.println(toHTML());
} catch (IOException e) {
e.printStackTrace();
}
return SKIP_BODY ;
}
/**
* 在页面要显示的内容
* @return str
*/
private StringBuffer toHTML() {
StringBuffer str = new StringBuffer("<select>");
//循环传进的集合数据
for (Object object : items) {
try {
//获取该对象的属性值
String val= getObjAttrVal(object,optionval);
String text= getObjAttrVal(object,optiontext);
//在下拉框里循环增加option标签
//selected判断初次进入页面的选中指定的option标签
str.append("<option value='"+val+"' "+(val==selected?"selected":"")+">"+text+"</option>");
} catch (Exception e) {
e.printStackTrace();
}
}
str.append("</select>");
return str;
}
/**
* @param object 对象
* @param val 属性
* @return 根据属性(val)获取对象(object)相对应属性值
* @throws SecurityException
* @throws NoSuchFieldException
*/
private String getObjAttrVal(Object object, String val) throws Exception {
Class cl = object.getClass();
//获取该对象的val属性
Field field = cl.getDeclaredField(val);
//打开权限
field.setAccessible(true);
return field.get(object).toString();
}
public List<Object> getItems() {
return items;
}
public void setItems(List<Object> items) {
this.items = items;
}
public String getOptionval() {
return optionval;
}
public void setOptionval(String optionval) {
this.optionval = optionval;
}
public String getOptiontext() {
return optiontext;
}
public void setOptiontext(String optiontext) {
this.optiontext = optiontext;
}
public String getSelected() {
return selected;
}
public void setSelected(String selected) {
this.selected = selected;
}
}
编写tld:
<?xml version="1.0" encoding="UTF-8" ?>
<taglib xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-jsptaglibrary_2_1.xsd"
version="2.1">
<description>JSTL 1.1 core library</description>
<display-name>JSTL core</display-name>
<tlib-version>1.1</tlib-version>
<short-name>x</short-name>
<uri>http://jsp.xiaoye.xy</uri>
<tag>
<name>select</name>
<tag-class>com.xiaoye.tag.SelectTag</tag-class>
<body-content>JSP</body-content>
<attribute>
<name>items</name>
<required>true</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
<attribute>
<name>optionval</name>
<required>true</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
<attribute>
<name>optiontext</name>
<required>true</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
<attribute>
<name>selected</name>
<required>false</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
</tag>
输出结果: