rich:挑选名单&;selectManyListbox-JSF转换器验证错误:值无效
我将rich:pickList替换为SelectManyList进行测试,当向服务器提交值时,它仍然显示错误:“验证错误:值无效”。我还将断点设置为调试StaffConverter(getAsobject),但系统从未调用它。请让我知道为什么我的转换器从来没有调用的一些原因,并建议我如何解决这个问题。谢谢 xhtml文件:rich:挑选名单&;selectManyListbox-JSF转换器验证错误:值无效,jsf,richfaces,Jsf,Richfaces,我将rich:pickList替换为SelectManyList进行测试,当向服务器提交值时,它仍然显示错误:“验证错误:值无效”。我还将断点设置为调试StaffConverter(getAsobject),但系统从未调用它。请让我知道为什么我的转换器从来没有调用的一些原因,并建议我如何解决这个问题。谢谢 xhtml文件: <h:selectManyListbox value="#{reportController.selectedStaffList}" converter="staffC
<h:selectManyListbox value="#{reportController.selectedStaffList}" converter="staffConverter">
<f:selectItems value="#{reportController.staffList}"
var="item" itemValue="#{item}" itemLabel="#{item.name}" />
</h:selectManyListbox>
<rich:pickList value="#{reportController.selectedStaffList}" converter="staffConverter" sourceCaption="Available flight" targetCaption="Selected flight" listWidth="195px" listHeight="100px" orderable="true">
<f:selectItems value="#{reportController.staffList}" var="item" itemValue="#{item}" itemLabel="#{item.name}" />
</rich:pickList>
我在员工中实施了equals方法:
@Override
public boolean equals(Object object) {
if (!(object instanceof Staff)) {
return false;
}
Staff other = (Staff) object;
return (this.staffCode == other.staffCode);
}
谢谢布莱恩和巴卢斯的帮助。我通过在selectManyListbox&rich:picklist中添加命名转换器修复了我的问题,这样它们运行良好。但通常情况下,我只使用@FacesConverter(forClass=Staff.class),不需要在jsf中添加命名转换器,所以除了selectManyListbox&rich:picklist之外,它们仍然运行良好,只是为了让像我这样喜欢在google或stackoverflow上搜索开发问题解决方案的人把它发布到某个地方 我有过几次这个问题,这取决于我在转换器中使用的POJO类型。。。最后我想我找到了一个优雅的解决方案。 在我的例子中,我直接使用JPA实体类,因为我想保存DTO层。好吧,对于某些实体,富人:挑选名单起作用,而其他实体则不起作用。。。我还找到了equals方法。例如,在下面的示例中,它不适用于userGroupConverter bean 我的解决方案只是内联覆盖equals方法,因此实体中的方法(我经常使用lombok)是未被触及的,根本不需要更改!因此,在下面的转换器中,我只比较等于中的名称字段: xhtml:
<rich:pickList id="pickListUserGroupSelection"
value="#{usersBean.selectedUserGroups}" switchByDblClick="true"
sourceCaption="Available user groups" targetCaption="Groups assigned to user"
listWidth="365px" listHeight="100px" orderable="false"
converter="#{userGroupConverter}"
disabled="#{!rich:isUserInRole('USERS_MAINTAIN')}">
<f:validateRequired disabled="true" />
<rich:validator disabled="true" />
<f:selectItems value="#{usersBean.userGroups}" var="userGroup"
itemValue="#{userGroup}"
itemLabel="#{userGroup.name}" />
<f:selectItems value="#{usersBean.selectedUserGroups}" var="userGroup"
itemValue="#{userGroup}"
itemLabel="#{userGroup.name}" />
</rich:pickList>
<rich:message for="pickListUserGroupSelection" />
转换器:
@FacesConverter(forClass = Staff.class)
public static class StaffConverter implements Converter {
public Object getAsObject(FacesContext facesContext, UIComponent component, String value) {
if (value == null || value.length() == 0) {
return null;
}
StaffController controller = StaffController.getInstance();
return controller.facade.find(Staff.class, getKey(value));
}
java.lang.Integer getKey(String value) {
java.lang.Integer key;
key = Integer.valueOf(value);
return key;
}
String getStringKey(java.lang.Integer value) {
StringBuffer sb = new StringBuffer();
sb.append(value);
return sb.toString();
}
public String getAsString(FacesContext facesContext, UIComponent component, Object object) {
if (object == null) {
return null;
}
if (object instanceof Staff) {
Staff o = (Staff) object;
return getStringKey(o.getStaffCode());
} else {
throw new IllegalArgumentException("object " + object + " is of type " + object.getClass().getName()
+ "; expected type: " + StaffController.class.getName());
}
}
}
package ...;
import ...
/**
* JSF UserGroup converter.<br>
* Description:<br>
* JSF UserGroup converter for rich:pickList elements.<br>
* <br>
* Copyright: Copyright (c) 2014<br>
*/
@Named
@Slf4j
@RequestScoped // must be request scoped, as it can change every time!
public class UserGroupConverter implements Converter, Serializable {
/**
*
*/
private static final long serialVersionUID = 9057357226886146751L;
@Getter
Map<String, UserGroup> groupMap;
@Inject
MessageUtil messageUtil;
@Inject
UserGroupDao userGroupDao;
@PostConstruct
public void postConstruct() {
groupMap = new HashMap<>();
List<UserGroup> userGroups;
try {
userGroups = userGroupDao.findAll(new String[] {UserGroup.FIELD_USER_ROLE_NAMES});
if(userGroups != null) {
for (UserGroup userGroup : userGroups) {
// 20150713: for some reason the UserGroup entity's equals method is not sufficient here and causes a JSF validation error
// "Validation Error: Value is not valid". I tried this overridden equals method and now it works fine :-)
@SuppressWarnings("serial")
UserGroup newGroup = new UserGroup() {
@Override
public boolean equals(Object obj){
if (!(obj instanceof UserGroup)){
return false;
}
return (getName() != null)
? getName().equals(((UserGroup) obj).getName())
: (obj == this);
}
};
newGroup.setName(userGroup.getName());
groupMap.put(newGroup.getName(), newGroup);
}
}
} catch (DaoException e) {
log.error(e.getMessage(), e);
FacesContext fc = FacesContext.getCurrentInstance();
FacesMessage message = new FacesMessage(FacesMessage.SEVERITY_INFO, "Error initializing user group converter!", null);
fc.addMessage(null, message);
}
}
/*
* (non-Javadoc)
* @see javax.faces.convert.Converter#getAsObject(javax.faces.context.FacesContext, javax.faces.component.UIComponent, java.lang.String)
*/
@Override
public UserGroup getAsObject(FacesContext context, UIComponent component,
String value) {
UserGroup ug = null;
try {
ug = getGroupMap().get(value);
} catch (Exception e) {
log.error(e.getMessage(), e);
FacesContext fc = FacesContext.getCurrentInstance();
FacesMessage message = new FacesMessage(FacesMessage.SEVERITY_INFO, "Error converting user group!", null);
fc.addMessage(null, message);
}
return ug;
}
/*
* (non-Javadoc)
* @see javax.faces.convert.Converter#getAsString(javax.faces.context.FacesContext, javax.faces.component.UIComponent, java.lang.Object)
*/
@Override
public String getAsString(FacesContext context, UIComponent component,
Object value) {
String name = ((UserGroup) value).getName();
return name;
}
}
包。。。;
导入。。。
/**
*JSF用户组转换器。
*说明:
*用于rich:pickList元素的JSF用户组转换器。
*
*版权所有:版权所有(c)2014
*/
@命名
@Slf4j
@RequestScope//必须是RequestScope,因为它可以随时更改!
公共类UserGroupConverter实现转换器,可序列化{
/**
*
*/
私有静态最终长serialVersionUID=9057357226886146751L;
@吸气剂
地图组地图;
@注入
MessageUtil MessageUtil;
@注入
UserGroupDao UserGroupDao;
@施工后
施工后公共空间(){
groupMap=newhashmap();
列出用户组;
试一试{
userGroups=userGroupDao.findAll(新字符串[]{UserGroup.FIELD\u USER\u ROLE\u NAMES});
if(userGroups!=null){
for(用户组用户组:用户组){
//20150713:由于某种原因,用户组实体的equals方法在这里不够用,并导致JSF验证错误
//“验证错误:值无效”。我尝试了这个重写的equals方法,现在它工作正常:-)
@抑制警告(“串行”)
UserGroup newGroup=newusergroup(){
@凌驾
公共布尔等于(对象obj){
if(!(用户组的obj实例)){
返回false;
}
返回(getName()!=null)
?getName().equals(((用户组)obj.getName())
:(obj==这个);
}
};
setName(userGroup.getName());
groupMap.put(newGroup.getName(),newGroup);
}
}
}捕获(DAOE){
log.error(e.getMessage(),e);
FacesContext fc=FacesContext.getCurrentInstance();
FacesMessage=新的FacesMessage(FacesMessage.SEVERITY_INFO,“初始化用户组转换器时出错!”,null);
fc.addMessage(空,消息);
}
}
/*
*(非Javadoc)
*@see javax.faces.convert.Converter#getAsObject(javax.faces.context.FacesContext,javax.faces.component.UIComponent,java.lang.String)
*/
@凌驾
公共用户组getAsObject(FacesContext上下文、UIComponent组件、,
字符串值){
用户组ug=null;
试一试{
ug=getGroupMap().get(值);
}捕获(例外e){
log.error(e.getMessage(),e);
FacesContext fc=FacesContext.getCurrentInstance();
FacesMessage=新的FacesMessage(FacesMessage.SEVERITY_INFO,“转换用户组时出错!”,null);
fc.addMessage(空,消息);
}
返回ug;
}
/*
*(非Javadoc)
*@see javax.faces.convert.Converter#getAsString(javax.faces.context.FacesContext,javax.faces.component.UIComponent,java.lang.Object)
*/
@凌驾
公共字符串getAsString(FacesContext上下文、UIComponent、,
对象值){
字符串名称=((用户组)值).getName();
返回名称;
}
}
祝你玩得开心 您好,BalusC,staffCode的类型为整数。我将断点设置为debug,但它从未跳转到getAsObject函数中,我不明白为什么。在对象上使用
=
是错误的,但如果从未调用getAsObject()
,这不能成为原因。我看不出原因。也许它是RichFaces特有的。我将代码从==替换为equals:this.staffCode.equals(other.staffCode),但它仍然显示错误:frmReport:j_idt26:Validation error:Value不是validinges,我知道,我已经说过,如果从未调用getAsObject()
,这不可能是原因。但这至少消除了未来可能出现的问题。嗨,巴卢斯克,我刚刚更新了我的代码,但仍然显示错误,如果我有什么错误,请你纠正我好吗?