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

我只是一个虾纸丫 提交于 2019-12-11 12:07:57

问题


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


回答1:


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.




回答2:


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.




回答3:


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



来源:https://stackoverflow.com/questions/10214121/why-is-facesconverterforclass-string-class-not-working

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!