Problem with accentuation when saving text in the database

Asked

Viewed 814 times

0

Good morning guys, I have the following problem. My system makes changes to a table in the database, if the content is inserted directly by script in postgres, the accent works normally, but when updating the text by the system, the accent does not work. Follow xhtml and manageBean code:


XHTML

<!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:ui="http://java.sun.com/jsf/facelets" xmlns:h="http://java.sun.com/jsf/html"     xmlns:f="http://java.sun.com/jsf/core" xmlns:p="http://primefaces.org/ui"> <h:body>     <ui:composition template="/pages/protected/templates/master.xhtml">         <ui:define name="divMain">      <h:form>        <h2 style="padding-left:10px;">Customização da Certidão de Dívida Ativa</h2>        <table>             <tr>
                <td>
                    <h:outputText value="Título do Relatório: " style="margin-left:5px;"/>
                </td>
                <td>
                    <p:inputTextarea rows="1"
                                label="dsTitulo" style="width:1000px;"
                                counterTemplate="{0} caracteres restantes."
                                id="dsTitulo"
                                value="#{div_customizacao_certidao_divida_ativaMB.dsTitulo}"
                                required="true" />
                </td>           </tr>           <tr>
                <td>
                    <p:commandButton action="#{div_customizacao_certidao_divida_ativaMB.update()}"  
                              value="Salvar" ajax="false">
                    </p:commandButton>
                </td>           </tr>   
                    </table>            </h:form>                   </ui:define>    </ui:composition> </h:body> </html>

MB

public void update() {
    try {
        certidaoDividaAtivaCustomizacao.setDescricaoTitulo(getDsTitulo());
        getCertidaoDividaAtivaCustomizacaoFacade().update(certidaoDividaAtivaCustomizacao);
        displayInfoMessageToUser("Registro salvo com sucesso!");
        certidaoDividaAtivaCustomizacao = null;
        loadCertidaoDividaAtivaCustomizacao();
    } catch (RollbackException ce) {
        ce.printStackTrace();
        try {
            Exception ex = ce;
            while (!(ex instanceof BatchUpdateException)) {
                ex = (Exception) ex.getCause();
            }
        } catch (Exception e) {
            displayErrorMessageToUser(ce.getMessage());
        }
    } catch (Exception e) {
        displayErrorMessageToUser(e.getMessage());
        e.printStackTrace();
    }

}

As shown in the figure below, you are using UTF-8 encoding. inserir a descrição da imagem aqui

3 answers

1

Not necessarily the problem is the bank. I would bet that it is not.

It seems to me that XHTML does not explicitly define which encoding; it is good to review this.

A lot can interfere in the encoding of the text between what the user types and what the DBMS writes to the disk. For example:

  • the encoding in which the file was saved or compiled (the server can use this to deduce which encoding to use)
  • some server configuration - it may be forcing a specific encoding on HTML headers, for example (https://wiki.apache.org/tomcat/FAQ/CharacterEncoding)
  • the reverse proxy, if using one (see options like charset in Nginx and Adddefaultcharset in Apache)
  • the OS on which the server is running
  • the database connection configuration
  • the configuration of Postgres itself (see https://www.postgresql.org/docs/current/static/multibyte.html)
  • the OS user configuration that started the service (seriously: I’ve seen errors like "only works right when so restarts")

etc..

The problem usually is: someone in the middle of the queue where the information passes (in cases where I saw: browser-proxy-application-pool-bd) receives a text, for example in ISO, wrongly supposes that the next in the queue is waiting for a UTF-8, and gently does the conversion, messing everything up.

I suggest, if possible, always keep the same coding in everything. Not required: you can have a form sending in UTF-8 to a server that receives in ISO (conversion) which in turn writes in UTF-8 (conversion) in a connection to a BD that was created as ISO (conversion).

For the sake of your sanity, do not do this; but if you do, explain the coding at each point. Tampering only with Postgres can solve, but it can also result in nothing - it can set the default encoding of clients, but if the Connection Pool is configured to use another, it will ask to exchange and the bank will obey.

  • Vlw for the answer Uriel, quite complete. I had Debbugando the code and the problem is the value that comes from the screen, because as soon as I click on save, goes to the UPDATE method, and the item already pulls the value of the converted screen. I just haven’t been able to solve the problem yet. Have I tried changing from <?xml version='1.0' encoding='utf-8' ? > to <?xml version='1.0' encoding='ISO-8859-1' ? > but it didn’t work either. The database is set right. I have other screens in the system that update or insert values in the bank and have no problem with accentuation, but for some reason this screen is not saved properly.

0

  • Vlw for the help.

0


I managed to solve the problem. I put acceptcharset="ISO-8859-1" inside the <h:form>, was like this: inserir a descrição da imagem aqui

Browser other questions tagged

You are not signed in. Login or sign up in order to post.