سؤال

I am trying to create a custom jsf2 component in order to avoid tyiping the converter and message tags for my date fields. I've done this with Icefaces 1.x using templates. With JSF 2, though, I was forced to use composite components; that is not going as well as I hoped.

The composite component has been successfully created:

<composite:interface>
    <composite:attribute name="style" />
    <composite:attribute name="partialSubmit" />
    <composite:attribute name="rendered" />
    <composite:attribute name="immediate" />
    <composite:attribute name="value" required="true" />
    <composite:attribute name="pattern" required="true" />
</composite:interface>

<composite:implementation>
    <ice:panelGroup rendered="#{cc.attrs.rendered}">
        <ice:selectInputDate value="#{cc.attrs.value}"
            style="#{cc.attrs.style}" id="input" renderAsPopup="true"
            partialSubmit="#{cc.attrs.partialSubmit }"
            immediate="#{cc.attrs.immediate }">
        </ice:selectInputDate>
        <ice:message for="input" style="color: red; display: block"></ice:message>
    </ice:panelGroup>
</composite:implementation>

When the property is null, the component behaves as expected. However, when I load data from my database, I get a java.sql.Date instance (despite the fact that my object uses java.util.date - the former extends the latter), I get this exception:

java.lang.IllegalArgumentException: Cannot convert 03/03/11 20:00 of type class java.util.Date to class java.sql.Date

What is weird is that if I use an ice:selectInputDate outside my composite component, I don't get that error. Any ideas?

هل كانت مفيدة؟

المحلول

Alright, this might seem a little too extreme of a work around: rebuilding Tomcat. I figured tomcat doesn't change as often as JSF or ICEFaces. Call it a hack if you must; it solves my problem...

Download the tomcat source for the version you use and find a file named ELSupport.java. Look for the method with this signature:

public static final Object coerceToType(final Object obj,
        final Class<?> type) throws ELException {

Now add the following lines to it, right above the line that throws the ELException:

if (java.util.Date.class.isAssignableFrom(obj.getClass())){
    return obj;
}

It pretty much just prevents the exception from being thrown when the object is an instance of a sub-class from java.util.Date.

Does anyone think this is a contribution to the Tomcat project? I never really contributed with anything open source, so I don't really know how it works.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top