How to solve encoding problems when using strange

2019-08-20 23:18发布

I am in trouble with encoding values. Well I am developing a web page with spanish content, and it uses tittles like ó,á,è, etc, or characters like ñ. Then when I pressed the button to save the values, I guess the ajax event for a button didn't take the encodig, them I add encoding='ISO-8859-1', and it worked. But I have an autocomplete for countries in primefaces that also use an ajax event to proccess information, and for this field the encoding doesn't work and before to add encoding='ISO-8859-1' it worked. Well when solve one, the other one failed, and vice versa.

Happen that I need the country to consult the states and list them.

Web code:

<p:autoComplete id="pais" value="#{personal.pais}"   
                    completeMethod="#{personal.listPaises}" forceSelection="true" required="true" effect="fade" scrollHeight="400" 
                    var="p"  itemLabel="#{p}" itemValue="#{p}" requiredMessage="Es necesario seleccionar país" label="País" validator="#{personal.validatePaises}" >
                     <p:column style="width:80%" >  
                      #{p} 
                      </p:column>
                    <p:ajax event="itemSelect" update="departamento" />  
                 </p:autoComplete>

Java Code:

public void setPais(String pais) {
        int codPais = pDao.getPaisCod(pais); 
        departamentosList = pDao.listDepatamentosByPais(codPais);
        this.pais = pais;
    }

For example if I choose España as Country in jsf form, in the bean is taken as Espa±a.

I need unify the encodig.

Thanks a lot.

2条回答
beautiful°
2楼-- · 2019-08-21 00:04

I know that my problem was with ajax request of autocomplete and commandButton.Then I tried everything and didn't work. well I solve my problem changing my jsf commandButton to Primefaces commandButton.

Before

<h:commandButton value="submit" action="Periodico?faces-redirect=true" actionListener="#{personal.insertarUsuario}" />

After

<p:commandButton value="submit" action="Periodico?faces-redirect=true" actionListener="#{personal.insertarUsuario}" />

And so I solve my problem. Thanks a lot for your time and answers

查看更多
放荡不羁爱自由
3楼-- · 2019-08-21 00:21

You should just use UTF-8, it will support any character and more importantly it is the only encoding that everything has in common. Often something will only work with UTF-8, such as many JSON implementations. And when that isn't the case, JSON cannot support ISO-8859-1 anyways.

For instance, primeface's Ajax uses jQuery.param, which uses encodeURIComponent, which uses URL encoding that is based on UTF-8.

So if you want to unify encoding, UTF-8 is your only option.

Btw, by "use UTF-8", I don't mean just to put UTF-8 in a random place that seems right but actually ensure UTF-8 is the declared and physical encoding everywhere in your project.

查看更多
登录 后发表回答