1

私は JSF 2.1 と PrimeFaces 3.2、サーバー Tomcat 7 を使用してアプリケーションに取り組んでいます。現在、新しいユーザーを登録するためのフォームに取り組んでいます。問題はコンバータにあります。

標準フィールドはほとんど使用せず、そのうちの 2 つはパスワードです。パスワードのカスタムデータ型があるため、コンバーターを使用して、文字列データをフィールドから Bean のパスワード変数に変換したいと考えています。Primefaces フォームは送信後に AJAX を使用しますが、おそらく問題があります。検証エラーなしでフォームに完全に入力すると、すべてが正常に機能します。しかし、検証エラーがあり、コンバーター エラーがない場合 (コンバーターでパスワードの長さを確認します)、フォーム全体がまったく機能しなくなります。再び機能させるには、ページを更新する必要があります。

ここにいくつかのソースがあります:

パスワード クラス:

public class Password {

    public static final short MIN_LENGTH = 5;

    private String text;
    private String hash;

    public Password(String text) {
        this.text = text;
        this.hash = Hasher.sha512(text);
    }

    /**
     * Get password instance with known hash only
     * @param hash SHA-512 hash
     * @return Password instance
     */
    public static Password getFromHash(String hash) {
        Password password = new Password(null);
        password.hash = hash;
        return password;
    }



    @Override
    public int hashCode() {
        return hash.hashCode();
    }

    @Override
    public boolean equals(Object obj) {
        if (obj == null) {
            return false;
        }
        if (getClass() != obj.getClass()) {
            return false;
        }
        final Password other = (Password) obj;
        if ((this.hash == null) ? (other.hash != null) : !this.hash.equals(other.hash)) {
            return false;
        }
        return true;
    }

    @Override
    public String toString() {
        return hash;
    }

    /**
     * @return the text
     */
    public String getText() {
        return text;
    }
}

パスワードコンバーター:

@FacesConverter(forClass = Password.class)
public class PasswordConverter implements Converter {

    @Override
    public Object getAsObject(FacesContext context, UIComponent component, String value) {
        String text = (String) value;

        if (text.length() >= Password.MIN_LENGTH) {
            return new Password(text);
        }

        FacesMessage msg = new FacesMessage(Texter.get("forms/forms", "shortPassword").replace("%limit%", String.valueOf(Password.MIN_LENGTH)));
        msg.setSeverity(FacesMessage.SEVERITY_ERROR);
        throw new ConverterException(msg);
    }

    @Override
    public String getAsString(FacesContext context, UIComponent component, Object value) {
        try {
            Password password = (Password) value;
            return password.getText();
        } catch (Exception ex) {
            throw new ConverterException(ex);
        }
    }
}

facelet のフォーム:

<h:form id="registration">
    <p:panelGrid columns="3">

        <h:outputLabel value="#{commonTxt.email}:" for="email" />
        <p:inputText id="email" value="#{userRegistrationForm.email}" required="true" requiredMessage="#{formsTxt.msgEmpty}">
            <f:validator validatorId="email" />

            <f:validator validatorId="unique" />
            <f:attribute name="entity" value="SystemUser" />
            <f:attribute name="field" value="email" />
            <f:attribute name="uniqueMessage" value="#{formsTxt.nonUniqueEmail}" />
        </p:inputText>
        <p:message for="email" />

        <h:outputLabel value="#{usersTxt.password}:" for="password" />
        <p:password id="password" value="#{userRegistrationForm.password}" binding="#{password}" autocomplete="off" feedback="true" weakLabel="#{formsTxt.passwordWeak}" goodLabel="#{formsTxt.passwordGood}" strongLabel="#{formsTxt.passwordStrong}" promptLabel="#{formsTxt.passwordPrompt}" />
        <p:message for="password" />

        <h:outputLabel value="#{usersTxt.passwordCheck}:" for="passwordCheck" />
        <p:password id="passwordCheck" value="#{userRegistrationForm.passwordCheck}" binding="#{passwordCheckInput}" autocomplete="off">
            <f:validator validatorId="match" />
            <f:attribute name="matchAgainst" value="#{password}" />
            <f:attribute name="matchMessage" value="#{formsTxt.passwordMismatch}" />
        </p:password>
        <p:message for="passwordCheck" />

        <p:column /><p:column /><p:column />

        <h:outputLabel value="#{usersTxt.name}:" for="name" />
        <p:inputText id="name" value="#{userRegistrationForm.name}" maxlength="255" required="true" requiredMessage="#{formsTxt.msgEmpty}" />
        <p:message for="name" />


        <f:facet name="footer">
            <p:commandButton value="#{usersTxt.register}" action="#{userRegistrationForm.register()}" update="registration" />
        </f:facet>
    </p:panelGrid>
</h:form>

bean のコードは掲載しません#{userRegistrationForm}。getter と setter を持つ 2 つの Passwords プロパティがあります。

私の問題の解決につながる助けをいただければ幸いです。前もって感謝します。

4

1 に答える 1

1

解決しました!以前FacesContext.getCurrentInstance().isValidationFailed()は、検証が失敗したかどうかを確認していました。失敗した場合、コンバーターは null を返すようになりました (変換は行われません)。それ以外の場合、コンバーターは適切なオブジェクトを返します。そして、フォームは変換が機能しても問題なく機能します。

于 2012-04-10T16:25:30.940 に答える