1

私はjsf2.1.1とprimefaces3.0.M4を使用しています。国のコメントを投稿するために使用されたサンプルのjsfページがあります。コンバーターでf:viewparamタグを使用して、国のページを表示します。コードは次のとおりです。

country.xhtml:

<f:metadata>
        <f:viewParam name="country" value="#{countryBean2.selectedCountry}" converter="countryConverter" required="true"/>
    </f:metadata>

    <h:head>
        <title>Country</title>
    </h:head>

    <h:body>
        <h:form id="form">
            <h:outputText value="#{countryBean2.selectedCountry.countryName}" />
            <br/><br/>
            <h:outputText value="Comment:" />
            <h:inputText value="#{countryBean2.comment}" />
            <br/>
            <p:commandButton value="Send" action="#{countryBean2.sendComment}" update="@this" />
        </h:form>
    </h:body>

CountryBean2.java:

@Named("countryBean2")
@SessionScoped
public class CountryBean2 implements Serializable {
    private EntityCountry selectedCountry;
    private String comment;

    public EntityCountry getSelectedCountry() { return selectedCountry; }
    public void setSelectedCountry(EntityCountry newValue) { selectedCountry = newValue; }

    public String getComment() { return comment; }
    public void setComment(String newValue) { comment = newValue; }

    EntityManagerFactory emf = Persistence.createEntityManagerFactory("testPU");

    public void sendComment() {
        EntityManager em = emf.createEntityManager();
        try {
            FacesMessage msg = null;
            EntityTransaction entr = em.getTransaction();
            boolean committed = false;
            entr.begin();
            try {
                EntityCountryComment c = new EntityCountryComment();
                c.setCountry(selectedCountry);
                c.setComment(comment);
                em.persist(c);
                committed = true;
                msg = new FacesMessage();
                msg.setSeverity(FacesMessage.SEVERITY_INFO);
                msg.setSummary("Comment was sended");
            } finally {
                if (!committed) entr.rollback();
            }
        } finally {
            em.close();
        }
    }
}

CountryConverter.java:

public class CountryConverter implements Converter {
    public static EntityCountry country = new EntityCountry();

    EntityManagerFactory emf = Persistence.createEntityManagerFactory("testPU");


    @Override
    public EntityCountry getAsObject(FacesContext context, UIComponent component, String value) {
        EntityManager em = emf.createEntityManager();
        Query query = em.createQuery("SELECT c FROM EntityCountry c WHERE c.countryName = :countryName")
                .setParameter("countryName", value);
        country = (EntityCountry) query.getSingleResult();
        return country;
    }


    @Override
    public String getAsString(FacesContext context, UIComponent component, Object value) {
        EntityCountry c = (EntityCountry) value;
        return c.getCountryName();
    }
}

コマンドボタンを使用してコメントを投稿しているときに、CountryConverterを呼び出さずに「setComment」セッターを呼び出したい。どうやってやるの ?

4

1 に答える 1

1

残念ながら、これは<f:viewParam>コンポーネントの設計によるものです。リクエストパラメータを変換し、すべてのHTTPリクエストとポストバックにプロパティを設定します。この動作を変更するに<f:viewParam>は、その状態の初期リクエストパラメータを記憶していないカスタムコンポーネントで拡張する必要があります。setSubmittedValue()これは比較的単純で、とgetSubmittedValue()を委任する代わりにStateHelper、インスタンス変数にする必要があります。これについては、このブログで詳しく説明しています。

@FacesComponent("com.my.UIStatelessViewParameter")
public class UIStatelessViewParameter extends UIViewParameter {

    private String submittedValue;

    @Override
    public void setSubmittedValue(Object submittedValue) {  
        this.submittedValue = (String) submittedValue;
    }

    @Override
    public String getSubmittedValue() { 
        return submittedValue;
    }   
}

OmniFacesには、このためのすぐに使用できるコンポーネントがあり<o:viewParam>ます。これがライブの例です。

于 2012-05-14T18:21:01.307 に答える