我正在尝试将转换器和验证器添加到单个字段时遇到上面指定的问题。我知道这两个是如何工作的,因为我之前使用过它们,但这是我第一次在单个场上尝试过它们。这是我的代码:
<p:selectOneMenu id="bussProgram"
value="#{signupWizardBean.subscription.businessProgram}">
<f:converter binding="#{membershipProgramConverter}"></f:converter>
<f:validator binding="#{dropdownValidator}"></f:validator>
<f:selectItems value="#{signupWizardBean.membershipPrograms}"
var="plan" itemValue="#{plan}"
itemLabel="#{plan.description}" />
</p:selectOneMenu>
转换器
@Named
@ApplicationScoped
public class MembershipProgramConverter implements Converter {
@PersistenceContext
private transient EntityManager em;
@Inject
private Logger log;
@Override
public Object getAsObject(FacesContext ctx, UIComponent component,
String value) {
if (StringUtils.isBlank(value) || value.equals("0"))
return null;
return em.find(MembershipProgram.class, new Long(value));
}
@Override
public String getAsString(FacesContext fc, UIComponent uic, Object o) {
MembershipProgram mp = (MembershipProgram) value;
return mp.getId() != null ? String.valueOf(mp.getId()) : null;
}
}
验证
@Named
@ApplicationScoped
public class DropdownValidator implements Validator, Serializable {
private static final long serialVersionUID = -456545939475878299L;
@Inject
private ResourceBundle bundle;
@Inject
private FacesContext facesContext;
@Inject
private Logger log;
@Override
public void validate(FacesContext context, UIComponent component,
Object value) throws ValidatorException {
log.debug("[dropship-web] validating value={}", value);
if (value == null) {
FacesMessage msg = new FacesMessage(
bundle.getString("error.requiredField"),
bundle.getString("error.requiredField"));
msg.setSeverity(FacesMessage.SEVERITY_ERROR);
facesContext.addMessage("promoCode", msg);
throw new ValidatorException(msg);
}
}
}
实体类:
@Entity
@Table(name = "CRM_MEMBERSHIP_PROGRAMS", uniqueConstraints = @UniqueConstraint(columnNames = { "code" }))
@SequenceGenerator(name = "ID_GENERATOR", sequenceName = "CRM_MEMBERSHIP_PROGRAM_SEQ")
public class MembershipProgram extends BaseEntity {
private static final long serialVersionUID = -7796298586810386239L;
@Size(max = 25)
@Column(name = "CODE", nullable = false)
private String code;
@Size(max = 100)
@Column(name = "DESCRIPTION", nullable = true)
private String description;
public MembershipProgram() {
}
public MembershipProgram(long id, String description) {
setId(id);
this.description = description;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public String getCode() {
return code;
}
public void setCode(String code) {
this.code = code;
}
@Override
public int hashCode() {
final int prime = 31;
int result = super.hashCode();
result = prime * result + ((code == null) ? 0 : code.hashCode());
result = prime * result
+ ((description == null) ? 0 : description.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (!super.equals(obj))
return false;
if (getClass() != obj.getClass())
return false;
MembershipProgram other = (MembershipProgram) obj;
if (code == null) {
if (other.code != null)
return false;
} else if (!code.equals(other.code))
return false;
if (description == null) {
if (other.description != null)
return false;
} else if (!description.equals(other.description))
return false;
return true;
}
}
请注意,在调试时:调用MembershipProgramConverter.getAsObject / getAsString,DropdownValidator.validate方法。
我还发现,无论是否有下拉列表中的选定项目,我都会遇到:
.validation.UnexpectedTypeException: HV000030: No validator could be found for type: com.czetsuya.models.membership.MembershipProgram.
javax.validation.UnexpectedTypeException: HV000030: No validator could be found for type: com.czetsuya.models.membership.MembershipProgram.
答案 0 :(得分:0)
最后我能够找出问题,我在目标实体上添加了@Size注释,只是删除了@Size:
@Size(max = 25)
@OneToOne(optional = false)
@JoinColumn(name = "BUSINESS_PROGRAM", nullable = false)
private MembershipProgram businessProgram;
当我删除@Size时,问题解决但我遇到了一个新的问题,在提交表单时总是抛出:
Validation Error: Value is not valid
要修复Value无效,请确保已正确实现Model.equals方法。我的错是我依赖eclipse生成hashcode和equals功能,我没注意到它从我的BaseEntity中调用了equals方法。