我正在使用 jsf 2.1、prettyfaces 3.3.3 和休眠 jpa 3.6.7。我有国家页面,我正在尝试使用命令按钮发送评论。
国家.xhtml:
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:f="http://java.sun.com/jsf/core">
<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/>
<h:commandButton value="Send" action="#{countryBean2.sendComment}" />
</h:form>
</h:body>
</html>
国家转换器:
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();
}
漂亮的config.xml:
<pretty-config xmlns="http://ocpsoft.com/prettyfaces/3.3.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://ocpsoft.com/prettyfaces/3.3.0
http://ocpsoft.com/xml/ns/prettyfaces/ocpsoft-pretty-faces-3.3.0.xsd">
<url-mapping id="home">
<pattern value="/" />
<view-id value="/faces/index.xhtml" />
</url-mapping>
<url-mapping id="country">
<pattern value="/country/#{country}" />
<view-id value="/faces/country.xhtml" />
</url-mapping>
</pretty-config>
faces-config.xml 中的转换器配置:
<converter>
<converter-id>countryConverter</converter-id>
<converter-for-class>test.EntityCountry</converter-for-class>
<converter-class>test.CountryConverter</converter-class>
</converter>
当我首先打开 localhost:8080/test/country/england 页面时,一切正常。但是当我尝试通过命令按钮发送评论时,countryConverter 的 getAsObject 方法再次调用错误的字符串参数(例如“test.CountryBean@bd9eff”)并且找不到实体。
当我使用默认的丑陋网址(例如 localhost:8080/test/faces/country.xhtml?country=england)并尝试发送评论时,countryConverter 的 getAsObject 方法正在使用 true 字符串参数调用,我可以成功发送评论。我认为这是一个漂亮的错误,但我想使用漂亮的网址。