如何使用Prettyfaces与转换器?

时间:2012-01-07 13:50:21

标签: jsf-2 prettyfaces

我正在使用prettyfaces 3.3.2和JSF 2.2.1。我想将漂亮的url字符串转换为实体对象并使用网页中的对象。漂亮-config.xml中:

<url-mapping id="country">
    <pattern value="/country/#{countryBean.selectedCountry}">
        <convert param="#{countryBean.selectedCountry}" converterId="countryConverter" converterClass="Test.CountryBean" />
    </pattern>
    <view-id value="/faces/countries/country.xhtml" />
</url-mapping>

country.xhtml:

<f:metadata>
    <f:viewParam name="country" value="#{countryBean.selectedCountry}">
        <f:converter converterId="countryConverter" />
    </f:viewParam>
</f:metadata>
<ui:define name="content">
    #{countryBean.selectedCountry.country}
</ui:define>

CountryConverter:

public static EntityCountry country = new EntityCountry();

EntityManagerFactory emf = Persistence.createEntityManagerFactory("testPU");

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 = :countryUrl")
            .setParameter("countryUrl", value);
    country = (EntityCountry) query.getSingleResult();
    return country;
}

public String getAsString(FacesContext context, UIComponent component, Object value) {
    EntityCountry c = (EntityCountry) value;
    return c.getUrl();
}

我将断点切换到getAsObject方法,但它没有命中断点。 faces-config.xml中:

<converter>
    <converter-id>countryConverter</converter-id>
    <converter-class>Test.CountryConverter</converter-class>
</converter>
<managed-bean>
    <managed-bean-name>countryBean</managed-bean-name>
    <managed-bean-class>Test.CountryBean</managed-bean-class>
    <managed-bean-scope>session</managed-bean-scope>
</managed-bean>

CountryBean:

@Named("countryBean")
@SessionScoped
public class CountryBean implements Serializable {
    private EntityCountry selectedCountry;

    public EntityCountry getSelectedCountry() { return selectedCountry; }
    public void setSelectedCountry(EntityCountry newValue) { selectedCountry = newValue; }
}

错误:

PrettyFaces: Exception occurred while processing <country:#{countryBean.selectedCountry}> for URL </country/turkiye>
- Stack Trace
com.ocpsoft.pretty.PrettyException: PrettyFaces: Exception occurred while processing <country:#{countryBean.selectedCountry}> for URL 

</country/turkiye>
    at com.ocpsoft.pretty.faces.beans.ParameterInjector.injectPathParams(ParameterInjector.java:96)
    at com.ocpsoft.pretty.faces.beans.ParameterInjector.injectParameters(ParameterInjector.java:54)
    at com.ocpsoft.pretty.faces.event.PrettyPhaseListener.afterPhase(PrettyPhaseListener.java:98)
    at com.sun.faces.lifecycle.Phase.handleAfterPhase(Phase.java:189)
    at com.sun.faces.lifecycle.Phase.doPhase(Phase.java:107)
    at com.sun.faces.lifecycle.RestoreViewPhase.doPhase(RestoreViewPhase.java:116)
    at com.sun.faces.lifecycle.LifecycleImpl.execute(LifecycleImpl.java:118)
    at javax.faces.webapp.FacesServlet.service(FacesServlet.java:593)
    at org.apache.catalina.core.StandardWrapper.service(StandardWrapper.java:1539)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:343)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:217)
    at com.ocpsoft.pretty.PrettyFilter.doFilter(PrettyFilter.java:118)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:256)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:217)
    at org.apache.catalina.core.ApplicationDispatcher.doInvoke(ApplicationDispatcher.java:785)
    at org.apache.catalina.core.ApplicationDispatcher.invoke(ApplicationDispatcher.java:649)
    at org.apache.catalina.core.ApplicationDispatcher.processRequest(ApplicationDispatcher.java:483)
    at org.apache.catalina.core.ApplicationDispatcher.doDispatch(ApplicationDispatcher.java:454)
    at org.apache.catalina.core.ApplicationDispatcher.dispatch(ApplicationDispatcher.java:350)
    at org.apache.catalina.core.ApplicationDispatcher.forward(ApplicationDispatcher.java:300)
    at com.ocpsoft.pretty.PrettyFilter.doFilter(PrettyFilter.java:110)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:256)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:217)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:279)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:175)
    at org.apache.catalina.core.StandardPipeline.doInvoke(StandardPipeline.java:655)
    at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:595)
    at com.sun.enterprise.web.WebPipeline.invoke(WebPipeline.java:98)
    at com.sun.enterprise.web.PESessionLockingStandardPipeline.invoke(PESessionLockingStandardPipeline.java:91)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:162)
    at org.apache.catalina.connector.CoyoteAdapter.doService(CoyoteAdapter.java:330)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:231)
    at com.sun.enterprise.v3.services.impl.ContainerMapper.service(ContainerMapper.java:174)
    at com.sun.grizzly.http.ProcessorTask.invokeAdapter(ProcessorTask.java:828)
    at com.sun.grizzly.http.ProcessorTask.doProcess(ProcessorTask.java:725)
    at com.sun.grizzly.http.ProcessorTask.process(ProcessorTask.java:1019)
    at com.sun.grizzly.http.DefaultProtocolFilter.execute(DefaultProtocolFilter.java:225)
    at com.sun.grizzly.DefaultProtocolChain.executeProtocolFilter(DefaultProtocolChain.java:137)
    at com.sun.grizzly.DefaultProtocolChain.execute(DefaultProtocolChain.java:104)
    at com.sun.grizzly.DefaultProtocolChain.execute(DefaultProtocolChain.java:90)
    at com.sun.grizzly.http.HttpProtocolChain.execute(HttpProtocolChain.java:79)
    at com.sun.grizzly.ProtocolChainContextTask.doCall(ProtocolChainContextTask.java:54)
    at com.sun.grizzly.SelectionKeyContextTask.call(SelectionKeyContextTask.java:59)
    at com.sun.grizzly.ContextTask.run(ContextTask.java:71)
    at com.sun.grizzly.util.AbstractThreadPool$Worker.doWork(AbstractThreadPool.java:532)
    at com.sun.grizzly.util.AbstractThreadPool$Worker.run(AbstractThreadPool.java:513)
    at java.lang.Thread.run(Thread.java:722)
Caused by: javax.el.ELException: java.lang.IllegalArgumentException: Cannot convert turkiye of type class 

java.lang.String to class Test.EntityCountry
    at com.sun.el.ExpressionFactoryImpl.coerceToType(ExpressionFactoryImpl.java:68)
    at org.jboss.weld.util.el.ForwardingExpressionFactory.coerceToType(ForwardingExpressionFactory.java:37)
    at com.ocpsoft.pretty.faces.util.FacesElUtils.setValue(FacesElUtils.java:86)
    at com.ocpsoft.pretty.faces.beans.ParameterInjector.injectPathParams(ParameterInjector.java:90)
    ... 46 more
Caused by: java.lang.IllegalArgumentException: Cannot convert turkiye of type class java.lang.String to 

class Test.EntityCountry
    at com.sun.el.lang.ELSupport.coerceToType(ELSupport.java:397)
    at com.sun.el.ExpressionFactoryImpl.coerceToType(ExpressionFactoryImpl.java:66)
    ... 49 more

1 个答案:

答案 0 :(得分:2)

看起来你正在使用PrettyFaces注入和ViewParams。如果您使用的是ViewParams,则应使用命名参数而不是PrettyFaces的参数注入,如下所示:

<url-mapping id="country">
    <pattern value="/country/#{country}" />
    <view-id value="/faces/countries/country.xhtml" />
</url-mapping>

然后你可以让JSF按照你的要求进行转换值的工作。