我正在使用jsf 2.1.1和primefaces 3.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();
}
}
我想调用“setComment”setter而不调用CountryConverter,当我使用commandbutton发表评论时。我怎么能这样做?
答案 0 :(得分:1)
不幸的是,这是<f:viewParam>
组件的设计。它将转换请求参数并在每个 HTTP请求上设置属性,也在回发上设置。为了更改此行为,您需要使用自定义组件扩展<f:viewParam>
,该组件不记得其状态中的初始请求参数。它非常简单,而不是将setSubmittedValue()
和getSubmittedValue()
委托给StateHelper
,您只需要将其设为实例变量即可。这在this blog中有详细描述。
@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>
的味道。这是live example。