0

JSF2.1.1を使用しています。国のコメントを投稿するために使用されるサンプルJSFページがあります。f:viewparamタグを使用して国のページを選択します。コードは次のとおりです。

country.xhtml

<f:metadata>
        <f:viewParam name="country" value="#{countryBean2.selectedCountry}" converter="countryConverter" />
    </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/>
            <h:commandButton value="Send">
                <f:ajax  listener="#{countryBean2.sendComment}" render="form" />
            </h:commandButton>
        </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();
    }
}

国のページを正常に開くことはできますが(たとえばhttp://localhost:8080/test/faces/country.xhtml?country=england)、を使用してコメントを投稿しようとするとcommandButtonsetCommentセッターが呼び出されず、comment変数は残りnullます。immediate="true"との両方inputTextに設定しようとしましcommandButtonたが、動作しません。

4

1 に答える 1

1

execute属性の<f:ajax>デフォルト@thisは、現在のコンポーネントです。@formフォーム全体を送信する場合は、代わりに必要です。にもこれを使用renderします。

<h:commandButton value="Send">
    <f:ajax execute="@form" listener="#{countryBean2.sendComment}" render="@form" />
</h:commandButton>
于 2012-05-14T13:54:27.547 に答える