Passing a Character (vs passing String) to backing bean method in EL

前端 未结 2 907
隐瞒了意图╮
隐瞒了意图╮ 2021-01-23 02:51

I would like to call a setter directly from a command button and pass a value. My problem is that the setter is expecting a Character and jsf if passing it back as a String. Is

2条回答
  •  孤城傲影
    2021-01-23 03:27

    This is unfortunately by design. Everything in quotes is in EL treated as String. A workaround would be to pass String#charAt() instead.

    #{manageItemHandler.dataEntryOp.setBomComplete('Y'.charAt(0))}
    

    This is only ugly. An alternative is to pass its int codepoint instead, which is 89 for Y.

    #{manageItemHandler.dataEntryOp.setBomComplete(89)}
    

    But this is not exactly self-documenting. Much better is to just make use of enums.

    public enum Choice {
        Y, N;
    }
    

    with

    protected Choice bomComplete;
    

    which you can just invoke the desired way

    #{manageItemHandler.dataEntryOp.setBomComplete('Y')}
    

    The string 'Y' will be automatically converted to that enum. As a bonus, enums have many more additional advantages, such as compile time type safety.

提交回复
热议问题