【问题标题】:Spring form and controller UTF-8 bad encodingSpring 表单和控制器 UTF-8 错误编码
【发布时间】:2013-07-24 13:28:35
【问题描述】:

我的 UTF-8 编码有问题。我的 webapp 使用在我的 jsp 中正确显示的法语单词,但在 POST 之后不在我的控制器中。 例如,在我的 jsp 中,我有:

Prénom de mon père

当我发布表单时,控制器得到:

Prénom de mon pére

characterEncodingFilter 是文件中的第一个过滤器,如this post 中所述

这是我的jsp:

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<!doctype html>
<%@ taglib uri="http://tiles.apache.org/tags-tiles"         prefix="tiles" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core"          prefix="c" %>
<%@ taglib uri="http://www.springframework.org/tags/form"   prefix="form" %>

<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta http-equiv="content-language" content="fr">
    ...
    </head>

    <form:form class="form-horizontal" modelAttribute="AlimentationForm"
    action="${actionUrl}" method="POST">
    ...
         <form:input path="questions" class='input-xlarge' type='text' value='Prénom de mon père'/>
    </form>

我的 web.xml:

<filter>
    <filter-name>characterEncodingFilter</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>characterEncodingFilter</filter-name>
    <url-pattern>/web/*</url-pattern>
</filter-mapping>

还有我的 application-config.xml

<beans:bean id="messageSource" class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
    <beans:property name="basename">
        <beans:value>classpath:messages</beans:value>
    </beans:property>
    <beans:property name="defaultEncoding" value="UTF-8" />
</beans:bean>

我不知道我的应用或配置出了什么问题,你知道吗?

提前致谢。

编辑:我正在使用 HDIV 框架

【问题讨论】:

  • 添加“" 在 jsp 上?
  • 完成了,但我没有提到它。我已经编辑了我的帖子

标签: post spring-mvc utf-8 character-encoding hdiv


【解决方案1】:

这个问题实际上来自 HDIV 框架版本 2.1.2。如果您无法使用下一个版本,可以使用补丁 here

希望对某人有所帮助。

【讨论】:

    【解决方案2】:

    确保 JSP 页面正确编码发送到服务器的数据。尝试添加

    <%@page pageEncoding="UTF-8" %>
    

    到您的 JSP 的顶部,因为这样可以确保通过 http 发送的任何数据都将正确编码。

    我相信默认的 http 编码是 ISO-8859-1。

    【讨论】:

    • 这一行已经在我的jsp文件中了。如何更改 post 上的默认 http 编码?
    • 在您编辑之前不是这样!通常使用普通表单,您可以执行类似
      not sure with a spring forms tag 之类的操作。
    猜你喜欢
    • 2016-09-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-08-21
    • 1970-01-01
    相关资源
    最近更新 更多