简体   繁体   中英

spring boot @RequestParam issue with UTF-8

I have spring boot application, as front I using thymeleaf,

in controller I have

@GetMapping(value = "/edit" )
public String getEdit(HttpServletRequest request, HttpServletResponse response,Model model,
        @RequestParam("empName") String empName
        ){
    System.out.println(empName);
       }

and

@PostMapping(value = "/edit")
public String postEdit(HttpServletRequest request, HttpServletResponse response,@Valid @ModelAttribute("emp") Employees emp,BindingResult bindingResult, Model model,@RequestParam("file") MultipartFile file,
        @RequestParam("empName") String empName
        ){

            return "redirect:/edit?empName="+empName;
        }

empName has utf-8 content and if i press link from front then System.out.println(empName); has correct result and if i do redirect from post then result is "????"

i tried everywhere in this controller put utf-8 as tried

URLEncoder.encode(url, StandardCharsets.UTF_8)

URLEncoder.encode(url, "UTF_8")

but no correct result,

anyone can help?

添加字符集:

@GetMapping(value = "/edit", produces = "application/json; charset=UTF-8")

You just need to add this properties to the application.properties file: For Spring Boot 1.x

# Charset of HTTP requests and responses. Added to the "Content-Type" 
# header if not set explicitly.
spring.http.encoding.charset=UTF-8
# Enable http encoding support.
spring.http.encoding.enabled=true
# Force the encoding to the configured charset on HTTP requests and responses.
spring.http.encoding.force=true

Source: https://docs.spring.io/spring-boot/docs/1.5.22.RELEASE/reference/html/common-application-properties.html

For Spring Boot 2.x

server.servlet.encoding.charset=UTF-8
server.servlet.encoding.force-response=true

Source: https://docs.spring.io/spring-boot/docs/current/reference/html/appendix-application-properties.html#server.servlet.encoding.charset

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM