【发布时间】:2014-01-16 15:31:02
【问题描述】:
我有这样的表格:
<h:form>
Phone number:
<h:inputText id="phone_number"
value="#{dbUserManager.phoneNumber}"
validatorMessage="The phone number is invalid."
required="#{auth.value eq 'MOBILE'}"
requiredMessage="Phone number is required." />
</h:inputText>
Authentication type:
<h:selectOneMenu value="#{dbUserManager.authentication}" converter="#{authenticationTypeConverter}" binding="#{auth}">
<f:selectItems value="#{dbUserManager.authTypeList}" />
</h:selectOneMenu>
</h:form>
AuthenticationType 是一个类似这样的实体:
@Entity
public class AuthenticationType implements Serializable
{
@Id
@Basic(optional = false)
private Short id;
@Basic(optional = false)
private String description;
@Override
//...
public String toString()
{
return description;
}
最后是 AuthenticationTypeConverter:
@Stateless
@ManagedBean
@FacesConverter(value="authenticationTypesConverter")
@TransactionAttribute(TransactionAttributeType.REQUIRED)
public class AuthenticationTypeConverter implements Converter
{
@PersistenceContext(unitName="pu")
private EntityManager em = null;
@Override
public Object getAsObject(FacesContext fc, UIComponent uic, String string)
{
TypedQuery<AuthenticationType> q = em.createNamedQuery(
AuthenticationType.FIND_BY_DESCRIPTION,
AuthenticationType.class);
q.setParameter("description", string);
return q.getSingleResult();
}
@Override
public String getAsString(FacesContext fc, UIComponent uic, Object o)
{
if (o == null)
{
return "";
}
return o.toString();
}
}
我尝试做的是,当身份验证类型设置为“移动”时,将所需的电话号码设置为 true。但它的行为不像我预期的那样。
我认为问题在于我需要将 FacesConverter 用于 AuthenticationType。但是不知道怎么解决。
谁能给个提示?
谢谢
【问题讨论】:
标签: validation jsf-2