2015-02-14 19 views
5

Non sono stato in grado di capire come visualizzare un valore java.time.LocalDate in un JSP. Nel mio JSP, ho questo:JSTL formatDate e java.time.LocalDate

<fmt:formatDate value="${std.datum}" type="date" pattern="dd.MM.yyyy" var="stdDatum" /> 

std.datum è di tipo java.time.LocalDate. Quando eseguo il jsp, ottengo questa eccezione:

javax.el.ELException: Cannot convert 2015-02-14 of type class java.time.LocalDate to class java.util.Date 

Suppongo che sia la conversione?

Grazie, john.

+1

Secondo il messaggio 'Can not conv ert 2015-02-14 di tipo classe java.time.LocalDate alla classe java.util.Date', il tag JSTL '' non supporta 'java.time.LocalDate' che è introdotto in Java 8. – Tiny

+0

http://www.tech.theplayhub.com/jstl_formatdate_and_java-time-localdate/ –

risposta

5

Sì. È possibile utilizzare fmt: parseDate per eseguire la conversione e quindi eseguire la formattazione. Esempio sotto

<fmt:parseDate value="${std.datum}" pattern="yyyy-MM-dd" 
          var="parsedDate" type="date" /> 

<fmt:formatDate value="${parsedDate}" var="stdDatum" 
          type="date" pattern="dd.MM.yyyy" /> 

Buona fortuna.

1

Questa è una vecchia domanda, ma trovo che sia molto meglio fare un tld personalizzato in questo caso: senza alcuna doppia conversione da e verso String.

Eseguire il proprio file tld, quindi eseguire l'override della classe FormatDate. Infine, dichiara il tuo prefisso personalizzato e usa personalizzato: formatDate invece di fmt: formatDate.

qui è una versione semplificata

utilizzo in JSP:

<%@ taglib uri="/WEB-INF/custom" prefix="custom" %> 
... 
<custom:formatDate value="${std.datum}" pattern="dd/MM/yyyy" /> 

di file WEB-INF/custom.tld

<?xml version="1.0" encoding="UTF-8"?> 
<tag ib 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"> 

<tag> 
    <description> 
     FormatDate with java8 type 
    </description> 
    <name>formatDate</name> 
    <tag-class>com.custom.tag.FormatDateTag</tag-class> 
    <body-content>empty</body-content> 
    <attribute> 
     <description> 
      Date and/or time to be formatted. 
     </description> 
     <name>value</name> 
     <required>true</required> 
     <rtexprvalue>true</rtexprvalue> 
    </attribute> 

    <attribute> 
     <description> 
      Custom formatting style for dates and times. 
     </description> 
     <name>pattern</name> 
     <required>false</required> 
     <rtexprvalue>true</rtexprvalue> 
    </attribute> 
</tag> 
</taglib> 

Poi il file di tag classe java

public class FormatDateTag extends TagSupport { 

    protected Temporal value; 
    protected String pattern; 
    private String var; 
    private int scope; 


    public FormatDateTag() 
    { 
     super(); 
     init(); 
    } 

    private void init() 
    { 

     this.pattern = this.var = null; 
     this.value = null; 
     this.scope = PageContext.PAGE_SCOPE; 
    } 


    public void setVar(final String var) 
    { 
     this.var = var; 
    } 

    public void setScope(final String scope) 
    { 
     this.scope = Util.getScope (scope); 
    } 


    public void setValue(final Temporal value) 
    { 
     this.value = value; 
    } 


    public void setPattern(final String pattern) 
    { 
     this.pattern = pattern; 
    } 


    @Override 
    public int doEndTag() throws JspException 
    { 

     String formatted = null; 

     if (this.value == null) 
     { 
      if (this.var != null) 
      { 
       this.pageContext.removeAttribute (this.var, this.scope); 
      } 
      return EVAL_PAGE; 
     } 

     // Create formatter 
     if (this.pattern != null) 
     { 
      final DateTimeFormatter formatter = DateTimeFormatter.ofPattern (this.pattern); 
      formatted = formatter.format (this.value); 
     } 
     else 
     { 
      // no formatting locale available, use Date.toString() 
      formatted = this.value.toString(); 
     } 

     if (this.var != null) 
     { 
      this.pageContext.setAttribute (this.var, formatted, this.scope); 
     } 
     else 
     { 
      try 
      { 
       this.pageContext.getOut().print (formatted); 
      } 
      catch (final IOException ioe) 
      { 
       throw new JspTagException (ioe.toString(), ioe); 
      } 
     } 

     return EVAL_PAGE; 
    } 


    @Override 
    public void release() 
    { 
     init(); 
    } 

}