Is a custom JSF converter needed for this simple class?

后端 未结 1 475
庸人自扰
庸人自扰 2021-01-25 05:25

I have been trying with limited success to code a JSF application. In one section of the application, I need users to select from a select menu which displays a list of selectab

1条回答
  •  孤城傲影
    2021-01-25 05:39

    I am trying to find out if I need a converter here at all, and if so, how best to implement the converter.

    You need a converter whenever you want to pass non-standard Java Objects from a HTTP request to another HTTP request. With non-standard I mean not a String, Number or Boolean. This all simply because HTTP request parameters can only be Strings. That Number and Boolean works is because EL can recognize them and has built-in coercions for it.

    For non-standard Java Objects you need to implement a javax.faces.convert.Converter which converts the Object to a String (or a Number so you want, for example a Long id which can be the PK of the associated row in database table) inside the getAsString() method before displaying in HTML. You do the other way round in the getAsObject() method during processing of the request parameters (e.g. get the associated object from DAO by its id).

    You can find here an example of how to use a Converter for a h:selectOneMenu. You see that this article also contains an alternative, but you'll need to do a bit more work in the backing bean to convert (map) the objects yourself.

    0 讨论(0)
提交回复
热议问题