Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
784 views
in Technique[技术] by (71.8m points)

java - JSF, validateRegex and & (ampersand)

I want to test inputText field to see if its content matches anything but at least one & (ampersand).

When I test & or & or \& inside pattern field of validateRegex, I always received this error :

javax.faces.view.facelets.FaceletException: Error Parsing /index.xhtml: Error Traced[line: 71] Le nom de l'identité doit immédiatement suivre le caractère "&" dans la référence d'entité.

and I get 500 error...

How can I escape this character?


Update

I have tested

<p:inputText id="player_name_register" value="#{login.name}">
    <f:validateRegex pattern="[&amp;]{3, 50}" />
</p:inputText>

But when I test with &&&&&& it doesn't work.

I also tested

<![CDATA[
    <p:inputText id="player_name_register" value="#{login.name}">
        <f:validateRegex pattern="[&amp;]{3, 50}" />
    </p:inputText>
]]>

but my inputText doesn't appear anymore.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Facelets is a XML based view technology. The whole view has to be syntactically valid XML. XML special characters like &, < and > needs to be escaped as &amp;, &lt; and &gt; when they're supposed to be interpreted as-is.

<f:validateRegex pattern="...&amp;..." />

(here, the ... represents the remainder of your regex)

A CDATA block won't work as it basically escapes the entire content, including JSF components. Quoting the escaped XML character makes no sense. After being parsed by the Facelets XML parser, the &amp; becomes & again.


Update as per your update, the space in {3, 50} is causing the regex syntax error. Remove it.

<p:inputText id="player_name_register" value="#{login.name}">
    <f:validateRegex pattern="[&amp;]{3,50}" />
</p:inputText>

Using CDATA blocks around the JSF component is not the right solution at all. It would XML-escape the entire content, resulting in &lt;p:inputText&gt; being emitted plain vanilla instead of the component's HTML representation.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...