jsp标签下

简介: jsp标签下
  • foreach标签
  • dept数据标签
  • select标签

foreach标签

package com.zhulinjun;
public class Dept {
private int id;
private String name;
public int getId() {
  return id;
}
public void setId(int id) {
  this.id = id;
}
public String getName() {
  return name;
}
public void setName(String name) {
  this.name = name;
}
@Override
public String toString() {
  return "Dept [id=" + id + ", name=" + name + "]";
}
public Dept(int id, String name) {
  super();
  this.id = id;
  this.name = name;
}
public Dept() {
  super();
  // TODO Auto-generated constructor stub
}
}
<?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>z</short-name>
  <uri>http://jsp.veryedu.cn</uri>
  <tag>
    <name>foreach</name>
    <tag-class>com.zhulinjun.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>
</taglib>
package com.zhulinjun;
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;
  }
}
<%@page import="java.util.ArrayList"%>
<%@page import="com.zhulinjun.Dept"%>
<%@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.veryedu.cn" prefix="z"%>
<!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> lst=new ArrayList<Dept>();
lst.add(new Dept(1,"研发部"));
lst.add(new Dept(2,"销售部"));
lst.add(new Dept(3,"财务部"));
request.setAttribute("lst", lst);
%>
</head>
<body>
<p>自定义foreach标签</p>
<c:forEach items="${lst}" var="l">
${l.id}:${l.name}<hr> 
</c:forEach>
<hr>
<z:foreach items="${lst }" var="l">
${l.id }=========${l.name }<hr>
</z:foreach>
</body>
</html>

dept数据标签

package com.zhulinjun;
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 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> lst=new ArrayList<Dept>();
  lst.add(new Dept(1,"研发部"));
  lst.add(new Dept(2,"销售部"));
  lst.add(new Dept(3,"财务部"));
//  将数据放入var中
  pageContext.setAttribute(var, lst);
  JspWriter out = pageContext.getOut();
  return SKIP_BODY; 
}
}
<?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>z</short-name>
  <uri>http://jsp.veryedu.cn</uri>
    <tag>
    <name>dept</name>
    <tag-class>com.zhulinjun.DeptTag</tag-class>
    <body-content>JSP</body-content>
    <attribute>
        <name>var</name>
        <required>true</required>
        <rtexprvalue>true</rtexprvalue>
    </attribute>
    </tag>
</taglib>
<%@page import="java.util.ArrayList"%>
<%@page import="com.zhulinjun.Dept"%>
<%@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.veryedu.cn" prefix="z"%>
<!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> lst=new ArrayList<Dept>();
lst.add(new Dept(1,"研发部"));
lst.add(new Dept(2,"销售部"));
lst.add(new Dept(3,"财务部"));
request.setAttribute("lst", lst);
%>
</head>
<body>
<p>自定义dept标签:数据标签</p>
作用:当某一些数据多个页面,或者有多个地方频繁使用时,就可以做成数据标签,减少代码量
<z:dept var="dd"></z:dept>
商品模块使用部门数据 (财务部门才能操作以下内容):${dd }
</body>
</html>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
    <%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
    <%@taglib prefix="z" uri="http://jsp.veryedu.cn" %>
<!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>
<z:dept var="dd"></z:dept>
商品模块使用部门数据 (销售部门才能操作以下内容):${dd }
</body>
</html>

select标签

<?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>z</short-name>
  <uri>http://jsp.veryedu.cn</uri>
  <tag>
    <name>select</name>
    <tag-class>com.zhulinjun.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>false</rtexprvalue>
    </attribute>
    <attribute>
        <name>optionText</name>
        <required>true</required>
        <rtexprvalue>false</rtexprvalue>
    </attribute>
    <attribute>
        <name>selectedVal</name>
        <required>false</required>
        <rtexprvalue>true</rtexprvalue>
    </attribute>
  </tag>
</taglib>
package com.zhulinjun;
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;
private String selectedVal;//数据库中存储的数据,用于下拉框回显
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 getSelectedVal() {
  return selectedVal;
}
public void setSelectedVal(String selectedVal) {
  this.selectedVal = selectedVal;
}
@Override
public int doStartTag() throws JspException {
//  因为z:select标签没有标签体,页面又要输出下拉框的内容
  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 {
//  String 与StringBuffer的区别(面试题)
//  S听处理字符串拼接更消耗服务器内存
//  String s="Hello";//hello
//  s+="";//hello
//  s+="world";//hello world
//  StringBuffer sb=new StringBuffer();
//  sb.append("hello");
//  sb.append("");
//  sb.append("world");
  StringBuffer sb=new StringBuffer("<select>");
  for (Object object : items) {
//    object==new Dept(1, "研发部");
//    object== new Goods(2,"牛仔裤");
//    取出某一个对象的某个属性值
//    sb.append("<option value='1'>研发部</option>");
    String val=getObjAttrValue(object,optionVal);
    String text=getObjAttrValue(object, optionText);
    sb.append("<option "+(val.equals(selectedVal) ? "selected" : "")+" value='"+val+"'>"+text+"</option>");
  }
  sb.append("</select>");
  return sb.toString();
}
/**获取某个对象的某个属性值
 * @param object   dept
 * @param attr  id/name
 * @return
 * @throws Exception 
 */
private String getObjAttrValue(Object object, String attr) throws Exception{
  Class clz = object.getClass();
  Field f = clz.getDeclaredField(attr);
  f.setAccessible(true);
  return f.get(object).toString();
}
}
<%@page import="com.zhulinjun.Likes"%>
<%@page import="java.util.ArrayList"%>
<%@page import="com.zhulinjun.Dept"%>
<%@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.veryedu.cn" prefix="z"%>
<!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> lst=new ArrayList<Dept>();
lst.add(new Dept(1,"研发部"));
lst.add(new Dept(2,"销售部"));
lst.add(new Dept(3,"财务部"));
request.setAttribute("lst", lst);
List<Likes> likes=new ArrayList<Likes>();
likes.add(new Likes(1,"唱歌"));
likes.add(new Likes(2,"跳舞"));
likes.add(new Likes(3,"吃"));
likes.add(new Likes(4,"玩"));
request.setAttribute("likes", likes);
%>
</head>
<body>
<p>自定义dept标签:数据标签</p>
作用:当某一些数据多个页面,或者有多个地方频繁使用时,就可以做成数据标签,减少代码量
<z:dept var="dd"></z:dept>
商品模块使用部门数据 (财务部门才能操作以下内容):${dd }
<p>自定义select标签</p>
<%-- 新增(以前的写法)
<select>
<c:forEach items="" var="">
<option>湖南</option>
</c:forEach>
</select>
修改
<select>
<c:forEach items="" var="a">
<option ${user.address==a.adress?'selected':"" }>湖南</option>
</c:forEach>
</select> --%>
<!-- 现在 -->
<%-- <z:select items="" optionval="id" optionText="name"></z:select>
<z:select items="" optionval="id" optionText="name" selectedText="1"></z:select> --%>
<z:select optionVal="id" items="${lst }" optionText="name"></z:select>
<z:select optionVal="id" items="${lst }" optionText="name" selectedVal="2"></z:select>
<z:select optionVal="id" items="${lst }" optionText="name" selectedVal="3"></z:select>
</body>
</html>
目录
相关文章
|
6月前
|
XML SQL Java
JSP标签介绍(02)
JSP标签介绍(02)
20 0
|
6月前
J2EE&JSP标签02&Foreach标签&select
J2EE&JSP标签02&Foreach标签&select
|
2月前
|
Java
jsp页面中使用jstl标签报错:javax.servlet.jsp.JspTagException
jsp页面中使用jstl标签报错:javax.servlet.jsp.JspTagException
13 0
|
4月前
|
XML Java 数据格式
jsp标签上
jsp标签上
26 0
|
4月前
|
Java
JSP标签(2) -----自定义foreach ,select标签,全网最详细,最完整易懂
JSP标签(2) -----自定义foreach ,select标签,全网最详细,最完整易懂
|
4月前
|
Java API
JSP标签 01 完整详细
JSP标签 01 完整详细
|
4月前
|
Java
JAVA2EE-----jsp标签(02)
JAVA2EE-----jsp标签(02)
26 0
|
4月前
|
Java
JSP标签(01)
JSP标签(01)
27 0
|
4月前
|
Java 数据安全/隐私保护
Shiro - JSP页面标签应用
Shiro - JSP页面标签应用
29 0
|
5月前
|
XML Java 程序员
自定义JSP标签
自定义JSP标签

相关课程

更多