简体   繁体   中英

How to escape double quotes for Primefaces SelectOneMenu in Application level?

Using PrimeFaces, when the content of itemLabel of a p:selectOneMenu has the char double quote ("), the selectOneMenu stop working (can't select the item). For example, in the code below is not possible to select: Item "2"

Is there a way to escape or replace the double quotes in every selectOneMenu from the entire application?

Maybe using a tag decorator or a tag handler?

@Named(value = "bb")
@ViewScoped
@Getter @Setter
public class BB implements Serializable {

    List<String> items;
    String selectedItem;

    @PostConstruct
    public void init() {
        items = new ArrayList<>();
        items.add("Item 1");
        items.add("Item \"2\""); // <= note the double quotes here.
        items.add("Item 3");
    }

    public void logSelected() {
        System.out.println(selectedItem);
    }
}
    <h:form>
        <p:selectOneMenu value="#{bb.selectedItem}">
            <f:selectItems value="#{bb.items}"/>
        </p:selectOneMenu>

        <p:commandButton action="#{bb.logSelected}"
                         process="@form"
                         update="@form"
                         value="GO"/>
    </h:form>

When I try to select 'Item "2"', the following error is shown on console log:

Uncaught Error: Syntax error, unrecognized expression: [data-label="Item "2""]
    at Function.ga.error (eNptjUsOgzAMRE9E2XMbEwwkcmxjJ6qQenh-i6qF1UhvnmbUYsYRAnqXloq2tle8kn_02wUxvKGswsjF_4qfnUapTpFPp1KJGRqCVWrpkrcMLB5MiNAeBX9HPX9n8HmAAgecSHqgnW7lbUr_.js.xhtml?ln=omnifaces.combined&v=1558274343370:2)
    at ga.tokenize (eNptjUsOgzAMRE9E2XMbEwwkcmxjJ6qQenh-i6qF1UhvnmbUYsYRAnqXloq2tle8kn_02wUxvKGswsjF_4qfnUapTpFPp1KJGRqCVWrpkrcMLB5MiNAeBX9HPX9n8HmAAgecSHqgnW7lbUr_.js.xhtml?ln=omnifaces.combined&v=1558274343370:2)
    at ga.compile (eNptjUsOgzAMRE9E2XMbEwwkcmxjJ6qQenh-i6qF1UhvnmbUYsYRAnqXloq2tle8kn_02wUxvKGswsjF_4qfnUapTpFPp1KJGRqCVWrpkrcMLB5MiNAeBX9HPX9n8HmAAgecSHqgnW7lbUr_.js.xhtml?ln=omnifaces.combined&v=1558274343370:2)
    at ga.select (eNptjUsOgzAMRE9E2XMbEwwkcmxjJ6qQenh-i6qF1UhvnmbUYsYRAnqXloq2tle8kn_02wUxvKGswsjF_4qfnUapTpFPp1KJGRqCVWrpkrcMLB5MiNAeBX9HPX9n8HmAAgecSHqgnW7lbUr_.js.xhtml?ln=omnifaces.combined&v=1558274343370:2)
    at ga (eNptjUsOgzAMRE9E2XMbEwwkcmxjJ6qQenh-i6qF1UhvnmbUYsYRAnqXloq2tle8kn_02wUxvKGswsjF_4qfnUapTpFPp1KJGRqCVWrpkrcMLB5MiNAeBX9HPX9n8HmAAgecSHqgnW7lbUr_.js.xhtml?ln=omnifaces.combined&v=1558274343370:2)
    at Function.ga.matches (eNptjUsOgzAMRE9E2XMbEwwkcmxjJ6qQenh-i6qF1UhvnmbUYsYRAnqXloq2tle8kn_02wUxvKGswsjF_4qfnUapTpFPp1KJGRqCVWrpkrcMLB5MiNAeBX9HPX9n8HmAAgecSHqgnW7lbUr_.js.xhtml?ln=omnifaces.combined&v=1558274343370:2)
    at Function.r.filter (eNptjUsOgzAMRE9E2XMbEwwkcmxjJ6qQenh-i6qF1UhvnmbUYsYRAnqXloq2tle8kn_02wUxvKGswsjF_4qfnUapTpFPp1KJGRqCVWrpkrcMLB5MiNAeBX9HPX9n8HmAAgecSHqgnW7lbUr_.js.xhtml?ln=omnifaces.combined&v=1558274343370:2)
    at E (eNptjUsOgzAMRE9E2XMbEwwkcmxjJ6qQenh-i6qF1UhvnmbUYsYRAnqXloq2tle8kn_02wUxvKGswsjF_4qfnUapTpFPp1KJGRqCVWrpkrcMLB5MiNAeBX9HPX9n8HmAAgecSHqgnW7lbUr_.js.xhtml?ln=omnifaces.combined&v=1558274343370:2)
    at r.fn.init.filter (eNptjUsOgzAMRE9E2XMbEwwkcmxjJ6qQenh-i6qF1UhvnmbUYsYRAnqXloq2tle8kn_02wUxvKGswsjF_4qfnUapTpFPp1KJGRqCVWrpkrcMLB5MiNAeBX9HPX9n8HmAAgecSHqgnW7lbUr_.js.xhtml?ln=omnifaces.combined&v=1558274343370:2)
    at c.setLabel (eNptjUsOgzAMRE9E2XMbEwwkcmxjJ6qQenh-i6qF1UhvnmbUYsYRAnqXloq2tle8kn_02wUxvKGswsjF_4qfnUapTpFPp1KJGRqCVWrpkrcMLB5MiNAeBX9HPX9n8HmAAgecSHqgnW7lbUr_.js.xhtml?ln=omnifaces.combined&v=1558274343370:38)

Verified as fixed in PrimeFaces 7.0.

However, it may also be fixed in 6.2.X depending on which fix was applied.

6.2.9: https://github.com/primefaces/primefaces/issues/3982

7.0: https://github.com/primefaces/primefaces/issues/3466

It appears this isse was reintroduced in PrimeFaces 10.0.0 and fixed in version 10.0.9:

10.0: https://github.com/primefaces/primefaces/issues/7878

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM