Problem with accent when saving text to the database

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, accenting works normally, but when updating the text by the system, the accent does not work. Here is the code for xhtml and manageBean:

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.

    
asked by anonymous 27.04.2017 / 15:15

3 answers

0

I was able to solve the problem. I put acceptcharset="ISO-8859-1" inside <h:form> , it looks like this:

    
27.04.2017 / 22:27
1

Not necessarily the problem is the bank. I'd bet it is not.

It seems to me that XHTML does not explicitly define what encoding; It's good to see that.

A lot can interfere with the text encoding 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 in the HTML headers, for example ( link )
  • the reverse proxy if you are using one (see options like charset in NGinx and AddDefaultCharset in Apache)
  • the OS on which the server is running
  • configuring the database connection
  • the configuration of Postgres itself (see link )
  • the configuration of the user of the OS that started the service (seriously: I already saw errors of type "only works right when so and so restart")

etc.

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

I suggest, if possible, always keep the same coding at all. Not required: you can have a form sending UTF-8 to a server that receives in ISO (conversion) which in turn writes to 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, spell out the encoding at each point. Moving only on Postgres can resolve, but it may also not result in anything - it can set the default encoding for clients, but if the Connection Pool is set to use another, it will ask to change and the database will obey. >     

27.04.2017 / 18:30
0

Use compatible client-encoding and server-encoding.

See the manual:

link

    
27.04.2017 / 16:11