JSF Validation Error: Value is not valid(值无效)错误解决一

问题提出:

平台:Richfaces,Jsf,Spring,Ejb3.0

  • 页面文件:
Html代码
  1. <h:selectOneListboxsize="1"
  2. value="#{coalDailyBackBean.currentEntity.coalDaily.coalTS}"converter="com.mycompany.CoalTransportStyleConverter">
  3. <f:selectItemsvalue="#{coalDailyBackBean.allTSs}">
  4. </f:selectItems>
  5. </h:selectOneListbox>

coalTS是一个CoalTransportStyle的对象

allTSs是一个CoalTransportStyle的List

  • JSF配置文件faces-config.xml:
Xml代码
  1. <converter>
  2. <converter-id>
  3. com.mycompany.CoalTransportStyleConverter
  4. </converter-id>
  5. <converter-class>com.mycompany
  6. .parameter.plantinfo.web.converter.CoalTransportStyleConverter
  7. </converter-class>
  8. </converter>

CoalTransportStyle.java

Java代码
  1. @Entity
  2. @Table(name="T_CoalTransportStyle")
  3. publicclassCoalTransportStyleimplementsSerializable{
  4. /**
  5. *
  6. */
  7. privatestaticfinallongserialVersionUID=-5090574246490412429L;
  8. privateLongid;
  9. privateStringparaName;
  10. @Id
  11. @GeneratedValue(strategy=GenerationType.AUTO)
  12. publicLonggetId(){
  13. returnid;
  14. }
  15. publicvoidsetId(Longid){
  16. this.id=id;
  17. }
  18. @Column(unique=true,nullable=false)
  19. publicStringgetParaName(){
  20. returnparaName;
  21. }
  22. publicvoidsetParaName(StringparaName){
  23. this.paraName=paraName;
  24. }
  25. }

Java代码
  1. publicclassCoalTransportStyleConverterimplementsConverter{
  2. publicObjectgetAsObject(FacesContextarg0,UIComponentarg1,Stringarg2){
  3. CoalTransportStylestyle=newCoalTransportStyle();
  4. Stringstrs[]=arg2.split(":");
  5. style.setId(newLong(strs[0]));
  6. style.setParaName(strs[1]);
  7. returnstyle;
  8. }
  9. publicStringgetAsString(FacesContextarg0,UIComponentarg1,Objectarg2){
  10. CoalTransportStylestyle=(CoalTransportStyle)arg2;
  11. returnstyle.getId()+":"+style.getParaName();
  12. }
  13. }

定义了一个converter

  • 后台支撑Bean:
    Java代码
    1. /**
    2. *获得所有的运输方式
    3. *
    4. *@returntheallTSs
    5. */
    6. publicList<SelectItem>getAllTSs(){
    7. allTSs=newArrayList<SelectItem>();
    8. List<CoalTransportStyle>list=coalTransportStyleService.queryAll(
    9. "paraName",true);
    10. for(CoalTransportStylestyle:list){
    11. allTSs.add(newSelectItem(style,style.getParaName()));
    12. }
    13. returnallTSs;
    14. }
    Java代码
    1. /**
    2. *@returnthecurrentEntity
    3. */
    4. publicCoalDailyEntitygetCurrentEntity(){
    5. if(getCoalDailyId()!=null){
    6. currentEntity=newCoalDailyEntity();
    7. currentEntity.setPlant(plant);
    8. currentEntity.setT_Date(date);//设置时间
    9. currentEntity.setCoalDaily(coal);
    10. }
    11. returncurrentEntity;
    12. }
    初始页面显示时,h:selectOneListbox显示没有问题,但是当数据提交时,就报了一个错:Validation Error: Value is not valid 上网找了半天也没找到答案,后来自己调试了一下。
  • 当数据提交时,是调用了CoalTransportStyleConverter的getAsObject说明这个对象已经创建起来了。但是为什么还报这个错误呢?
  • 于是我找了Messages_en.properties错误信息文件。
    Java代码
    1. javax.faces.component.UISelectOne.INVALID={0}:ValidationError:Valueisnotvalid
  • 又找到UISelectOne的validateValue方法。
    Java代码
    1. protectedvoidvalidateValue(FacesContextcontext,Objectvalue){
    2. //Skipvalidationifitisnotnecessary
    3. super.validateValue(context,value);
    4. if(!isValid()||(value==null)){
    5. return;
    6. }
    7. //Ensurethatthevaluematchesoneoftheavailableoptions
    8. booleanfound=matchValue(value,newSelectItemsIterator(this));
    9. //Enqueueanerrormessageifaninvalidvaluewasspecified
    10. if(!found){
    11. FacesMessagemessage=
    12. MessageFactory.getMessage(context,INVALID_MESSAGE_ID,
    13. MessageFactory.getLabel(context,this));
    14. context.addMessage(getClientId(context),message);
    15. setValid(false);
    16. }
    17. }
    18. //---------------------------------------------------------PrivateMethods
    19. /**
    20. *<p>Return<code>true</code>ifthespecifiedvaluematchesoneofthe
    21. *availableoptions,performingarecursivesearchififa
    22. *{@linkSelectItemGroup}instanceisdetected.</p>
    23. *
    24. *@paramvalue{@linkUIComponent}valuetobetested
    25. *@paramitemsIteratoroverthe{@linkSelectItem}stobechecked
    26. */
    27. privatebooleanmatchValue(Objectvalue,Iteratoritems){
    28. while(items.hasNext()){
    29. SelectItemitem=(SelectItem)items.next();
    30. if(iteminstanceofSelectItemGroup){
    31. SelectItemsubitems[]=
    32. ((SelectItemGroup)item).getSelectItems();
    33. if((subitems!=null)&&(subitems.length>0)){
    34. if(matchValue(value,newArrayIterator(subitems))){
    35. return(true);
    36. }
    37. }
    38. }else{
    39. //Coercetheitemvaluetypebeforecomparingvalues.
    40. Classtype=value.getClass();
    41. ObjectnewValue;
    42. try{
    43. newValue=getFacesContext().getApplication().
    44. getExpressionFactory().coerceToType(item.getValue(),type);
    45. }catch(ELExceptionele){
    46. newValue=item.getValue();
    47. }catch(IllegalArgumentExceptioniae){
    48. //IfcoerceToTypefails,perthedocsitshouldthrow
    49. //anELException,however,GF9.0and9.0u1willthrow
    50. //anIllegalArgumentExceptioninstead(seeGFissue1527).
    51. newValue=item.getValue();
    52. }
    53. if(value.<SPANstyle="COLOR:#ff0000">equals</SPAN>
    54. (newValue)){
    55. return(true);
    56. }
    57. }
    58. }
    59. return(false);
    60. }
    这里调用了equals方法,结果是不行,所以抛出了这个异常。
  • 重写CoalTransportStype的equals方法即可:
    Java代码
    1. @Entity
    2. @Table(name="T_CoalTransportStyle")
    3. publicclassCoalTransportStyleimplementsSerializable{
    4. /**
    5. *
    6. */
    7. privatestaticfinallongserialVersionUID=-5090574246490412429L;
    8. privateLongid;
    9. privateStringparaName;
    10. @Id
    11. @GeneratedValue(strategy=GenerationType.AUTO)
    12. publicLonggetId(){
    13. returnid;
    14. }
    15. publicvoidsetId(Longid){
    16. this.id=id;
    17. }
    18. @Column(unique=true,nullable=false)
    19. publicStringgetParaName(){
    20. returnparaName;
    21. }
    22. publicvoidsetParaName(StringparaName){
    23. this.paraName=paraName;
    24. }
    25. publicbooleanequals(Objectobj){
    26. if(this==obj)
    27. returntrue;
    28. if(obj==null)
    29. returnfalse;
    30. if(getClass()!=obj.getClass())
    31. returnfalse;
    32. finalCoalTransportStyleother=(CoalTransportStyle)obj;
    33. if(id==null){
    34. if(other.id!=null)
    35. returnfalse;
    36. }elseif(!id.equals(other.id))
    37. returnfalse;
    38. if(paraName==null){
    39. if(other.paraName!=null)
    40. returnfalse;
    41. }elseif(!paraName.equals(other.paraName))
    42. returnfalse;
    43. returntrue;
    44. }
    45. }

你可能感兴趣的:(validation)