Why is @FacesConverter(forClass=String.class) not

2019-08-09 17:31发布

I followed the example here: Why does <h:inputText required="true"> allow blank spaces? to create a "Global" converter to trim all input fields. However, the converter is not being invoked when input fields are submitted.

@FacesConverter(forClass=String.class) 
... 
<p:inputText value="#{controller.inputValue}"/> 

but when I change to:

@FacesConverter("StringTrimmer") 
... 
<p:inputText value="#{controller.inputValue}" converter="StringTrimmer"/> 

it works.

Using Mojarra 2.1.7 and PrimeFaces 3.2

标签: jsf-2
3条回答
男人必须洒脱
2楼-- · 2019-08-09 17:35

This didnt work because the inputValue was not actually of type String. Once changed to type String-- it worked.

查看更多
虎瘦雄心在
3楼-- · 2019-08-09 17:37

If you checked that the bound variable is of type String and the converter still doesn't get called, you may also check the following:

  • If the input component is encapsulated inside a composite component, you may have this issue. In that case, converters would not be called correctly, resulting in your custom method to be never reached. Calling the converter explicitly on the input component solves this.

  • If you add both value="someName" and forClass="someClass" to the @FacesConverter annotation, the forClass attribute will be ignored. This has been reported here.

查看更多
再贱就再见
4楼-- · 2019-08-09 17:45

A converter with a forClass will only be invoked whenever the type of the property is an instance of the specified class. In your particular case, that can only mean that the #{controller.inputValue} is not of type String.

查看更多
登录 后发表回答