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

问题提出:

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

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

	value="#{coalDailyBackBean.currentEntity.coalDaily.coalTS}" converter="com.mycompany.CoalTransportStyleConverter">

	<f:selectItems value="#{coalDailyBackBean.allTSs}">

	</f:selectItems>

</h:selectOneListbox>

    coalTS是一个CoalTransportStyle的对象

    allTSs是一个CoalTransportStyle的List

 

  •  JSF配置文件faces-config.xml:
Xml代码 复制代码
  1. <converter>  
  2.   
  3.     <converter-id>  
  4.   
  5.         com.mycompany.CoalTransportStyleConverter   
  6.   
  7.     </converter-id>  
  8.   
  9.     <converter-class>     com.mycompany   
  10.   
  11. .parameter.plantinfo.web.converter.CoalTransportStyleConverter   
  12.   
  13.     </converter-class>  
  14.   
  15. </converter>  

    CoalTransportStyle.java

Java代码 复制代码
  1. @Entity  
  2.   
  3. @Table(name = "T_CoalTransportStyle")   
  4.   
  5. public class CoalTransportStyle implements Serializable {   
  6.   
  7.   
  8.   
  9.     /**  
  10.  
  11.      *   
  12.  
  13.      */  
  14.   
  15.     private static final long serialVersionUID = -5090574246490412429L;   
  16.   
  17.     private Long id;       
  18.   
  19.     private String paraName;   
  20.   
  21.     @Id  
  22.   
  23.     @GeneratedValue(strategy=GenerationType.AUTO)   
  24.   
  25.     public Long getId() {   
  26.   
  27.         return id;   
  28.   
  29.     }   
  30.   
  31.     public void setId(Long id) {   
  32.   
  33.         this.id = id;   
  34.   
  35.     }   
  36.   
  37.     @Column(unique=true,nullable=false)   
  38.   
  39.     public String getParaName() {   
  40.   
  41.         return paraName;   
  42.   
  43.     }   
  44.   
  45.     public void setParaName(String paraName) {   
  46.   
  47.         this.paraName = paraName;   
  48.   
  49.     }   
  50.   
  51. }  
@Entity

@Table(name = "T_CoalTransportStyle")

public class CoalTransportStyle implements Serializable {



	/**

	 * 

	 */

	private static final long serialVersionUID = -5090574246490412429L;

	private Long id;	

	private String paraName;

	@Id

	@GeneratedValue(strategy=GenerationType.AUTO)

	public Long getId() {

		return id;

	}

	public void setId(Long id) {

		this.id = id;

	}

	@Column(unique=true,nullable=false)

	public String getParaName() {

		return paraName;

	}

	public void setParaName(String paraName) {

		this.paraName = paraName;

	}

}

 

Java代码 复制代码
  1. public class CoalTransportStyleConverter implements Converter {   
  2.   
  3.     public Object getAsObject(FacesContext arg0, UIComponent arg1, String arg2) {   
  4.   
  5.         CoalTransportStyle style = new CoalTransportStyle();   
  6.   
  7.         String strs[] = arg2.split(":");   
  8.   
  9.         style.setId(new Long(strs[0]));   
  10.   
  11.         style.setParaName(strs[1]);   
  12.   
  13.         return style;   
  14.   
  15.     }   
  16.   
  17.   
  18.   
  19.     public String getAsString(FacesContext arg0, UIComponent arg1, Object arg2) {   
  20.   
  21.         CoalTransportStyle style = (CoalTransportStyle) arg2;   
  22.   
  23.         return style.getId() + ":" + style.getParaName();   
  24.   
  25.     }   
  26.   
  27. }  
public class CoalTransportStyleConverter implements Converter {

	public Object getAsObject(FacesContext arg0, UIComponent arg1, String arg2) {

		CoalTransportStyle style = new CoalTransportStyle();

		String strs[] = arg2.split(":");

		style.setId(new Long(strs[0]));

		style.setParaName(strs[1]);

		return style;

	}



	public String getAsString(FacesContext arg0, UIComponent arg1, Object arg2) {

		CoalTransportStyle style = (CoalTransportStyle) arg2;

		return style.getId() + ":" + style.getParaName();

	}

}

 

   定义了一个converter

  • 后台支撑Bean:
    Java代码 复制代码
    1. /**  
    2.  
    3.  * 获得所有的运输方式  
    4.  
    5.  *   
    6.  
    7.  * @return the allTSs  
    8.  
    9.  */  
    10.   
    11. public List<SelectItem> getAllTSs() {   
    12.   
    13.     allTSs = new ArrayList<SelectItem>();   
    14.   
    15.     List<CoalTransportStyle> list = coalTransportStyleService.queryAll(   
    16.   
    17.             "paraName"true);   
    18.   
    19.     for (CoalTransportStyle style : list) {   
    20.   
    21.         allTSs.add(new SelectItem(style, style.getParaName()));   
    22.   
    23.     }   
    24.   
    25.     return allTSs;   
    26.   
    27. }  
    /**
    
     * 获得所有的运输方式
    
     * 
    
     * @return the allTSs
    
     */
    
    public List<SelectItem> getAllTSs() {
    
    	allTSs = new ArrayList<SelectItem>();
    
    	List<CoalTransportStyle> list = coalTransportStyleService.queryAll(
    
    			"paraName", true);
    
    	for (CoalTransportStyle style : list) {
    
    		allTSs.add(new SelectItem(style, style.getParaName()));
    
    	}
    
    	return allTSs;
    
    }
     
    Java代码 复制代码
    1. /**  
    2.  
    3.  * @return the currentEntity  
    4.  
    5.  */  
    6.   
    7. public CoalDailyEntity getCurrentEntity() {   
    8.   
    9.     if (getCoalDailyId() != null) {   
    10.   
    11.         currentEntity = new CoalDailyEntity();   
    12.   
    13.         currentEntity.setPlant(plant);   
    14.   
    15.         currentEntity.setT_Date(date);// 设置时间   
    16.   
    17.         currentEntity.setCoalDaily(coal);   
    18.   
    19.     }   
    20.   
    21.     return currentEntity;   
    22.   
    23. }  
    /**
    
     * @return the currentEntity
    
     */
    
    public CoalDailyEntity getCurrentEntity() {
    
    	if (getCoalDailyId() != null) {
    
    		currentEntity = new CoalDailyEntity();
    
    		currentEntity.setPlant(plant);
    
    		currentEntity.setT_Date(date);// 设置时间
    
    		currentEntity.setCoalDaily(coal);
    
    	}
    
    	return currentEntity;
    
    }
     初始页面显示时,h:selectOneListbox显示没有问题,但是当数据提交时,就报了一个错:Validation Error: Value is not valid 上网找了半天也没找到答案,后来自己调试了一下。
  • 当数据提交时,是调用了CoalTransportStyleConverter的getAsObject说明这个对象已经创建起来了。但是为什么还报这个错误呢?
  • 于是我找了Messages_en.properties错误信息文件。
    Java代码 复制代码
    1. javax.faces.component.UISelectOne.INVALID={0}: Validation Error: Value is not valid  
    javax.faces.component.UISelectOne.INVALID={0}: Validation Error: Value is not valid
  • 又找到UISelectOne的validateValue方法。
    Java代码 复制代码
    1. protected void validateValue(FacesContext context, Object value) {   
    2.   
    3.   
    4.   
    5.         // Skip validation if it is not necessary   
    6.   
    7.         super.validateValue(context, value);   
    8.   
    9.   
    10.   
    11.         if (!isValid() || (value == null)) {   
    12.   
    13.             return;   
    14.   
    15.         }   
    16.   
    17.   
    18.   
    19.         // Ensure that the value matches one of the available options   
    20.   
    21.         boolean found = matchValue(valuenew SelectItemsIterator(this));   
    22.   
    23.   
    24.   
    25.         // Enqueue an error message if an invalid value was specified   
    26.   
    27.         if (!found) {   
    28.   
    29.             FacesMessage message =   
    30.   
    31.                 MessageFactory.getMessage(context, INVALID_MESSAGE_ID,   
    32.   
    33.                      MessageFactory.getLabel(context, this));   
    34.   
    35.             context.addMessage(getClientId(context), message);   
    36.   
    37.             setValid(false);   
    38.   
    39.         }   
    40.   
    41.     }   
    42.   
    43.   
    44.   
    45.   
    46.   
    47.     // --------------------------------------------------------- Private Methods   
    48.   
    49.   
    50.   
    51.   
    52.   
    53.     /**  
    54.  
    55.      * <p>Return <code>true</code> if the specified value matches one of the  
    56.  
    57.      * available options, performing a recursive search if if a  
    58.  
    59.      * {@link SelectItemGroup} instance is detected.</p>  
    60.  
    61.      *  
    62.  
    63.      * @param value {@link UIComponent} value to be tested  
    64.  
    65.      * @param items Iterator over the {@link SelectItem}s to be checked  
    66.  
    67.      */  
    68.   
    69.     private boolean matchValue(Object value, Iterator items) {   
    70.   
    71.   
    72.   
    73.         while (items.hasNext()) {   
    74.   
    75.             SelectItem item = (SelectItem) items.next();   
    76.   
    77.             if (item instanceof SelectItemGroup) {   
    78.   
    79.                 SelectItem subitems[] =   
    80.   
    81.                     ((SelectItemGroup) item).getSelectItems();   
    82.   
    83.                 if ((subitems != null) && (subitems.length > 0)) {   
    84.   
    85.                     if (matchValue(valuenew ArrayIterator(subitems))) {   
    86.   
    87.                         return (true);   
    88.   
    89.                     }   
    90.   
    91.                 }   
    92.   
    93.             } else {   
    94.   
    95.                 //Coerce the item value type before comparing values.   
    96.   
    97.                 Class type = value.getClass();   
    98.   
    99.                 Object newValue;   
    100.   
    101.                 try {   
    102.   
    103.                     newValue = getFacesContext().getApplication().   
    104.   
    105.                         getExpressionFactory().coerceToType(item.getValue(), type);   
    106.   
    107.                 } catch (ELException ele) {   
    108.   
    109.                     newValue = item.getValue();   
    110.   
    111.                 } catch (IllegalArgumentException iae) {   
    112.   
    113.                     // If coerceToType fails, per the docs it should throw   
    114.   
    115.                     // an ELException, however, GF 9.0 and 9.0u1 will throw   
    116.   
    117.                     // an IllegalArgumentException instead (see GF issue 1527).                       
    118.   
    119.                     newValue = item.getValue();   
    120.   
    121.                 }   
    122.   
    123.                 if (value.<SPAN style="COLOR: #ff0000">equals</SPAN>   
    124. (newValue)) {   
    125.   
    126.                     return (true);   
    127.   
    128.                 }   
    129.   
    130.             }   
    131.   
    132.         }   
    133.   
    134.         return (false);   
    135.   
    136.   
    137.   
    138.     }  
    protected void validateValue(FacesContext context, Object value) {
    
    
    
            // Skip validation if it is not necessary
    
            super.validateValue(context, value);
    
    
    
            if (!isValid() || (value == null)) {
    
                return;
    
            }
    
    
    
            // Ensure that the value matches one of the available options
    
            boolean found = matchValue(value, new SelectItemsIterator(this));
    
    
    
            // Enqueue an error message if an invalid value was specified
    
            if (!found) {
    
                FacesMessage message =
    
                    MessageFactory.getMessage(context, INVALID_MESSAGE_ID,
    
                         MessageFactory.getLabel(context, this));
    
                context.addMessage(getClientId(context), message);
    
                setValid(false);
    
            }
    
        }
    
    
    
    
    
        // --------------------------------------------------------- Private Methods
    
    
    
    
    
        /**
    
         * <p>Return <code>true</code> if the specified value matches one of the
    
         * available options, performing a recursive search if if a
    
         * {@link SelectItemGroup} instance is detected.</p>
    
         *
    
         * @param value {@link UIComponent} value to be tested
    
         * @param items Iterator over the {@link SelectItem}s to be checked
    
         */
    
        private boolean matchValue(Object value, Iterator items) {
    
    
    
            while (items.hasNext()) {
    
                SelectItem item = (SelectItem) items.next();
    
                if (item instanceof SelectItemGroup) {
    
                    SelectItem subitems[] =
    
                        ((SelectItemGroup) item).getSelectItems();
    
                    if ((subitems != null) && (subitems.length > 0)) {
    
                        if (matchValue(value, new ArrayIterator(subitems))) {
    
                            return (true);
    
                        }
    
                    }
    
                } else {
    
                    //Coerce the item value type before comparing values.
    
                    Class type = value.getClass();
    
                    Object newValue;
    
                    try {
    
                        newValue = getFacesContext().getApplication().
    
                            getExpressionFactory().coerceToType(item.getValue(), type);
    
                    } catch (ELException ele) {
    
                        newValue = item.getValue();
    
                    } catch (IllegalArgumentException iae) {
    
                        // If coerceToType fails, per the docs it should throw
    
                        // an ELException, however, GF 9.0 and 9.0u1 will throw
    
                        // an IllegalArgumentException instead (see GF issue 1527).                    
    
                        newValue = item.getValue();
    
                    }
    
                    if (value.equals
    (newValue)) {
    
                        return (true);
    
                    }
    
                }
    
            }
    
            return (false);
    
    
    
        }
     这里调用了equals方法,结果是不行,所以抛出了这个异常。
  • 重写CoalTransportStype的equals方法即可:
    Java代码 复制代码
    1. @Entity  
    2.   
    3. @Table(name = "T_CoalTransportStyle")   
    4.   
    5. public class CoalTransportStyle implements Serializable {   
    6.   
    7.   
    8.   
    9.     /**  
    10.  
    11.      *   
    12.  
    13.      */  
    14.   
    15.     private static final long serialVersionUID = -5090574246490412429L;   
    16.   
    17.     private Long id;       
    18.   
    19.     private String paraName;   
    20.   
    21.     @Id  
    22.   
    23.     @GeneratedValue(strategy=GenerationType.AUTO)   
    24.   
    25.     public Long getId() {   
    26.   
    27.         return id;   
    28.   
    29.     }   
    30.   
    31.     public void setId(Long id) {   
    32.   
    33.         this.id = id;   
    34.   
    35.     }   
    36.   
    37.     @Column(unique=true,nullable=false)   
    38.   
    39.     public String getParaName() {   
    40.   
    41.         return paraName;   
    42.   
    43.     }   
    44.   
    45.     public void setParaName(String paraName) {   
    46.   
    47.         this.paraName = paraName;   
    48.   
    49.     }   
    50.   
    51.        
    52.   
    53.     public boolean equals(Object obj) {   
    54.   
    55.         if (this == obj)   
    56.   
    57.             return true;   
    58.   
    59.         if (obj == null)   
    60.   
    61.             return false;   
    62.   
    63.         if (getClass() != obj.getClass())   
    64.   
    65.             return false;   
    66.   
    67.         final CoalTransportStyle other = (CoalTransportStyle) obj;   
    68.   
    69.         if (id == null) {   
    70.   
    71.             if (other.id != null)   
    72.   
    73.                 return false;   
    74.   
    75.         } else if (!id.equals(other.id))   
    76.   
    77.             return false;   
    78.   
    79.         if (paraName == null) {   
    80.   
    81.             if (other.paraName != null)   
    82.   
    83.                 return false;   
    84.   
    85.         } else if (!paraName.equals(other.paraName))   
    86.   
    87.             return false;   
    88.   
    89.         return true;   
    90.   
    91.     }   
    92.   
    93. }  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值