Prettyfaces issue when using with viewparam converter
Splash › Forums › PrettyFaces Users › Prettyfaces issue when using with viewparam converter
Tagged: converter
This topic contains 7 replies, has 3 voices, and was last updated by Christian Kaltepoth 7 months, 1 week ago.
-
AuthorPosts
-
October 15, 2012 at 1:30 pm #18800
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}" />
<h:outputText value="Comment:" />
<h:inputText value="#{countryBean2.comment}" />
<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.url = :url")
.setParameter("url", value);
country = (EntityCountry) query.getSingleResult();
return country;
}
@Override
public String getAsString(FacesContext context, UIComponent component, Object value) {
EntityCountry c = (EntityCountry) value;
return c.getUrl();
}
}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 also asked this question before there: http://ocpsoft.org/support/prettyfaces-users/pretty-urls-turns-ugly-when-i-click-commandbutton-without-immediate-attribute but it was not resolved.
October 15, 2012 at 8:55 pm #23084First, please do not paste HTML formatting into code snippits. I had to go back and clean up your post. It helps us answer you faster if you make sure it looks good

Now that that’s out of the way!
Could you please upload a small example project that we can use to reproduce this problem? I think we need to debug to figure out what is going on. Otherwise, if you want, take a look in PrettyActionHandler and see if you can figure out why the link is being converted “too aggressively.”
Sorry your first post was not answered to your satisfaction. I believe it was left after I asked you to upload a simple project with as few dependencies as possible, that way we could actually build and run it. Hopefully you can do this, because it sounds like a very complicated issue and we need to be able to look at the actual code and debug it!
Cheers,
Lincoln
October 15, 2012 at 8:56 pm #23085A simple JPA / JSF / PrettyFaces project would be best.
October 16, 2012 at 8:37 am #23086Just for the sake of completeness. Here is the corresponding stackoverflow post:
http://stackoverflow.com/questions/12842275/prettyfaces-issue-when-using-with-viewparam-converter
October 16, 2012 at 4:42 pm #23087hey i found the mistake when i try to make simpler example. 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. but i don’t understand relationship between @Named annotation and prett-config.xml.
October 16, 2012 at 4:46 pm #23088Awesome! Glad you solved it! I figured it was something weird like that, but I was also scared it was going to be a bug
We haven’t had any bugs from this before so it would have been interesting (scary) to debug 
~Lincoln
October 17, 2012 at 5:07 am #23089AFAIK for a pattern like
/country/#{country}PrettyFaces also interpretscountryas an EL expressions and tries to set the path parameter value (which will usually work). Perhaps this is related?I would like to change this behavior, but I’m afraid it may break other peoples code. Especially because people may use
#{country}inside the page for displaying it or something like this.October 17, 2012 at 5:08 am #23090BTW: This explains why the URL renders to: test.CountryBean@bd9eff
-
AuthorPosts
You must be logged in to reply to this topic.