使用视图参数时,不会调用setter

时间:2012-05-14 13:47:48

标签: jsf jsf-2

我正在使用JSF 2.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),但是当我尝试使用commandButton发布评论时,setComment设置者不会被调用而comment 1}}变量仍为null。我尝试在immediate="true"inputText上设置commandButton,但它不起作用。

1 个答案:

答案 0 :(得分:1)

execute的{​​{1}}属性默认为<f:ajax>,即当前组件。如果您想提交整个表单,则需要@this。也可以在@form中使用它。

render