使用UTF8的Webapp

时间:2018-01-12 18:30:13

标签: java hibernate spring-mvc utf-8 tomcat7

我正在使用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>

我还在我UTF-8的{​​{1}}和maven-resources-plugin中添加maven-compiler-plugin编码。插入pom.xml文件中的textarea

jsp

但是当我从这个表单发送文本时(例如“ššš”),在我的控制器中,我得到<%@ 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"; } 参数将war文件部署到Tomcat7中。但仍然没有变化。我做得不好?

1 个答案:

答案 0 :(得分:0)

默认情况下,Servlet编码为ISO-8859-1。将此arg添加到Tomcat可以解决您的问题:

-Djavax.servlet.request.encoding=UTF-8