gpt4 book ai didi

spring boot + thymeleaf 无法正确编码 POST 形式的 UTF-8 字符

转载 作者:行者123 更新时间:2023-12-02 08:33:11 27 4
gpt4 key购买 nike

我正在使用 thymeleaf 模板引擎评估 Spring Boot。我面临UTF8字符形式发帖问题。

对于最简单的 form.html 帖子示例:

<head>
<title>test</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
</head>

<body>

<form th:action="@{/test/doPost}" method="post">
<input type="text" name="name" />
<button type="submit">submit</button>
</form>

在 Controller 中:

@Controller
@RequestMapping("/test")
public class TestController {

private Logger logger = LoggerFactory.getLogger(getClass());

@RequestMapping(value = "/form", method = RequestMethod.GET)
public String showForm() {
return "test/form";
}

@RequestMapping(value = "/doPost", method = RequestMethod.POST)
public String doPost(@RequestBody String body,
@RequestParam(value = "name", required = true) String name) {
logger.info("body = {}", body);
logger.info("name = {}", name);
return "redirect:/test/form";
}
}

在此示例中,Spring MVC(或 thymeleaf)无法使用 UTF-8 字符正确编码表单。

如果我输入 ä ,我将看到此日志:

TestController - body = name=%C3%83%C2%A4
TestController - name = ä

ä 应编码为 %C3%A4 。正文应该是 name=%C3%A4 ,但我不知道为什么它变成 %C3%83%C2%A4 并解码为错误的字符。

我用谷歌搜索并找到了一些解决方案,例如添加编码过滤器:

  @Bean
public Filter characterEncodingFilter() {
CharacterEncodingFilter characterEncodingFilter = new CharacterEncodingFilter();
characterEncodingFilter.setEncoding("UTF-8");
characterEncodingFilter.setForceEncoding(true);
return characterEncodingFilter;
}

但不工作。

另一个解决方案是通过这种方式 thymeleafViewResolver.setCharacterEncoding("UTF-8"); :

  @Bean
public SpringResourceTemplateResolver templateResolver() {
final SpringResourceTemplateResolver templateResolver = new SpringResourceTemplateResolver();
templateResolver.setCacheable(false);
templateResolver.setPrefix("classpath:/templates/");
templateResolver.setSuffix(".html");
templateResolver.setTemplateMode("HTML5");
return templateResolver;
}

@Bean
public SpringTemplateEngine springTemplateEngine() {
SpringTemplateEngine springTemplateEngine = new SpringTemplateEngine();
springTemplateEngine.setTemplateResolver(templateResolver());

return springTemplateEngine;
}

@Bean
public ThymeleafViewResolver viewResolver() {
ThymeleafViewResolver thymeleafViewResolver = new ThymeleafViewResolver();
thymeleafViewResolver.setTemplateEngine(springTemplateEngine());
thymeleafViewResolver.setCharacterEncoding("UTF-8");
return thymeleafViewResolver;
}

但这也不起作用。

此外,ThymeleafViewResolver的characterEncoding已经在ThymeleafAutoConfiguration source code中设置。 。这里似乎没有必要重新定义UTF-8

看来我对这个问题也有类似的情况:UTF-8 encoding with form post and Spring Controller 。可接受的解决方案是

Moving CharacterEncodingFilter to the top and forcing the encoding to be set as UTF-8 solved the problem.

我尝试过这个:

  @Bean
public FilterRegistrationBean filterRegistrationBean() {
FilterRegistrationBean registrationBean = new FilterRegistrationBean();
CharacterEncodingFilter characterEncodingFilter = new CharacterEncodingFilter();
characterEncodingFilter.setEncoding("UTF-8");
characterEncodingFilter.setForceEncoding(true);
registrationBean.setFilter(characterEncodingFilter);
registrationBean.setOrder(0);
return registrationBean;
}

但仍然无法工作。

我认为 UTF-8 表单发布是非常基本的,但为什么它在这里不起作用。我错过了什么吗?

环境:

<springboot.version>1.3.0.M5</springboot.version>
<spring.version>4.2.1.RELEASE</spring.version>

非常感谢。

有点题外话:我使用过很多其他框架,例如 JSP/wicket/grails/play1/play2 ,但没有一个有如此烦人的 UTF-8 情况。

最佳答案

This is a known issue, already fixed - 该修复将随 Spring Boot 1.3.0.RC1 一起提供。

同时,您可以通过手动声明编码过滤器来解决该问题:

@Autowired
private HttpEncodingProperties httpEncodingProperties;

@Bean
public OrderedCharacterEncodingFilter characterEncodingFilter() {
OrderedCharacterEncodingFilter filter = new OrderedCharacterEncodingFilter();
filter.setEncoding(this.httpEncodingProperties.getCharset().name());
filter.setForceEncoding(this.httpEncodingProperties.isForce());
filter.setOrder(Ordered.HIGHEST_PRECEDENCE);
return filter;
}

关于spring boot + thymeleaf 无法正确编码 POST 形式的 UTF-8 字符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33108990/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com