我得到了null Converter错误,我认为这是一个非常简单的场景:
<!-- My View -->
<ui:composition template="/template/template_v1.xhtml"
xmlns="http://www.w3.org/1999/xhtml"
xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:h="http://java.sun.com/jsf/html">
<!-- Simplified for clarity -->
<h:form>
<div class="block-panel customer-data">
<h:outputLabel for="txtUsername">Username:</h:outputLabel>
<h:inputText id="txtUsername" name="Username"
value="#{userBean.user.id}"
styleClass="text" />
<rich:message id="errorUsername" for="txtUsername"/>
</div>
<!-- Other fields omitted for clarity -->
</h:form>
/* The User Bean - simplified */
@ManagedBean
@ViewScoped
public class UserBean implements Serializable {
private User user;
public User getUser() {
// Contains logic for reading a user from the database or creating a new
// user object
return user;
}
public void setUser(User user) {
this.user = user;
}
/* The user Entity - Simplified */
@Entity
@Table(name = "user")
@Inheritance(strategy = InheritanceType.JOINED)
@DiscriminatorColumn(name = "user_type", discriminatorType = DiscriminatorType.STRING)
public class User implements IEntity<String>, Serializable {
private static final long serialVersionUID = 1L;
private String id;
@Id
@Column(name = "username", length = 50)
@NotNull(message = "{userIdMandatory}")
@Size(max = 50)
public String getId() {
return this.id;
}
public void setId(String id) {
this.id = id;
}
/* The IEntity interface */
public interface IEntity<ID extends Serializable> {
ID getId();
void setId(final ID pId);
}
所以基本上我试图将我的用户实体的字符串属性绑定到inputText字段。就我而言,应该不需要转换,所以我不应该得到我所看到的错误。
有趣的是,如果我将以下getter和setter添加到我的实体:
public String getTmpId() {
return this.id;
}
public void setTmpId(String id) {
this.id = id;
}
然后对我的视图进行必要的更改以绑定到tmpId而不是id,一切都按预期工作。
这对我来说似乎是一个错误,因为我绑定到接口中定义的getter / setter,在通用接口中定义或者因为getter标记了Id属性。不过我会很感激别人的想法。
顺便说一下,我继承了这个设计,并不特别喜欢它,所以我可能最终会重构它以引入新的用户名属性而不是尝试使用Id。
答案 0 :(得分:1)
据我所知,我认为这是由BeanELResolver中的一个模糊错误引起的,它用于获取被绑定的属性的类型 - 而不是返回String它返回Serializable,没有转换器和因此我看到的错误。
它不是特别优雅,但我通过向userBean添加userId属性然后绑定到我的视图中的属性而不是用户实体上的Id属性来解决这个问题。然后我在保存时使用该值在实体上手动设置Id:
<!-- My New View -->
<ui:composition template="/template/template_v1.xhtml"
xmlns="http://www.w3.org/1999/xhtml"
xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:h="http://java.sun.com/jsf/html">
<!-- Simplified for clarity -->
<h:form>
<div class="block-panel customer-data">
<h:outputLabel for="txtUsername">Username:</h:outputLabel>
<h:inputText id="txtUsername" name="Username"
value="#{userBean.userId}"
styleClass="text" />
<rich:message id="errorUsername" for="txtUsername"/>
</div>
<!-- Other fields omitted for clarity -->
/* The New User Bean - simplified */
@ManagedBean
@ViewScoped
public class UserBean implements Serializable {
private string userId;
private User user;
public String getUserId() {
return userId;
}
public void setUserId(String userId) {
this.userId = userId;
}
public User getUser() {
// Contains logic for reading a user from the database or creating a new
// user object
return user;
}
public void setUser(User user) {
this.user = user;
}
public void saveUser() {
if (user.getId() == null) {
user.setId(userId);
}
// Actual saving omitted for brevity
}
}