【问题标题】:Prettyfaces issue when using with viewparam converter与 viewparam 转换器一起使用时出现 Prettyfaces 问题
【发布时间】:2012-10-02 06:40:10
【问题描述】:

我正在使用 jsf 2.1、prettyfaces 3.3.3 和 hibernate 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();
    }

漂亮的配置.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”)并且找不到实体。

当我使用默认的丑陋 url(例如 localhost:8080/test/faces/country.xhtml?country=england)并尝试发送评论时,countryConverter 的 getAsObject 方法正在使用 true 字符串参数调用,我可以成功发送评论.我认为这是一个漂亮的错误,但我想使用漂亮的网址。

【问题讨论】:

    标签: jsf prettyfaces


    【解决方案1】:

    您能否尝试为EntityCountry 类型注册您的转换器。如果您使用faces-config.xml 进行配置,请使用以下内容:

    <converter>
      <converter-for-class>com.myapp.EntityCountry</converter-for-class>
      <converter-class>com.myapp.CountryConverter</converter-class>
    </converter>
    

    来自 PrettyFaces 文档:

    请注意 PrettyFaces 会自动使用 JSF 转换器 注册为引用的bean属性的类型来转换 路径参数。这意味着 PrettyFaces 支持所有 JSF 标准 已手动注册为的转换器和转换器 用于使用converter-for-class 元素的特定类型 faces-config.xml(或@FacesConverter 的forClass 属性 注释)。

    如果这不起作用,请在 OcpSoft 支持论坛上打开一个主题:

    http://ocpsoft.org/support/

    我希望这会有所帮助。 :)

    【讨论】:

    • 我在 faces-config.xml 中编辑并添加了我的转换器配置到我的问题中。同样的错误再次发生。我也有关于 ocpsoft 的话题:ocpsoft.org/support/prettyfaces-users/…
    • 我试图重现这一点,但在我的测试中一切正常。您能否在 ocpsoft.org 上开设一个新帖子来讨论这个问题?
    • 你尝试过实体对象吗?当我用简单的 java 类创建国家类时,一切正常。实体对象出现问题。
    • 是的,我用实体进行了测试。但这不应该有任何区别。对于转换器来说,它只是一个对象。
    • 我在 ocpsoft 上打开了新主题:ocpsoft.org/support/prettyfaces-users/… 我想看看你的测试,如果可能的话,我想把我的测试项目发给你。谢谢。
    【解决方案2】:

    我有另一个以“国家”命名的托管 bean,并且在 pretty-config.xml 中有以“国家”命名的模式值。

    @Named("country")
    @SessionScoped
    public class CountryBean implements Serializable {
        .......
    }
    

    当我更改 @Named("country") 值时,它会成功运行。

    【讨论】:

    • 所以你的 CountryBean 托管 bean 默认命名为 countryBean 对吗?
    猜你喜欢
    • 2012-02-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-03-15
    • 2011-05-04
    • 2011-05-27
    相关资源
    最近更新 更多