JSP自定义标签
在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>
JSP自定义标签的更多相关文章
- JSP 自定义标签
0 标签技术的API继承体系 1 作用 jsp自定义标签用于移除页面中的java代码 2 实现 2.1 标签处理类ViewIPTag.java package com.zsm.util; import ...
- JSP自定义标签开发入门
一般情况下开发jsp自定义标签需要引用以下两个包 import javax.servlet.jsp.*; import javax.servlet.jsp.tagext.*; 首先我们需要大致了解开发 ...
- 一个简单的jsp自定义标签
学到了一个简单的jsp自定义标签,后面有更多的例子,会更新出来: 例子1: 步骤: 1.编写标签实现类: 继承javax.servlet.jsp.tagext.SimpleTagSupport; 重写 ...
- jsp自定义标签分析
jsp自定义标签的优势体现在于jsp页面上面减少了java代码. jsp自定义标签有三大部分组成,首先是类继承TagSupport,实现doStartTag方法. public int doStart ...
- JSP自定义标签库
总所周知,JSP自定义标签库,主要是为了去掉JSP页面中的JAVA语句 此处以格式化输出时间戳为指定日期格式为例,简单介绍下JSP自定义标签的过程. 编写标签处理类(可继承自javax.servlet ...
- JSP自定义标签配置
JSP自定义标签配置 JSP自定义标签 <taglib> <taglib-uri>/WEB-INF/you.tld</taglib-uri> ...
- jsp 自定义标签解决jsp页面中int时间戳的时间格式化问题
jsp 自定义标签解决jsp页面中int时间戳的时间格式化问题 之前在项目中根据需求,需要自定义标签,经过查询w3c文档,自己也踩了一些坑,特此记录自定义标签的步骤,下面就以我之前的一个例子中的定义一 ...
- java JSP自定义标签
来至: http://blog.csdn.net/jiangwei0910410003/article/details/23915373 http://blog.csdn.net/jiangwei09 ...
- JSP自定义标签开发入门《转》
JSP自定义标签开发入门 一般情况下开发jsp自定义标签需要引用以下两个包 import javax.servlet.jsp.*; import javax.servlet.jsp.tagext.*; ...
- 报错分析---->jsp自定义标签:类cannot be resolved to a type
这个困扰我一个晚上,仔细上网查阅发现,主要是因为jsp自定义标签要用到的这个jsp-api.jar的问题 这是我eclipes中的jar: 然而jsp-api.jar这个jar在tomcat中也有(报 ...
随机推荐
- Android 自定义View
Android 自定义View流程中的几个方法解析: onFinishInflate():从布局文件.xml加载完组件后回调 onMeasure() :调用该方法负责测量组件大小 onSizeChan ...
- SpringMVC之controller篇
概述 继 spring 2.0 对 Spring MVC 进行重大升级后,Spring 2.5 又为 Spring MVC 引入了注解驱动功能.现在你无须让 Controller 继承任何接口,无需在 ...
- Android布局整理Relative/Linear
1.RelativeLayout布局 android:layout_centerHorizontal 水平居中 android:layout_centerVertical 垂直居中 android:l ...
- C++中的const和指针组合
在C++里,const修饰指针有以下三种情况 (1)指针常量:即指向常量的指针 const int *p或者int const *p const在*前,,可以这样理解它的功能,因为const在*前, ...
- MySQL数据库的备份与还原
http://www.cnblogs.com/lql123/p/6090681.html //安装WordPress 1.备份 密码为:AAAzzz//123 mysqldump -uroot ...
- 环境jdk、编码不一致造成的项目报错
一个项目在eclipse 中可以运行 , 到另一个eclipse 中不能运行,多是因为jdk过低.包没有引人.环境jdk.编码不一致造成的.或者是因为编译文件在另一个环境里跟JDK等 不匹配. 解决办 ...
- Galera集群server.cnf参数调整--Innodb存储引擎内存相关参数(一)
在innodb引擎中,内存的组成主要有三部分:缓冲池(buffer pool),重做日志缓存(redo log buffer),额外的内存池(additional memory pool).
- phpize建立php扩展 Cannot find config.m4
centos php 安装 memcache 扩展的时候 爆 Cannot find config.m4 错误 解决方案参考以下文章 参考文章 http://blog.csdn.net/wgl ...
- 伪静态重写模块rewrite.dll及httpd.ini文件参考下载
伪静态重写模块rewrite.dll及httpd.ini文件参考下载 http://www.ledaokj.com/download/rewrite.rar 服务器端开启伪静态,可以查看以下文章< ...
- 临界区 TRTLCriticalSection 和 TCriticalSection
临界区对象TCriticalSection(Delphi) 与 TRtlCriticalSection 的区别 TRtlCriticalSection 是一个结构体,在windows单元中定义: 是I ...