与Viewparam转换器一起使用时的Prettyfaces问题 [英] Prettyfaces issue when using with viewparam converter

查看:69
本文介绍了与Viewparam转换器一起使用时的Prettyfaces问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用jsf 2.1,prettyfaces 3.3.3和hibernate jpa 3.6.7.我有国家/地区页面,并且正在尝试使用CommandButton发送评论.

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:

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:

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.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 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>

当我首先打开localhost:8080/test/country/england页面时,一切正常.但是当我尝试通过命令按钮发送评论时,countryConverter的getAsObject方法使用错误的字符串参数(例如"test.CountryBean@bd9eff")再次调用,并且找不到实体.

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.

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

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.

推荐答案

在pretty-config.xml中,我还有另一个以"country"命名的托管bean,并且有以"country"命名的模式值.

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 {
    .......
}

当我更改@Named("country")值时,它可以成功工作.

when i change @Named("country") value, it is working successfully.

这篇关于与Viewparam转换器一起使用时的Prettyfaces问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆