- 浏览: 6308840 次
- 性别:
- 来自: 一片神奇的土地
文章分类
- 全部博客 (745)
- JQuery (25)
- JS (33)
- 数据库 (59)
- Java基础 (56)
- JSON (8)
- XML (8)
- ireport (7)
- 设计模式 (10)
- 心情 (14)
- freemarker (1)
- 问题 (15)
- powerdesigner (2)
- CSS (15)
- DWR (4)
- tomcat (16)
- Hibernate (12)
- Oracle (7)
- Struts (7)
- Spring (34)
- JSP (23)
- 需学习 (64)
- 工具类库 (63)
- Maven (14)
- 笔试题 (34)
- 源码学习 (31)
- 多线程 (39)
- Android (32)
- 缓存 (20)
- SpringMVC (14)
- jQueryEasyUi (12)
- webservice-RPC (13)
- ant (1)
- ASP.NET (10)
- 正则表达式 (3)
- Linux (15)
- JBoss (1)
- EJB (3)
- UML (2)
- JMS (3)
- Flex (8)
- JSTL (2)
- 批处理 (5)
- JVM (16)
- 【工具】 (16)
- 数据结构 (29)
- HTTP/TCP/Socket (18)
- 微信 (1)
- tomcat源码学习 (15)
- Python (30)
- 主机 (2)
- 设计与架构 (19)
- thrift-RPC (2)
- nginx (6)
- 微信小程序 (0)
- 分布式+集群 (12)
- IO (1)
- 消息队列 (4)
- 存储过程 (8)
- redis (9)
- zookeeper (5)
- 海量数据 (5)
最新评论
-
360pluse:
技术更新,战术升级!Python爬虫案例实战从零开始一站通网盘 ...
Python爬虫实战:Scrapy豆瓣电影爬取 -
18335864773:
推荐用 pageoffice 组件生成 word 文件。
JAVA生成WORD工具类 -
jjhe369:
LISTD_ONE 写道起始地址为163.135.0.1 结束 ...
IP地址与CIDR -
baojunhu99:
private final int POOL_SIZE = 5 ...
使用CompletionService获取多线程返回值 -
LovingBaby:
胡说,javascript 运行时是单线程的,event lo ...
Ajax请求是否可以实现同步
实际使用:
<td style="text-align: center;"> <b:Call type="com.techson.util.DateUtil" method="dateToStr" var="checkin"> <b:Param paramclass="java.util.Date" value="${vo.checkin}" /> <b:Param paramclass="java.lang.String" value="@dd@-@MMM@-@yy@" /> </b:Call> ${checkin} </td>
输出的日期是Date类型,在dateToStr方法中将其格式化并转化为String类型显示,如
标签引入:
<%@ taglib uri="/WEB-INF/utils.tld" prefix="b"%>
utils.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> <short-name>utils</short-name> <uri>/WEB-INF/utils</uri> <tag> <name>Call</name> <tag-class>tags.Call</tag-class> <body-content>scriptless</body-content> <attribute> <name>bean</name> <required>false</required> <rtexprvalue>true</rtexprvalue> <type>java.lang.Object</type> </attribute> <attribute> <name>type</name> <required>false</required> <rtexprvalue>true</rtexprvalue> <type>java.lang.String</type> </attribute> <attribute> <name>method</name> <required>true</required> <rtexprvalue>true</rtexprvalue> <type>java.lang.String</type> </attribute> <attribute> <name>var</name> <rtexprvalue>true</rtexprvalue> <type>java.lang.String</type> </attribute> <attribute> <name>scope</name> <rtexprvalue>true</rtexprvalue> <type>java.lang.String</type> </attribute> <dynamic-attributes>true</dynamic-attributes> </tag> <tag> <name>Param</name> <tag-class>tags.ParamTag</tag-class> <body-content>empty</body-content> <attribute> <name>paramclass</name> <required>true</required> <rtexprvalue>true</rtexprvalue> <type>java.lang.String</type> </attribute> <attribute> <name>value</name> <required>true</required> <rtexprvalue>true</rtexprvalue> <type>java.lang.Object</type> </attribute> </tag> </taglib>
tags.ParamTag.java类:
package tags; import javax.servlet.jsp.JspException; public class ParamTag extends SimpleTagSupport { private java.lang.String _class; private java.lang.Object value; public void doTag() throws JspException { StrictTypeParams parent = (StrictTypeParams)findAncestorWithClass(this, StrictTypeParams.class); //调用父标签,即b:call if (parent == null) { throw new JspException("The Param tag is not enclosed by a supported tag type"); } parent.addStrictTypeParameter(this._class, this.value); //addStr..是父标签中方法 } /** * Setter for the class attribute. */ public void setParamclass(java.lang.String value) { this._class = value; } public void setValue(java.lang.Object value) { this.value = value; } }
StrictTypeParams:一个引用父标签的接口
package tags; public interface StrictTypeParams { void addStrictTypeParameter(String type, Object value); }
tags.Call.java类:
* Call.java package tags; import java.lang.reflect.Method; public class Call extends SimpleTagSupport implements DynamicAttributes, StrictTypeParams { /** * Initialization of bean property. */ private java.lang.Object bean; private java.lang.String type; private java.lang.String method; private java.lang.String var = null; private int scope = PageContext.PAGE_SCOPE; private List dynamicAttrValues = new ArrayList(); private List typeList = null; //doTag()为标签默认执行方法 public void doTag() throws JspException { JspWriter out=getJspContext().getOut(); try { JspFragment f=getJspBody(); if (f != null) f.invoke(out); } catch (java.io.IOException ex) { throw new JspException(ex.getMessage()); } boolean isStaticMethod = this.type!=null?true:false; Class cls = null; if(isStaticMethod){ try { cls = Class.forName(this.type); } catch (ClassNotFoundException e1) { throw new JspException("Class not be found for your type!"); } }else{ cls = bean.getClass(); } Method[] methods = cls.getMethods(); Method m = null; Class[] paramTypes = null; Method : for (int i = 0; i < methods.length; i++ ) { Method t = methods[i]; paramTypes = t.getParameterTypes(); //这个方法的参数类型的集合 if (t.getName().equals(method) && paramTypes.length == dynamicAttrValues.size()) { if (typeList != null){ for (int j = 0; j < paramTypes.length; j++){ if (!paramTypes[j].getName().equals(typeList.get(j))){ continue Method; } } } m = t; break; } } if (m == null) throw new JspException("JavaBean Object hasn't no method named '" + method + "'"); try { Object coercedValues[] = new Object[paramTypes.length]; Iterator paramIterator = dynamicAttrValues.iterator(); for (int i = 0; i < paramTypes.length; i++) { Object paramValue = paramIterator.next(); Class paramClass = paramTypes[i]; if (paramValue == null || paramValue.getClass() != paramClass) try { paramValue = Coercions.coerce(paramValue, paramClass, null); } catch (ELException e) { throw new JspException(e.getMessage(), e.getRootCause()); } coercedValues[i] = paramValue; } Object returnVal = null; if(isStaticMethod){ returnVal = m.invoke(null, coercedValues); }else{ returnVal = m.invoke(bean, coercedValues); } if (var != null) if (returnVal != null) getJspContext().setAttribute(var, returnVal, scope); else getJspContext().removeAttribute(var, scope); } catch (Exception ex) { ex.printStackTrace(); throw new JspException("Call method is Error!"); } } /** * Setter for the bean attribute. */ public void setBean(java.lang.Object value) throws JspException { if (value == null) throw new JspException("Null 'bean' attribute in 'Call' tag"); this.bean = value; } /** * Setter for the method attribute. */ public void setMethod(java.lang.String value) throws JspException { if (value == null) throw new JspException("Null 'method' attribute in 'Call' tag"); this.method = value; } /** * Setter for the var attribute. */ public void setVar(java.lang.String value) throws JspException { if (value == null) throw new JspException("Null 'var' attribute in 'Call' tag"); this.var = value; } /** * Setter for the scope attribute. */ public void setScope(java.lang.String value) { if (value.equalsIgnoreCase("page")) scope = PageContext.PAGE_SCOPE; else if (value.equalsIgnoreCase("request")) scope = PageContext.REQUEST_SCOPE; else if (value.equalsIgnoreCase("session")) scope = PageContext.SESSION_SCOPE; else if (value.equalsIgnoreCase("application")) scope = PageContext.APPLICATION_SCOPE; } public void setDynamicAttribute(String uri, String localName, Object value) throws JspException { dynamicAttrValues.add(value); } public void addStrictTypeParameter(String type, Object value) { if (typeList == null) typeList = new ArrayList(); typeList.add(type); dynamicAttrValues.add(value); } public void setType(java.lang.String type) { this.type = type; } public static void main(String[] agrs) throws ClassNotFoundException, JspException{ String type = "com.techson.util.DateUtil"; String method="dateDiff"; List dynamicAttrValues = new ArrayList(); List typeList = new ArrayList(); typeList.add("java.lang.Boolean"); dynamicAttrValues.add(true); Class cls = Class.forName(type); Method[] methods = cls.getMethods(); Method m = null; Class[] paramTypes = null; Method : for (int i = 0; i < methods.length; i++ ) { Method t = methods[i]; paramTypes = t.getParameterTypes(); if (t.getName().equals(method) && paramTypes.length == dynamicAttrValues.size()) { if (typeList != null){ for (int j = 0; j < paramTypes.length; j++){ if (!paramTypes[j].getName().equals(typeList.get(j))){ continue Method; } } } m = t; break; } } if (m == null) throw new JspException("JavaBean Object hasn't no method named '" + method + "'"); try { Object coercedValues[] = new Object[paramTypes.length]; Iterator paramIterator = dynamicAttrValues.iterator(); for (int i = 0; i < paramTypes.length; i++) { Object paramValue = paramIterator.next(); Class paramClass = paramTypes[i]; System.out.println(paramClass); if (paramValue == null || paramValue.getClass() != paramClass) try { System.out.println(paramValue.getClass() + "/ "+ paramClass); paramValue = Coercions.coerce(paramValue, paramClass, null); } catch (ELException e) { throw new JspException(e.getMessage(), e.getRootCause()); } coercedValues[i] = paramValue; } Object returnVal = null; returnVal = m.invoke(null, coercedValues); System.out.print(returnVal); } catch (Exception ex) { ex.printStackTrace(); throw new JspException("Call method is Error!"); } } }
处理方法DateUtils.java中方法dateToStr:
public static String dateToStr(Calendar cal , String format) { if(format.contains("@y")){ int _y = cal.get(Calendar.YEAR); String _yyyy = new DecimalFormat("0000").format(_y); String _yy = _yyyy.substring(2); format = format.replaceAll("@[y]{4}@",_yyyy) .replaceAll("@[y]{2}@",_yy) .replaceAll("@[y]{1}@","" + _y); } if(format.contains("@M")){ int _M= cal.get(Calendar.MONTH) + 1; String _MM = (_M<10)?"0"+_M:"" + _M; String _MMM = Constants.MTH[_M - 1]; format=format.replaceAll("@MMM@",_MMM); format=format.replaceAll("@MM@",_MM); format=format.replaceAll("@M@","" + _M); } if(format.contains("@d")){ int _D= cal.get(Calendar.DATE); String _DD=(_D<10)?"0"+_D:""+_D; format=format.replaceAll("@dd@",_DD); format=format.replaceAll("@d@","" +_D); } if(format.contains("@h")){ int _h=cal.get(Calendar.HOUR_OF_DAY); String _hh=(_h<10)?"0"+_h:"" +_h; format=format.replaceAll("@hh@",_hh); format=format.replaceAll("@h@","" + _h); } if(format.contains("@m")){ int _m=cal.get(Calendar.MINUTE); String _mm=(_m<10)?"0"+_m:""+_m; format=format.replaceAll("@mm@",_mm); format=format.replaceAll("@m@","" + _m); } if(format.contains("@s")){ int _s=cal.get(Calendar.SECOND); String _ss=(_s<10)?"0"+_s:"" + _s; format=format.replaceAll("@ss@",_ss); format=format.replaceAll("@s@","" + _s); } if(format.contains("@W")){ int _W=cal.get(Calendar.DAY_OF_WEEK); format=format.replaceAll("@WWW@",Constants.WEEK[_W-1]); format=format.replaceAll("@WW@",Constants.WEK[_W-1]); format=format.replaceAll("@W@","" + (_W-1)); } return format; } public static String dateToStr(Date d , String format) { Calendar cal = Calendar.getInstance(); cal.setTime(d); return dateToStr(cal,format); }
参考:http://zengsun.iteye.com/category/25493?show_full=true
http://www.ibm.com/developerworks/cn/java/j-lo-jsp2tag/index.html?ca=drs-
http://blog.csdn.net/zavens/article/details/1685615
findAncestorWithClass:
http://www.ibm.com/developerworks/cn/java/j-taglib/
getParameterTypes:
发表评论
-
Form的onsubmit
2015-02-05 11:06 47141. <form class="for ... -
使文本框不可编辑[不太常用的方法]
2012-09-10 11:17 2374为了使本框不可编辑,我们可以使用下面几种方法 解决思路: 文本 ... -
C 标签使用
2012-07-26 12:16 2885<title>虚拟机管控</title&g ... -
显示结果排序
2012-05-04 14:15 1253<td width="9%" s ... -
select级联操作的简便方法
2012-04-18 15:07 7730<script type=&quo ... -
GuestName处理方法
2012-01-31 10:39 2057主要考虑如何设计,如何实现,如何思考! 效果: ... -
[基础]Map在JSP中的两种显示方式
2011-10-27 09:36 138171、 Java: for(EglSpecialRequ ... -
[转]如何用div+css布局页面
2011-08-01 17:58 2256如今web2.0炒的很厉害, ... -
iframe总结 + window.open
2011-06-20 11:47 15850从frame中跳出并转向: parent.window. ... -
window.showModalDialog()
2011-06-01 15:23 3555window.open() 打开一个普通窗口 win ... -
一个星期中某一天存在即显示的思路
2011-05-31 17:48 11221、 days: 1,2,3,4,5,6,0 // ... -
某一模块动态+、—效果的实现
2011-04-21 11:08 1332代码(去掉部分css细节,只关注主要功能): < ... -
批量更新时判断哪个需要更新
2011-04-06 16:03 1842<input type="checkbox ... -
JSP中循环二维数组 + 循环Map
2011-04-01 16:46 7180代码如下: <C:forEach items=&q ... -
使用Table的insertRow实现某一模块的复制
2011-03-21 14:16 3287实现的效果:点击+号添加一组 JSP页面: < ... -
[基础]由复选框实现的批量删除
2011-03-21 11:31 2959批量删除: 选中多个复选框: 每一条记录: ... -
table总结insertRow、deleteRow
2011-03-16 16:13 20466表格有几行: var trCnt = table.rows.l ... -
struts中用到的复选框和查询技巧
2011-03-09 11:06 123实现效果:1、选中复选框时,其后文本框显示,没选中时,隐藏 ... -
js实现的复选框选中时的批量操作
2011-03-04 14:22 56731. var checkedList = []; // sa ... -
利用getParameterNames实现一个按钮所有模块数据都提交
2010-11-27 13:13 15141.总结: 1、注意 ...
相关推荐
JSP自定义标签JSP自定义标签JSP自定义标签JSP自定义标签JSP自定义标签JSP自定义标签
本人自行研究JSP自定义标签的学习笔记 目录如下: 目录 一、概述 1 1.1 使用简单标签机制 2 1.2 使用标签文件 3 二 、自定义标签简介 3 2.1 自定义标签概念 3 2.2 标签相关概念 3 2.3 如何创建自定义标签 3 三、自...
jsp 自定义标签 一个简单的自定义标签实现
jsp自定义标签jsp自定义标签jsp自定义标签
在网上学习jsp自定义标签的时候, 出现了异常, 在网上寻找了好久, 都没有找到合适的答案, 自己通过摸索最后成功, 分享给大家, 希望能够帮助到你, 自定义标签不能放在java Project中, 只能在java web Project中才可以.
jsp自定义标签jsp自定义标签jsp自定义标签jsp自定义标签
使用jsp自定义标签的功能实现权限的控制。(如果用户没有某个模块的删除权限,就不现实这个删除按钮) 在整个项目中所有的页面都可以引入自定义的标签去做到权限的控制。 自定义标签文件 删除 可以控制页面中的每...
jsp自定义标签库实现数据列表显示,包括代码,文档,希望对大家有帮助.
jsp自定义标签例子,能在Tomcat下直接运行 具体讲解参考 http://blog.csdn.net/zwhfyy/archive/2008/06/13/2543904.aspx
权威实用的jsp自定义标签demo,checkbox,radio> 导入eclipse 部署 看效果吧 带源代码哦
是用jsp自定义标签编写的一个非常不错的分页,扩展性非常不错,不依赖于数据库。
JSP自定义标签。在自定义标签的起始和结束标签之间的部分为标签体(Body)。 标签处理程序类:定义标签的行为,并在JSP引擎遇到自定义标签时调用执行。 标签库描述符(TLD)文件:描述标签库的XML文档,向JSP引擎...
jsp初学者实例,以及分页封装,下拉框绑定值封装
jsp自定义标签,比如说if,foreach,out,format
java jsp自定义标签非常好用大家下载学习java jsp自定义标签非常好用大家下载学习java jsp自定义标签非常好用大家下载学习java jsp自定义标签非常好用大家下载学习java jsp自定义标签非常好用大家下载学习java jsp...
创建JSP自定义标签.doc 创建JSP自定义标签.doc 创建JSP自定义标签.doc