您的位置:首页 > Web前端 > JavaScript

JSP自定义标签

2019-06-11 18:34 2456 查看

jsp自定义标签

自定义标签:我们所需要的jar包,我们所导入的C标签可以参考来结合自己定义的自定义标签

其次
编写一个普通的java类,继承BodyTagSupport类~

(1)、创建一个Java类,编写select标签处理类:

package com.zking.jsp.day02;

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;

/**
* 1、值的传递 id、name 2、数据源 Items 3、展示列与数据存储与实体类的对应关系 textKey textVal 4、数据回显
* selectedVal 5、可能下拉默认值(头标签) headerTextKey headerTextVal
*
*/
public class SelectTag extends BodyTagSupport {

private static final long serialVersionUID = -7624246014743974400L;
private String id;
private String name;
private List<Object> items = new ArrayList<>();
private String textKey; // id=textKey
private String textVal;
private String selectedVal;
private String headerTextKey;
private String headerTextVal;

@Override
public int doStartTag() throws JspException {
JspWriter out = pageContext.getOut();
try {
try {
out.print(toHTML());
} catch (NoSuchFieldException e) {
e.printStackTrace();
} catch (SecurityException e) {
e.printStackTrace();
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
}
} catch (IOException e) {
e.printStackTrace();
}
return SKIP_BODY;
}

private String toHTML()
throws NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException {
StringBuffer sb = new StringBuffer();
sb.append("<select id='" + id + "' name='" + name + "'>");// 初始select
if (!(headerTextKey == null || "".equals(headerTextVal) || headerTextVal == null || "".equals(headerTextVal))) {
sb.append("<option selected value='" + headerTextKey + "'>" + headerTextVal + "</option>"); // 拼接
}
String value;
String html;
for (Object obj : items) {
Field field = obj.getClass().getDeclaredField(textKey);
field.setAccessible(true);
value = (String) field.get(obj);
Field textValField = obj.getClass().getDeclaredField(textVal);
textValField.setAccessible(true);
html = (String) textValField.get(obj);
if (value.equals(selectedVal)) {
sb.append("<option selected value='" + value + "'>" + html + "</option>");
} else {
sb.append("<option value='" + value + "'>" + html + "</option>");
}
}
return sb.toString();
}

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;
}

public List<Object> getItems() {
return items;
}

public void setItems(List<Object> items) {
this.items = items;
}

public String getTextKey() {
return textKey;
}

public void setTextKey(String textKey) {
this.textKey = textKey;
}

public String getTextVal() {
return textVal;
}

public void setTextVal(String textVal) {
this.textVal = textVal;
}

public String getSelectedVal() {
return selectedVal;
}

public void setSelectedVal(String selectedVal) {
this.selectedVal = selectedVal;
}

public String getHeaderTextKey() {
return headerTextKey;
}

public void setHeaderTextKey(String headerTextKey) {
this.headerTextKey = headerTextKey;
}

public String getHeaderTextVal() {
return headerTextVal;
}

public void setHeaderTextVal(String headerTextVal) {
this.headerTextVal = headerTextVal;
}

public SelectTag() {
}
}

(2)、创建一个Java类,编写checkbox标签处理类:

package com.zking.jsp.day03;

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 CheckboxTag extends BodyTagSupport {
private static final long serialVersionUID = 1725404744433327629L;
private String textKey;
private String textVal;
private List<Object> checkedVal=new ArrayList<>();
private List<Object> item=new ArrayList<>();

@Override
public int doStartTag() throws JspException {
JspWriter out = pageContext.getOut();
try {
out.print(toHTML());
} catch (IOException e) {
e.printStackTrace();
} catch (NoSuchFieldException e) {
e.printStackTrace();
} catch (SecurityException e) {
e.printStackTrace();
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
}
return super.doStartTag();
}
private String toHTML() throws NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException {
StringBuffer sb=new StringBuffer();
String value;
String html;
for (Object obj : item) {
Field field = obj.getClass().getDeclaredField(textKey);
field.setAccessible(true);
value=(String)field.get(obj);
Field textValfield = obj.getClass().getDeclaredField(textVal);
textValfield.setAccessible(true);
html=(String)textValfield.get(obj);
if(checkedVal.contains(value)) {
sb.append("<input checked type='checkbox' value='"+value+"' />"+html+"");
}
else {
sb.append("<input type='checkbox' value='"+value+"' />"+html+"");
}
}
return sb.toString();
}

public List<Object> getItem() {
return item;
}
public void setItem(List<Object> item) {
this.item = item;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}

public String getTextKey() {
return textKey;
}
public List<Object> getCheckedVal() {
return checkedVal;
}
public void setCheckedVal(List<Object> checkedVal) {
this.checkedVal = checkedVal;
}
public void setTextKey(String textKey) {
this.textKey = textKey;
}
public String getTextVal() {
return textVal;
}
public void setTextVal(String textVal) {
this.textVal = textVal;
}

public CheckboxTag() {
super();
}

public CheckboxTag(String textKey, String textVal, List<Object> checkedVal, List<Object> item) {
super();
this.textKey = textKey;
this.textVal = textVal;
this.checkedVal = checkedVal;
this.item = item;
}

}

其次,我们写jsp自定义标签需要建立tld约束配置
在你的web应用目录下,找到WEB-INF文件夹,在里面新建一个tld类型的文件:

<?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>ing 1.1 core library</description>
<display-name>ing core</display-name>
<tlib-version>1.1</tlib-version>
<short-name>c</short-name>
<uri>/ing</uri>

<tag>
<name>select</name>
<tag-class>com.zking.jsp.day02.SelectTag</tag-class>
<body-content>JSP</body-content>

<attribute>
<name>id</name>
<required>false</required>
<rtexprvalue>false</rtexprvalue>
</attribute>
<attribute>
<name>name</name>
<required>false</required>
<rtexprvalue>false</rtexprvalue>
</attribute>
<attribute>
<name>items</name>
<required>true</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
<attribute>
<name>textKey</name>
<required>true</required>
<rtexprvalue>false</rtexprvalue>
</attribute>
<attribute>
<name>textVal</name>
<required>true</required>
<rtexprvalue>false</rtexprvalue>
</attribute>
<attribute>
<name>selectedVal</name>
<required>false</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
<attribute>
<name>headerTextKey</name>
<required>false</required>
<rtexprvalue>false</rtexprvalue>
</attribute>
<attribute>
<name>headerTextVal</name>
<required>false</required>
<rtexprvalue>false</rtexprvalue>
</attribute>
</tag>

<tag>
<name>checkbox</name>
<tag-class>com.zking.jsp.day03.CheckboxTag</tag-class>
<body-content>JSP</body-content>

<attribute>
<name>textKey</name>
<required>true</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
<attribute>
<name>textVal</name>
<required>true</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
<attribute>
<name>item</name>
<required>true</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
<attribute>
<name>checkedVal</name>
<required>false</required>
<rtexprvalue>true</rtexprvalue>
</attribute>

</tag>

</taglib>

我们就测试这两个自定义标签(1、selectTag 2、checkboxTag)

开始使用自定义标签:
1、selectTag

<%@page import="java.util.ArrayList"%>
<%@page import="com.zking.jsp.day02.Student"%>
<%@page import="java.util.List"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib uri="/ing" 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>
</head>
<body>

<%
List list = new ArrayList();
list.add(new Student("001","xiaoli"));
list.add(new Student("002","xiaogfhd"));
list.add(new Student("003","xiaoshs"));
request.setAttribute("stus", list);

%>

<z:select headerTextKey="-1" headerTextVal="---请选择---" textVal="name" items="${stus }" textKey="id" selectedVal="2"></z:select>

</body>
</html>

輸出結果:


2、checkboxTag

<%@page import="com.zking.jsp.day02.Student"%>
<%@page import="java.util.ArrayList"%>
<%@page import="java.util.List"%>
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<%@ taglib uri="/ing"  prefix="z"%>

<html>
<head>
<title>Insert title here</title>
</head>
<body>
<%
List list = new ArrayList();
list.add(new Student("001","xiaoyanjing"));
list.add(new Student("002","dayanjing"));
list.add(new Student("003","meiyanjing"));
list.add(new Student("004","xiaziyanjing"));
request.setAttribute("stus", list);

List ls=new ArrayList();
ls.add("001");
ls.add("002");
ls.add("003");
ls.add("004");
request.setAttribute("list", ls);
%>

<z:checkbox textVal="name"  checkedVal="${list}" item="${stus }" textKey="id"></z:checkbox>
</body>
</html>

输出结果:

OK啦,这样的话,selecttag与checkboxtag的自定义标签就完成了

注意:别忘了导该导的jar包
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: