View Javadoc

1   //========================================================================
2   //Copyright 2011-2012 Mort Bay Consulting Pty. Ltd.
3   //------------------------------------------------------------------------
4   //All rights reserved. This program and the accompanying materials
5   //are made available under the terms of the Eclipse Public License v1.0
6   //and Apache License v2.0 which accompanies this distribution.
7   //The Eclipse Public License is available at
8   //http://www.eclipse.org/legal/epl-v10.html
9   //The Apache License v2.0 is available at
10  //http://www.opensource.org/licenses/apache2.0.php
11  //You may elect to redistribute this code under either of these licenses.
12  //========================================================================
13  
14  package com.acme;
15  
16  import java.io.IOException;
17  import java.text.SimpleDateFormat;
18  import java.util.Date;
19  import java.util.StringTokenizer;
20  
21  import javax.servlet.jsp.JspContext;
22  import javax.servlet.jsp.JspException;
23  import javax.servlet.jsp.tagext.JspFragment;
24  import javax.servlet.jsp.tagext.SimpleTagSupport;
25  
26  public class Date2Tag extends SimpleTagSupport
27  {
28      String format;
29      
30      public void setFormat(String value) {
31          this.format = value;
32      }
33  
34      public void doTag() throws JspException, IOException {
35          String formatted = 
36              new SimpleDateFormat("long".equals(format)?"EEE 'the' d:MMM:yyyy":"d:MM:yy")
37              .format(new Date());
38          StringTokenizer tok = new StringTokenizer(formatted,":");
39          JspContext context = getJspContext();
40          context.setAttribute("day", tok.nextToken() );
41          context.setAttribute("month", tok.nextToken() );
42          context.setAttribute("year", tok.nextToken() );
43  
44          JspFragment fragment = getJspBody();
45          fragment.invoke(null);
46      }
47  }
48