we started use jsf 2.3 on our existing jsf 2.2 project. on our custom converters got warning converter raw type. references generic type converter<t> should parameterized.
problem experiencing when tried fix warning using generics:
@facesconverter(value = "myconverter", managed = true) public class myconverter implements converter<mycustomobject>{ @override public mycustomobject getasobject(facescontext context, uicomponent component, string submittedvalue){} @override public string getasstring(facescontext context, uicomponent component, mycustomobject modelvalue) {} }
and when converter used example in
<!doctype html> <ui:composition xmlns="http://www.w3.org/1999/xhtml" xmlns:c="http://xmlns.jcp.org/jsp/jstl/core" xmlns:h="http://java.sun.com/jsf/html" xmlns:f="http://java.sun.com/jsf/core" xmlns:ui="http://java.sun.com/jsf/facelets"> <h:selectonemenu id="#{componentid}" value="#{componentvalue}"> <f:converter converterid="myconverter" /> <f:selectitem itemlabel="label" itemvalue="" /> <f:selectitems value="listofvalues" var="singlevalue" itemvalue="singlevalue.value" itemlabel="singlevalue.label" /> </h:selectonemenu>
classcastexception
message java.lang.string cannot cast mycustomobject
is thrown. there 1 line in stacktrace maybe can com.sun.faces.cdi.cdiconverter.getasstring(cdiconverter.java:109)
.
but when converter generics definition changed mycustomobject
object
:
@facesconverter(value = "myconverter", managed = true) public class myconverter implements converter<object>{ @override public object getasobject(facescontext context, uicomponent component, string submittedvalue){} @override public string getasstring(facescontext context, uicomponent component, object modelvalue) {} }
then works expected, beats purpose of converter<t>
interface.
i had same issue here , came following solution not compiles works:
some_page.xhtml
(relevant excerpt):
<h:selectonemenu styleclass="select" id="companyuserowner" value="#{admincompanydatacontroller.companyuserowner}"> <f:converter converterid="userconverter" /> <f:selectitem itemvalue="#{null}" itemlabel="#{msg.none_selected}" /> <f:selectitems value="#{usercontroller.allusers()}" var="companyuserowner" itemvalue="#{companyuserowner}" itemlabel="#{companyuserowner.usercontact.contactfirstname} #{companyuserowner.usercontact.contactfamilyname} (#{companyuserowner.username})" /> </h:selectonemenu>
please note above jsf code full of custom stuff , may not work on end. , converter compiling without rawtype warning:
someuserconverter.java
:
@facesconverter (value = "userconverter") public class someuserconverter implements converter<user> { /** * user ejb */ private static usersessionbeanremote user_bean; /** * default constructor */ public financialsuserconverter () { } @override public user getasobject (final facescontext context, final uicomponent component, final string submittedvalue) { // value null or empty? if ((null == submittedvalue) || (submittedvalue.trim().isempty())) { // warning message // @todo not working jndi (no remote interface) this.loggerbeanlocal.logwarning(messageformat.format("{0}.getasobject(): submittedvalue null or empty - exit!", this.getclass().getsimplename())); //noi18n // return null return null; } // init instance user user = null; try { // try parse value long long userid = long.valueof(submittedvalue); // try user instance user = user_bean.finduserbyid(userid); } catch (final numberformatexception ex) { // throw again throw new converterexception(ex); } catch (final usernotfoundexception ex) { // debug message // @todo not working jndi (no remote interface) this.loggerbeanlocal.logdebug(messageformat.format("getasobject: exception: {0} - returning null ...", ex)); //noi18n } // return return user; } @override public string getasstring (final facescontext context, final uicomponent component, final user value) { // object null? if ((null == value) || (string.valueof(value).isempty())) { // null return ""; //noi18n } // return id number return string.valueof(value.getuserid()); } }
the converter class has jndi lookup removed (i rewrite part later anyway) should enough demonstrating right parts:
converter<user>
(byuser
custom poji) preventing raw-type warningvalue="userconverter"
actual name use in jsf page- and important, fixed
classcastexception
: <f:selectitem itemvalue="#{null}" itemlabel="#{msg.none_selected}" />
- by omitting
#{null}
empty string instead ofnull
being handled ongetasstring
method!
this last 1 fixed said exception , application working again lesser warnings , better type-hinting.
need remove forclass
value
there: facesconverter using both value , forclass, value applied.
Comments
Post a Comment