Spring MVC: CharacterEncodingFilter; why only set response encoding by force?

前端 未结 2 1009
时光说笑
时光说笑 2021-02-09 19:46

I was having a look at the CharacterEncodingFilter provided by Spring MVC. I was wondering why it was only possible to set the response encoding when the request encoding was fo

2条回答
  •  伪装坚强ぢ
    2021-02-09 20:04

    I can tell you what Juergen Hoeller says on link https://jira.springsource.org/browse/SPR-3328?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel,

    Add following filter in web.xml (Servlet 2.4+) to set encoding :

    
            CharacterEncodingFilter
            org.springframework.web.filter.CharacterEncodingFilter
            
                encoding
                UTF-8
            
            
                forceEncoding
                true
            
        
    
        
         CharacterEncodingFilter
         /*
    
    

    EDIT :

    CharacterEncodingFilter : Current browsers typically do not set a character encoding even if specified in the HTML page or form. The above filter can either apply its encoding if the request does not already specify an encoding, or enforce this filter's encoding in any case("forceEncoding"="true"). If we strictly want to encode characters, we set it forcibly.

    1. why it was only possible to set the response encoding when the request encoding was forced to the given encoding?
    2. Why not be able to set a default response encoding if nothing is specified in the accept header fields? Or if no encoding was present in the request?

    I think Boris's link in comment will answer these questions.

提交回复
热议问题