【发布时间】:2013-08-30 04:23:42
【问题描述】:
我有这个简单的复合组件:
<!-- INTERFACE -->
<cc:interface>
<cc:attribute name="username" required="true" type="java.lang.String"/>
<cc:attribute name="password" required="true" type="java.lang.String"/>
<cc:editableValueHolder name="input" targets="username password"/>
<cc:attribute name="validator" method-signature=
"void Action(javax.faces.context.FacesContext,
javax.faces.component.UIComponent,Object)"
required="true"/>
</cc:interface>
<!-- IMPLEMENTATION -->
<cc:implementation>
<h:panelGrid columns="3" styleClass="components" cellpadding="5px">
<h:outputText value="#{msg['login.username']}"/>
<h:inputText id="username" value="#{cc.attrs.username}" required="true"/>
<h:message styleClass="error" for="username"/>
<h:outputText value="#{msg['login.password']}"/>
<h:inputSecret id="password" value="#{cc.attrs.password}"
validator="#{cc.attrs.validator}" required="true"/>
<h:message styleClass="error" for="password"/>
<h:commandButton value="#{msg['login.confirm']}"/>
</h:panelGrid>
</cc:implementation>
我就是这样使用它的(当然都是在 h:form 标签内):
<imp:loginComponent username="#{databaseManager.username}"
password="#{databaseManager.password}"
validator="#{databaseManager.validator}" >
</imp:loginComponent>
这里可以看到我的验证方法:
public void validator(FacesContext context, UIComponent component, Object value)
throws ValidatorException {
// ziskani username a password komponent
UIInput passwordComponent = (UIInput) component;
UIInput usernameComponent = ((UIInput) component.findComponent("username"));
// az do teto chvile byl vstup validni?
if (usernameComponent.isValid() && passwordComponent.isValid()) {
// validace proti DB
String username = usernameComponent.getValue().toString();
String password = value.toString();
if (!userRecordFacade.authorizedAcces(username, password)) {
System.out.println("blbe");
FacesMessage message = new FacesMessage(FacesMessage.SEVERITY_ERROR, "Invalid", null);
throw new ValidatorException(message);
}
}
}
在我解决 ValidatorException 之前一切正常。没有例外,但在 h:message 中输入密码时出现了一些奇怪的消息:/login.xhtml @16,75 validator="#{databaseManager.validator}": The class 'com.dusek.DatabaseManager' does not have the property '验证器”。
这怎么可能?如果该方法没有抛出 ValidatorException,那就很好。但这是非常糟糕的行为,我的意思是:-)。
你能告诉我一些建议吗?谢谢。
【问题讨论】:
-
我知道,可以进行一些“应用程序验证”(ibm.com/developerworks/library/j-jsf3),但恐怕它在语义上不正确。
-
我现在无法解释这个问题,因为我从未使用过这种方法,我也没有心情在本地重新创建和调试它,但我至少可以告诉这是一种奇怪的做法。你应该这样做:stackoverflow.com/a/10874531。此外,此设计中复合组件的选择有些奇怪,因为它最终会绑定到模型中的多个属性,而正确的复合应该只绑定到模型中的一个属性。另见stackoverflow.com/a/6822269。对于您心目中的工作而言,复合材料本质上是错误的工具。
-
但是还有一个麻烦:我想在验证方法里面使用EJB(这是这一行:if (!userRecordFacade.authorizedAcces(username, password)))
-
我认为这是复合组件的 10000 个错误之一。我认为当你实现一个 getter
getValidator时它就解决了。它不会被使用,但出于某种原因 JSF 想要它。 -
Petr:stackoverflow.com/q/7572335 @noone:我不会称它为错误,因为它本质上是一种滥用。很多,真的很多 JSF 初学者没有看到复合材料的意义,并将它们用于他们最初不是为之设计的事情。
标签: validation jsf composite-component