dasLort
dasLort

Reputation: 1294

Primefaces update destroys Umlauts

edit: tl;dr: When saving Umlauts, they get corrupted (ä turns into ä). The rest of my question didn't really have anything to do with the problem as I now realized.

.

I'm building a webapp via JBoss, Hibernate, Infinispan Cache, derby, Maven and Primefaces.

I display a page that fetches data from a database, which has correct data in it (with umlauts). It is displayed correctly in a <p:dataTable id="dt1" var="as" value="#{aSBean.elementList}" ...>

There is a dialog popping up when one selects an entry from the table. The main part of the dialog code is

<p:dialog header="AS Detail" widgetVar="asDialog" resizable="false" id="asDlg"
          showEffect="fade" hideEffect="fade" modal="true" styleClass="detailDialog" >  
        <h:panelGrid id="display" >  

            <h:outputText value="Bemerkung" />  
            <h:inputText value="#{aSBean.selectedElement.bemerkungTxt}" />
            <h:outputText value="Bearbeiter" />  
            <h:outputText value="#{fehlerBean.selectedElement.bearbeiterNr}" />  

        </h:panelGrid>  
        <h:panelGrid id="diaBtnDisplay">  
            <p:commandButton value="Speichern" update=":form1:dt1" id="save"   validateClient="true" actionListener="#{aSBean.save}"/>
            <p:commandButton value="Abbrechen" id="cancel">
                <f:ajax event="click" onevent="asDlg.hide()" />
            </p:commandButton>          
        </h:panelGrid>  

</p:dialog>

Now, there isn't even an update attribute in the Abbrechen-CommandButton but still the dataTable gets updated when I press this button. It does not if I leave the dialog via the X in the upper right corner.

But the moment I press Abbrechen, the dataTable gets updated and my ä turns into ä. But it will only do so for the selected element. Here is some piece of my backing bean code:

public Arbeitsschluessel selectedElement = new Arbeitsschluessel();
public Arbeitsschluessel newElement = new Arbeitsschluessel();

public Arbeitsschluessel getSelectedElement() {
    return selectedElement;
}

public void setSelectedElement(Arbeitsschluessel selectedValue) {
    if (selectedValue != null) {
        this.selectedElement = selectedValue;
    }
}

public List<Arbeitsschluessel> getElementList() {
        return elementList;
    }

so definately nothing special. My HTML page starts with <?xml version="1.0" encoding="UTF-8"?> and I also had the following included <meta http-equiv="content-type" content="text/html; charset=utf-8" />

I debugged the update process after pressing the Abbrechen button and for my n-th element, the content of the as var was wrong. The callstack looks the same every time, so I cannot say at what exact point the value gets corrupted.

If I reload the datatable via a button (dao.findAll from database), everything is again displayed correctly, except ofc when I saved a wrong value into the database. So it is not that the database values are corrupted. Any help appreciated!

Edit: Code to opening the dialog:

<p:commandButton id="selectButton" update=":form1:display" oncomplete="PF('asDialog').show()" icon="" title="View"> 
                    <f:setPropertyActionListener value="#{as}" target="#{aSBean.selectedElement}" />
</p:commandButton>

Upvotes: 4

Views: 1190

Answers (1)

dasLort
dasLort

Reputation: 1294

I found the answer. One has to use a CharacterEncodingFilter

import java.io.IOException;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;

public class CharacterEncodingFilter implements Filter {

    @Override
    public void init(FilterConfig filterConfig) throws ServletException {   }

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain)
            throws IOException, ServletException {
        servletRequest.setCharacterEncoding("UTF-8");
        servletResponse.setContentType("text/html; charset=UTF-8");
        filterChain.doFilter(servletRequest, servletResponse);
    }

    @Override
    public void destroy() {  }
}

and then add the following lines to the web.xml in the WEB-INF folder:

<filter>
    <filter-name>CharacterEncodingFilter</filter-name>
    <filter-class>your.package.CharacterEncodingFilter</filter-class>
</filter>
<filter-mapping>
    <filter-name>CharacterEncodingFilter</filter-name>
    <url-pattern>/*</url-pattern>
</filter-mapping>

Upvotes: 3

Related Questions