JSP自定义标签

时间:2023-05-17 13:27:50

在JSP网页编程中,我们通常不满足于jstl或其他的框架,我们也可以自己写属于自己专用的标签。

在这里介绍一下表格中展示JavaBean的自定义标签的使用

第一步:写一个父标签,用来显示获取数据

  新建一个ShowBeanTag.java

package com.wuyu.tags;
import java.io.IOException;
import java.lang.reflect.Field;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.TagSupport; public class ShowBeanTag extends TagSupport {
private static final long serialVersionUID = 1L;
private String test;
private Object object;
@Override
public int doStartTag() throws JspException {
try {
JspWriter out = this.pageContext.getOut();
out.println("<table>");
} catch (SecurityException e) {
e.printStackTrace();
} catch(IOException e){
e.printStackTrace();
} catch (IllegalArgumentException e) {
e.printStackTrace();
}
return EVAL_BODY_INCLUDE;
} @Override
public int doEndTag() throws JspException {
// TODO Auto-generated method stub
JspWriter out = this.pageContext.getOut();
try {
out.println("</table>");
} catch (IOException e) {
e.printStackTrace();
}
return SKIP_BODY;
}
public String getTest() {
return test;
} public void setTest(String test) {
this.test = test;
} public Object getObject() {
return object;
} public void setObject(Object object) {
this.object = object;
}
}

  第二步:编写DisplayColumnTag.java,子标签的类

package com.wuyu.tags;

import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method; import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.JspTag;
import javax.servlet.jsp.tagext.TagSupport; public class DisplayColumnTag extends TagSupport {
/**
*
*/
private static final long serialVersionUID = 1L;
private String property;
private String showValue;
@Override
public int doStartTag() throws JspException {
try {
JspWriter out = this.pageContext.getOut();
JspTag parent = getParent();
ShowBeanTag sbt = (ShowBeanTag)parent;
Object obj = sbt.getObject();
property = property.substring(0, 1).toUpperCase() + property.substring(1);
Method getMethod = obj.getClass().getMethod("get" + property, new Class[] {});
out.println("<tr>");
if(showValue != null){
out.println("<td>"+ showValue +"</td>");
}else{
out.println("<td>"+ property +"</td>");
}
out.println("<td>"+ getMethod.invoke(obj, new Object[]{}) +"</td>");
out.println("</tr>");
}catch(IOException e){
e.printStackTrace();
}catch(NoSuchMethodException e){
e.printStackTrace();
}catch (SecurityException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InvocationTargetException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return EVAL_BODY_INCLUDE;
} public String getProperty() {
return property;
}
public void setProperty(String property) {
this.property = property;
} public String getShowValue() {
return showValue;
} public void setShowValue(String showValue) {
this.showValue = showValue;
} @Override
public int doEndTag() throws JspException {
// TODO Auto-generated method stub
return EVAL_PAGE;
}
}

  第三步:写tld文件

    在WEB-INF文件夹下,新建一个文件ShowBeanTag.tld,其内容如下

<?xml version="1.0" encoding="UTF-8"?>
<taglib version="2.0" 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 web-jsptaglibrary_2_0.xsd">
<tlib-version>1.0</tlib-version>
<jsp-version>2.0</jsp-version>
<short-name>b</short-name>
<uri>/showBeanTag</uri>
<tag>
<name>showBeanTag</name>
<tag-class>com.wuyu.tags.ShowBeanTag</tag-class>
<body-content>scriptless</body-content>
<attribute>
<name>test</name>
<required>false</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
</tag>
<tag>
<name>displayColumnTag</name>
<tag-class>com.wuyu.tags.DisplayColumnTag</tag-class>
<body-content>empty</body-content>
<attribute>
<name>property</name>
<required>true</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
<attribute>
<name>showValue</name>
<required>false</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
</tag>
</taglib>

  第四步:在web.xml中配置

  

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
<jsp-config>
<taglib>
<taglib-uri>/showBeanTag</taglib-uri>
<taglib-location>/WEB-INF/ShowBeanTag.tld</taglib-location>
</taglib>
</jsp-config>
</web-app>

  第五步:新建一个JavaBean  User.java

package com.wuyu.bean;

public class User {
private String userName;
private int age;
private String email;
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
}

  

  第六步,在index.jsp中使用

<%@ page language="java" contentType="text/html; charset=UTF-8"
import="com.wuyu.bean.User,java.util.*" pageEncoding="UTF-8"%>
<%@ taglib uri="/showBeanTag" prefix="b" %> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<%
User user = new User();
user.setUserName("wuyu");
user.setAge(24);
user.setEmail("512302907@qq.com");
pageContext.setAttribute("user", user);
%> <html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<b:showBeanTag test="user">
<b:displayColumnTag property="userName" showValue="用户名"/>
<b:displayColumnTag property="age" showValue="年龄"/>
</b:showBeanTag>
</body>
</html>