spring thymeleaf 中文乱码

userList.html
[code="java"]
<!DOCTYPE html>




用户名:
name age sex
go userList



[/code]

dispatcher-servlet.xml
[code="java"]
class="org.thymeleaf.templateresolver.ServletContextTemplateResolver">





<bean id="templateEngine" class="org.thymeleaf.spring3.SpringTemplateEngine">
    <property name="templateResolver" ref="templateResolver" />
</bean>

<bean class="org.thymeleaf.spring3.view.ThymeleafViewResolver">
    <property name="templateEngine" ref="templateEngine" />
    <property name="order" value="1" />
    <property name="viewNames" value="thymeleafe/*" />
</bean> 

[/code]

ThymeleafeControler
[code="java"]
package com.cn.ld.modules.thymeleaf;

import java.util.List;

@RequestMapping("/thymeleafe/")
@Controller
public class ThymeleafeControler {

@Autowired
private UserService userService;

@RequestMapping("userList")
public String goSerchUserH(HttpServletResponse response) {
    return "userList";
}

@ModelAttribute("allUsers")
public List<User> getAllUsers(){
    return this.userService.getUserList();
}

@RequestMapping("save")
public void save(User user){
    this.userService.addUser(user);
}

@RequestMapping("remove")
public void save(String name){
    User u = this.userService.findByName(name);
    this.userService.removeUser(u);
}

}

[/code]

web.xml
[code="java"]
<!-- 字符集 过滤器 -->



CharacterEncodingFilter

org.springframework.web.filter.CharacterEncodingFilter



encoding

UTF-8





forceEncoding

true



<filter-mapping>  
    <filter-name>CharacterEncodingFilter</filter-name>  
    <url-pattern>/*</url-pattern> 
    <dispatcher>REQUEST</dispatcher>
    <dispatcher>FORWARD</dispatcher> 
</filter-mapping> 

[/code]

pom.xml
[code="java"]
<!-- thymeleaf -->

org.thymeleaf
thymeleaf-spring3
2.0.17

 <dependency>
    <groupId>org.thymeleaf</groupId>
    <artifactId>thymeleaf</artifactId>
    <version>2.0.17</version>
 </dependency>

[/code]

[img]http://dl2.iteye.com/upload/attachment/0086/6304/9fc69b75-4a27-3ed3-b262-77fd356fc4f0.png[/img]

iteye_6961
iteye_6961 Mail 问题给了 thymeleafe team , 得到了及时的反馈。ViewResolve 默认的编码是ISO-8859-1,所以给ViewResolve 指定了<property name="characterEncoding" value="utf-8" /> 就ok了, 那为什么ServletContextTemplateResolver characterEncoding 没好使呢,其实不是没好使,翻看源码可以看到,ServletContextTemplateResolver 依赖一个write 和一个parse 模板,就是用来转化请求页面,这个characterEncoding 实际是指定 html 页面静态内容的编码。 ViewResolve characterEncoding 则指定的是动态内容的编码。
7 年多之前 回复

2个回答

我是看org.thymeleaf.spring3.view.ThymeleafViewResolver源码里也有个characterEncoding属性,所以在配置文件里设置了 ,页面中文乱码问题解决了。网上有关thymeleaf中文乱码问题贴,仅此一篇!

iteye_6961
iteye_6961 谢谢,该问题我已经解决,忘记了关掉了,谢谢回答。
7 年多之前 回复

楼上正解,在ThymeleafViewResolver中加入后,HTML上的乱码解决了

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐