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中也有(报 ...
随机推荐
- T-SQL、JET SQL、PL-SQL
数据库分为网状.层状.关系.对象四种类型,目前的数据库一般都是属于关系数据库(包括MYSQL和SQL SERVER),网状.层状基本上已成历史,对象类型尚未普及. SQL 语言是有 ANSI 标准的. ...
- 【JAVA】【leetcode】【使用堆栈实现后向计算】
题目:evaluate-reverse-polish-notation 要求: Evaluate the value of an arithmetic expression in Reverse Po ...
- win10 开机黑屏只显示鼠标
最靓,win10开机黑屏,网上试了很多办法没有解决,花了3个小时终于解决了,现在介绍一下方法. 首先,在黑屏模式下进入任务管理器,文件=>运行新任务=>Explorer.exe 此时桌面已 ...
- 分享一些关于PHP时间函数的常用时间
<?php // 各种时间函数 echo "现在:".date("Y-m-d H:i:s")."<br>"; echo & ...
- Webstorm 下的Angular2.0开发之路
人一旦上了年纪,记忆力就变得越来越不好. 最近写了许多的博文,倒不是为了给谁看,而是方便自己来搜索,不然一下子又忘记了. 如果恰巧帮助到了你,也是我的荣幸~~~~~~~~~~~~ 废话不多说,看正题~ ...
- iframe自动高度
<script> //设置iframe自动高度 function setIframe(id){ var fn = function(){ try{ var iframe = typeof ...
- Intellij Idea 编辑器使用之 安装、破解 版本15.0.1
工欲善其事必先利其器. 早就听说了Intellij idea 这个编辑器.也曾尝试过,由于对eclipse,myeclipse用的比较习惯.顺手了,所以对于Intellij idea 也只是浅尝辄止. ...
- 【EasyUI】combotree和combobox模糊查询
这里说的模糊查询指在输入框输入,然后自动在下拉框中显示匹配结果,类似Google搜索提示 EasyUI库已经实现了combobox的查询过滤功能,但只能从头匹配,原因是EasyUI库的代码限制: fi ...
- SVN使用教程
SVN简介: 为什么要使用SVN? 程序员在编写程序的过程中,每个程序员都会生成很多不同的版本,这就需要程序员有效的管理代码,在需要的时候可以迅速,准确取出相应的版本. Subversion是什么? ...
- Mysql 服务在本机,需要单机调试Mysql数据库 发生 不认识hostname‘localhost’
今天在本机安装Mysql Server然后用Workbench打开,连接本机数据库 hostname:localhost port:3306 弹出:localhost 不能连接 错误-1042 尝试了 ...