1

我正在使用 jsf 2.1.1 和 primefaces 3.0.M4。我有一个用于发布国家/地区评论的示例 jsf 页面。我使用带有转换器的 f:viewparam 标签来查看国家页面。以下是代码:

国家.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()and委托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 回答