在JSTL / JSP页面上,我的应用程序中有一个java.util.Date对象。我需要在该对象指定的日期之后找到的那一天。我可以使用< jsp:scriptlet>放入Java并使用java.util.Calendar进行必要的计算,但这对我来说感觉笨拙和不优雅。
On a JSTL/JSP page, I have a java.util.Date object from my application. I need to find the day after the day specified by that object. I can use <jsp:scriptlet> to drop into Java and use java.util.Calendar to do the necessary calculations, but this feels clumsy and inelegant to me.
有没有办法使用JSP或JSTL用标签实现这一目标而不必切换到全面的Java,或者后者是实现这一目标的唯一方法吗?
Is there some way to use JSP or JSTL tags to achieve this end without having to switch into full-on Java, or is the latter the only way to accomplish this?
推荐答案我不喜欢将java代码放入你的jsp。
I'm not a fan of putting java code in your jsp.
我使用静态方法和taglib来实现这一点。
I'd use a static method and a taglib to accomplish this.
尽管我的想法。有很多方法可以解决这个问题。
Just my idea though. There are many ways to solve this problem.
public static Date addDay(Date date){ //TODO you may want to check for a null date and handle it. Calendar cal = Calendar.getInstance(); cal.setTime (date); cal.add (Calendar.DATE, 1); return cal.getTime(); }functions.tld
functions.tld
<?xml version="1.0" encoding="UTF-8" ?> <taglib xmlns="java.sun/xml/ns/j2ee" xmlns:xsi="www.w3/2001/XMLSchema-instance" xsi:schemaLocation="java.sun/xml/ns/j2ee java.sun/xml/ns/j2ee/web-jsptaglibrary_2_0.xsd" version="2.0"> <description>functions library</description> <display-name>functions</display-name> <tlib-version>1.1</tlib-version> <short-name>xfn</short-name> <uri>yourdomain/functions.tld</uri> <function> <description> Adds 1 day to a date. </description> <name>addDay</name> <function-class>Functions</function-class> <function-signature>java.util.Date addDay(java.util.Date)</function-signature> <example> ${xfn:addDay(date)} </example> </function> </taglib>更多推荐
在JSTL / JSP中,给定java.util.Date,我如何找到第二天?
发布评论