3

I'm stuck with JSF RI 1.1_02 and seeing this problem.

Here's the Facelet code I'm expecting to work:

<h:form>
    <h:selectOneMenu value="#{bean.num}" converter="javax.faces.Integer">
        <f:selectItem itemLabel="one"   itemValue="1" />
        <f:selectItem itemLabel="two"   itemValue="2" />
        <f:selectItem itemLabel="three" itemValue="3" />
    </h:selectOneMenu>
    <h:commandButton value="submit" />
    <h:messages />
</h:form>

The request scoped bean:

public class Bean {
    private int num;

    public void setNum(Integer aNum) {
        num = aNum;
    }

    public Integer getNum() {
        return num;
    }
}

I'm receiving Validation Error: Value is not valid and can't imagine what I'm doing wrong already when I've written so little code which shouldn't need any converter. Am I missing something obvious or is this a bug in JSF RI 1.1_02?

I can get around the problem by simply changing the property type in the backing bean to String but am just frustrated by having to do that when (automatic) conversion is supposed to be there for me.

Community
  • 1
  • 1
billy
  • 1,435
  • 1
  • 11
  • 11
  • 2
    Just wondering, why still JSF 1.1? It has been replaced by JSF 1.2 over 6.5 years ago which has in turn been replaced by JSF 2.0 over 2.5 years ago. Note that a JSF 1.1 compatible environment should theoretically also support JSF 1.2 without any much hassle. – BalusC Jul 30 '12 at 17:35
  • large ships don't turn easily, i.e., the effort to migrate is too expensive right now...I guess this is the tradeoff in large enterprises for heavy change control. – billy Jul 30 '12 at 19:10
  • 1
    I'm however surprised to see that you're using Facelets. This was introduced 5.5 years ago (during the JSF 1.2 period). It wouldn't have made sense to start off with JSF 1.1 instead of 1.2. Migrating JSP to Facelets is harder than migrating JSF 1.1 to 1.2. – BalusC Jul 30 '12 at 19:26
  • @billy Your company might as well have written the application in FoxPro because JSF 1.1 is an abomination. – maple_shaft Jul 30 '12 at 19:39
  • The infinite wisdom of our architecture office throwing designs down from the ivory tower. – billy Jul 31 '12 at 00:01

1 Answers1

7

I took the time to create a JSF RI 1.1_02 playground environment and I was able to reproduce your problem. After checking the source code the culprit seems that the <f:selectItem> value is never converted to the same type as the submitted value. So basically it is comparing the item value as String against the submitted value as Integer and this comparison ain't ever going to return true.

This is a pretty awkward bug which can technically only be solved by replacing the UISelectOne component (the UISelectMany exposes the same bug by the way). The problem is in the private matchValue() method. A custom converter (a solution which I initially had in mind) isn't going to help in any way as it wouldn't be invoked for the <f:selectItem> value at all.

Upgrading to Mojarra 1.2_15 instantly fixed the problem.


Update: in case you really, really, can't upgrade, I found a workaround utilizing EL coercion: if you reference the values in EL instead of as static strings, then they're implicitly treated as Long. If you change the property type from Integer to Long, then it'll work without any converter.

<h:selectOneMenu value="#{bean.num}">
    <f:selectItem itemLabel="one"   itemValue="#{1}" />
    <f:selectItem itemLabel="two"   itemValue="#{2}" />
    <f:selectItem itemLabel="three" itemValue="#{3}" />
</h:selectOneMenu>

with

private Long num;
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • @Daniel: the bug or my effort? The bug indeed made me speechless. Reminds me how and why JSF was hated in the early years. – BalusC Jul 30 '12 at 20:55
  • Your effort, finding out this bug (which seems quite elusive) :) – Daniel Jul 30 '12 at 20:58
  • Agreed, @BalusC you really are an exceptional contributor, thanks for taking the time! – billy Jul 30 '12 at 23:59
  • @BalusC, I'm also curious, I've been looking everywhere for the source code and have only found one location that has the impl's source code but it doesn't line up in debug mode so I don't think it's the same build. Would you mind sharing where you got the impl's source for 1.1_02? I'd be happy to create a separate question for this if that would be more appropriate. – billy Aug 01 '12 at 11:15