问题
i'm using jsf 2.1, prettyfaces 3.3.3 and hibernate jpa 3.6.7. i have country page and i'm trying to send comment with commandbutton.
country.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>
countryConverter:
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();
}
pretty-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>
converter configuration in faces-config.xml:
<converter>
<converter-id>countryConverter</converter-id>
<converter-for-class>test.EntityCountry</converter-for-class>
<converter-class>test.CountryConverter</converter-class>
</converter>
when i open localhost:8080/test/country/england page firstly, everything works well. but when i try to send comment via commandbutton, countryConverter's getAsObject method is calling again with wrong string parameter (such as "test.CountryBean@bd9eff") and entity can not be found.
when i use with default ugly url (such as localhost:8080/test/faces/country.xhtml?country=england) and try to send comment, countryConverter's getAsObject method is calling with true string parameter and i can send comment successfully. i think it is a prettyfaces bug but i want to use pretty urls.
回答1:
Could you perhaps try to register your converter gobally for the EntityCountry
type. If you are using faces-config.xml
for configuration, use something like this:
<converter>
<converter-for-class>com.myapp.EntityCountry</converter-for-class>
<converter-class>com.myapp.CountryConverter</converter-class>
</converter>
From the PrettyFaces documentation:
Please note that PrettyFaces will automatically use the JSF converter registered for the type of the referenced bean property to convert the path parameter. This means that PrettyFaces supports all JSF standard converters and converters that have been manually registered to be used for a specific type using the converter-for-class element in the faces-config.xml (or the forClass attribute of the @FacesConverter annotation).
If this doesn't work, please open a topic on the OcpSoft support forums:
http://ocpsoft.org/support/
I hope this helps. :)
回答2:
i have another managed bean that named with "country" and i have pattern value named with "country" in pretty-config.xml.
@Named("country")
@SessionScoped
public class CountryBean implements Serializable {
.......
}
when i change @Named("country") value, it is working successfully.
来源:https://stackoverflow.com/questions/12842275/prettyfaces-issue-when-using-with-viewparam-converter