使用 UTF8 的 Web 应用程序
Webapp with UTF8
我正在开发 webapp,但无法正确设置编码。现在,我的 web.xml
中有过滤器
<filter>
<filter-name>encodingFilter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
<init-param>
<param-name>forceEncoding</param-name>
<param-value>true</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>encodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
我还在 pom.xml
中将 UTF-8
编码添加到 maven-resources-plugin
和 maven-compiler-plugin
中。插入 jsp
文件中的 textarea
:
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ page language="java" pageEncoding="UTF-8"%>
...
<form:form method="post">
<div class="row">
<form:textarea class="form-control" id="newPost" rows="3" placeholder="Text nového příspěvku..." maxlength="1000" path="content"></form:textarea>
</div>
<div class="row contentOnRight">
<button class="btn btn-primary mt-1">Odeslat</button>
</div>
</form:form>
但是当我从此表单发送文本(例如“ššš”)时,在我的控制器中,我得到 Å¡Å¡Å¡
。
控制器
@RequestMapping(value = "/mainPage", method = RequestMethod.POST)
public String postMainPage(HttpServletRequest req, Authentication authentication, @ModelAttribute("post") Post post, ModelMap modelMap){
try {
//this doesn't help too
req.setCharacterEncoding("UTF-8");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
//ModelAndView modelAndView = new ModelAndView("mainPage", "command", new Post());
try {
postManager.postNew(post, authentication.getName());
}
catch (PostValidationException e) {
modelMap.addAttribute(ERROR_ATTRIBUTE, e.getMessage());
//modelAndView.addObject(ERROR_ATTRIBUTE, e.getMessage());
e.printStackTrace();
}
return "redirect:/mainPage";
}
我还尝试使用 -Dfile.encoding=UTF8
参数将 war 文件部署到 Tomcat7 中。但还是没有变化。我做错了什么?
默认情况下,Servlet 编码为 ISO-8859-1。将此参数添加到 Tomcat 可以解决您的问题:
-Djavax.servlet.request.encoding=UTF-8
我正在开发 webapp,但无法正确设置编码。现在,我的 web.xml
<filter>
<filter-name>encodingFilter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
<init-param>
<param-name>forceEncoding</param-name>
<param-value>true</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>encodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
我还在 pom.xml
中将 UTF-8
编码添加到 maven-resources-plugin
和 maven-compiler-plugin
中。插入 jsp
文件中的 textarea
:
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ page language="java" pageEncoding="UTF-8"%>
...
<form:form method="post">
<div class="row">
<form:textarea class="form-control" id="newPost" rows="3" placeholder="Text nového příspěvku..." maxlength="1000" path="content"></form:textarea>
</div>
<div class="row contentOnRight">
<button class="btn btn-primary mt-1">Odeslat</button>
</div>
</form:form>
但是当我从此表单发送文本(例如“ššš”)时,在我的控制器中,我得到 Å¡Å¡Å¡
。
控制器
@RequestMapping(value = "/mainPage", method = RequestMethod.POST)
public String postMainPage(HttpServletRequest req, Authentication authentication, @ModelAttribute("post") Post post, ModelMap modelMap){
try {
//this doesn't help too
req.setCharacterEncoding("UTF-8");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
//ModelAndView modelAndView = new ModelAndView("mainPage", "command", new Post());
try {
postManager.postNew(post, authentication.getName());
}
catch (PostValidationException e) {
modelMap.addAttribute(ERROR_ATTRIBUTE, e.getMessage());
//modelAndView.addObject(ERROR_ATTRIBUTE, e.getMessage());
e.printStackTrace();
}
return "redirect:/mainPage";
}
我还尝试使用 -Dfile.encoding=UTF8
参数将 war 文件部署到 Tomcat7 中。但还是没有变化。我做错了什么?
默认情况下,Servlet 编码为 ISO-8859-1。将此参数添加到 Tomcat 可以解决您的问题:
-Djavax.servlet.request.encoding=UTF-8