[英]Prettyfaces issue when using with viewparam converter
我正在使用jsf 2.1,prettyfaces 3.3.3和hibernate jpa 3.6.7。 我有国家页面,并且我正在尝试使用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>
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方法使用真正的字符串参数进行调用,并且我可以成功发送注释。 我认为这是一个prettyfaces错误,但我想使用漂亮的网址。
您是否可以尝试将转换器转换为EntityCountry
类型。 如果您使用faces-config.xml
进行配置,请使用类似以下的内容:
<converter>
<converter-for-class>com.myapp.EntityCountry</converter-for-class>
<converter-class>com.myapp.CountryConverter</converter-class>
</converter>
从PrettyFaces文档中:
请注意,PrettyFaces将自动使用为引用bean属性的类型注册的JSF转换器来转换path参数。 这意味着PrettyFaces支持所有JSF标准转换器和使用faces-config.xml(或@FacesConverter批注的forClass属性)中的converter-for-class元素手动注册为用于特定类型的转换器。
如果这不起作用,请在OcpSoft支持论坛上打开一个主题:
我希望这有帮助。 :)
我有另一个以“ country”命名的托管bean,并且在pretty-config.xml中有以“ country”命名的模式值。
@Named("country")
@SessionScoped
public class CountryBean implements Serializable {
.......
}
当我更改@Named(“ country”)值时,它正在成功工作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.