Domanda

So basically my problem is that I have an ArrayList of Strings containing words with accents on some of letters. When I get the values of this ArrayList to display as a dropdown menu on the xhtml, I get the values correctly with the accents and everything, however, when I try to submit the string I've selected, I get this error: Validation Error: Value is not valid.

The encoding of the xhtml page is this: <?xml version='1.0' encoding='UTF-8' ?>.

When I select a word that has no accents, everything works as planned.

What am I supposed to do to fix this problem? Thanks.

Edit: As requested, here's the code :

<?xml version='1.0' encoding='UTF-8' ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:h="http://java.sun.com/jsf/html"
      xmlns:p="http://primefaces.org/ui"
      xmlns:c="http://java.sun.com/jsp/jstl/core"
      xmlns:f="http://java.sun.com/jsf/core">
    <h:head>
        <h:outputStylesheet library="css" name="tablestyle.css"  />
        <h:outputStylesheet library="css" name="pagestyle.css"  />
        <h:outputScript library="js" name="common.js" />
        <h:outputScript library="js" name="jquery-ui-1.10.3.custom.js"/>  
        <h:outputScript library="js" name="jquery-ui-1.10.3.custom.min.js"/>  
        <h:outputScript library="js" name="jquery-1.9.1.js" />
    </h:head>
    <h:body> 
        <div id="menu">
            <c:if test="#{SfpExposerBean.dataToRepresent == 'HelpRequest'}" >
                <h:form id="assistancerequest">
                    <p:poll interval="10" update="assistancerequest" /> 
                    <br/><br/>
                    <h:selectOneMenu value="${SfpExposerBean.entrPlat}" style="width: 100px" >
                        &nbsp;&nbsp;&nbsp;
                        <h:outputText value="Plat origem:" />&nbsp;&nbsp;
                        <c:forEach items="${SfpExposerBean.plats}" var="entrplat">
                            <f:selectItem itemValue="${entrplat}" />
                        </c:forEach>
                    </h:selectOneMenu>
                    <h:commandButton value="Ok" action="#{SfpExposerBean.setHelpRequests()}" style="width: 200px" id="submitAssistance" />
                </h:form>
            </c:if>
        </div>
        <div id="collapse" onclick="toggle();" />
   </h:body>
</html>
È stato utile?

Soluzione

When submitting, it is checking if the current request is an ajax request. Instead of request header the request parameter identifies it. For the first time the request parameter which is retrieved before the JSF view getting restored, will parse using servers default character encoding (ISO-8859-1) instead of JSF's own default character encoding (UTF-8).

This can be fixed by :

-> Setting ServletRequest#setCharacterEncoding() with UTF-8. Setting the response encoding by ServletResponse#setCharacterEncoding() is by the way unnecessary as it won't be affected by this issue.

@WebFilter("*.xhtml")
public class CharacterEncodingFilter implements Filter {
@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws ServletException, IOException {
    request.setCharacterEncoding("UTF-8");
    chain.doFilter(request, response);
  }
}

see Typing Chinese with PrimeFaces' editor component.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top