JSP自定义标签 函数,实现生日计算年龄
来源:互联网 发布:系统动力学软件 价格 编辑:程序博客网 时间:2024/05/14 23:45
在Spring+hibernate的框架中,通过生日计算年龄比较麻烦,通过SQL计算年龄也可以实现,还是挺麻烦的,测试过几种方法后,还是在显示层,通过JSP的自定义标签实现比较简单。
1、创建标签的处理类(Tag Handler Class)
package com.kingmed.jusmartcare.health.service;import java.util.Date;import java.text.SimpleDateFormat;import java.util.Calendar;public class FenDate { public static String age(java.util.Date brithday ) throws Exception { if (brithday==null){ return "无"; } Calendar cal = Calendar.getInstance(); cal.setTime(brithday); int year = cal.get(Calendar.YEAR); Calendar cal2 = Calendar.getInstance(); cal2.setTime(new java.util.Date()); int year2 = cal2.get(Calendar.YEAR); year2=year2-year;return String.valueOf(year2); } public static String ageex(String brithday ) throws Exception { SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");//小写的mm表示的是分钟 java.util.Date date=sdf.parse(brithday); return age(date); } }
2、创建标签库描述文件(Tag Library Descrptor File)
FenDate.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/webjsptaglibrary_2_0.xsd" version="2.0"> <description>functions library by feng, 2012-08-11</description> <display-name>functions of feng</display-name> <tlib-version>1.0</tlib-version> <short-name>fen</short-name> <uri>http://www.kingmed.com.cn</uri> <function> <name>age</name> <function-class>com.kingmed.jusmartcare.health.service.FenDate</function-class> <function-signature>java.lang.String age(java.util.Date)</function-signature> </function> <function> <name>ageex</name> <function-class>com.kingmed.jusmartcare.health.service.FenDate</function-class> <function-signature>java.lang.String ageex(java.lang.String )</function-signature> </function> </taglib>
3:在JSP文件中引人标签库
test.jsp
<%@ page contentType="text/html;charset=UTF-8"%>
<%@ taglib prefix="fen" uri="http://www.kingmed.com.cn"%>
<html xmlns="http://www.w3.org/1999/xhtml">
<body>
<tr><p>${fen:age(null) }</p></tr>
<tr><p>${fen:ageex("2009-1-1") }</p></tr>
</body>
</html>
4:显示结果
无
4
- JSP自定义标签 函数,实现生日计算年龄
- 根据生日计算年龄
- 根据生日计算年龄
- 根据生日计算年龄
- 根据生日计算年龄
- 根据生日计算年龄
- 根据生日计算年龄
- 计算性别,年龄,生日
- MySQL自定义函数计算年龄
- javascript根据生日计算年龄
- MySQL根据生日计算年龄
- 根据用户生日计算年龄
- MySQL根据生日计算年龄
- Jsp自定义标签实现
- jsp实现自定义标签
- 实现jsp 自定义标签
- 实现jsp自定义标签
- 新手学习:根据生日计算当前年龄
- 横瓜先生深度推测解密百度网页搜索引擎的内部索引结构及分布式架构原理
- 用于X86 体系结构研究的工具
- hdu 2578 dating with grils
- 直接拿来用!最火的Android开源项目(二)
- poj 2923 Relocation (枚举+背包 | 状态压缩+01背包)
- JSP自定义标签 函数,实现生日计算年龄
- 六级之阅读理解
- Android Studio 安装具体步骤
- tcpdump advanced filters
- 12.4
- Android Studio 安装问题总结
- 直接拿来用!最火的Android开源项目(三)(完结篇)
- MySql 之表设计原则
- Error: Cannot retrieve repository metadata (repomd.xml) for repository错误的解决