JSF Validation Error: Value is not valid错误
2013-04-10 22:54
337 查看
问题提出:
平台:Richfaces,Jsf,Spring,Ejb3.0
页面文件:
Html代码
<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代码
<converter>
<converter-id>
com.mycompany.CoalTransportStyleConverter
</converter-id>
<converter-class> com.mycompany
.parameter.plantinfo.web.converter.CoalTransportStyleConverter
</converter-class>
</converter>
CoalTransportStyle.java
Java代码
@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代码
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代码
/**
* 获得所有的运输方式
*
* @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代码
/**
* @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代码
javax.faces.component.UISelectOne.INVALID={0}: Validation Error: Value is not valid
又找到UISelectOne的validateValue方法。
Java代码
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.<span style="color: #ff0000;">equals</span>
(newValue)) {
return (true);
}
}
}
return (false);
}
这里调用了equals方法,结果是不行,所以抛出了这个异常。
重写CoalTransportStype的equals方法即可:
Java代码
@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;
}
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
final CoalTransportStyle other = (CoalTransportStyle) obj;
if (id == null) {
if (other.id != null)
return false;
} else if (!id.equals(other.id))
return false;
if (paraName == null) {
if (other.paraName != null)
return false;
} else if (!paraName.equals(other.paraName))
return false;
return true;
}
}
平台:Richfaces,Jsf,Spring,Ejb3.0
页面文件:
Html代码
<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代码
<converter>
<converter-id>
com.mycompany.CoalTransportStyleConverter
</converter-id>
<converter-class> com.mycompany
.parameter.plantinfo.web.converter.CoalTransportStyleConverter
</converter-class>
</converter>
CoalTransportStyle.java
Java代码
@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代码
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代码
/**
* 获得所有的运输方式
*
* @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代码
/**
* @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代码
javax.faces.component.UISelectOne.INVALID={0}: Validation Error: Value is not valid
又找到UISelectOne的validateValue方法。
Java代码
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.<span style="color: #ff0000;">equals</span>
(newValue)) {
return (true);
}
}
}
return (false);
}
这里调用了equals方法,结果是不行,所以抛出了这个异常。
重写CoalTransportStype的equals方法即可:
Java代码
@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;
}
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
final CoalTransportStyle other = (CoalTransportStyle) obj;
if (id == null) {
if (other.id != null)
return false;
} else if (!id.equals(other.id))
return false;
if (paraName == null) {
if (other.paraName != null)
return false;
} else if (!paraName.equals(other.paraName))
return false;
return true;
}
}
相关文章推荐
- JSF Validation Error: Value is not valid(值无效)JSF值转换无效的两个解决办法
- WPF 错误:DependencyProperty.UnsetValue}' is not a valid value for property 'Background'
- DependencyProperty.UnsetValue}' is not a valid value for property 'Background' 的错误原因
- Scala编程中常见错误:Error:(24, 29) value foreach is not a member of java.util.Set[String]
- Scala中类型不对错误(error: value % is not a member of scala.collection.immutable.Range.Inclusive)
- ERROR spi.SqlExceptionHelper : 'Infinity' is not a valid numeric or approximate numeric value 问题解决
- iOS 错误"This class is not key value coding-compliant for the key"解决方案
- python错误:TypeError: 'str' object is not callable
- 'Infinity' is not a valid numeric or approximate numeric value 异常问题
- SOAP-ERROR: Encoding: string … is not a valid utf-8 string
- Error parsing reference: "Pxxxxxx" is not a valid repository/tag
- RUNTIME_CLASS(xx)->CreateObject()错误Error: Trying to create object which is not DECLARE_DYNCREATE
- Error: 'S' is not a valid file-based resource name character: File-bas
- ThinkPHP报js错误:Uncaught ReferenceError: out is not defined
- TypeError: s[y] is not a function 表单提交错误
- xib自定义UIView报错误 "forUndefinedKey:]: this class is not key value coding-compliant for the key"
- Error contacting service. It is probably not running 错误的几个常见原因
- JNI DETECTED ERROR IN APPLICATION: input is not valid Modified UTF-8: illegal continuation byte 0x12
- samba getpeername failed. Error was Transport endpoint is not connected错误
- 在安装 Windows 过程中出现“Ntkrnlmp.exe Could Not Be Loaded. The Error Code Is 7”(无法加载 Ntkrnlmp.exe。错误代码是 7)错误消息