春天mvc charset

时间:2012-02-16 21:33:30

标签: java spring spring-mvc character-encoding

我正在使用spring mvc。我用希伯来语写作,所以在我的jsp表格中,我要求用户用希伯来语给出他的名字。这是表格:

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@taglib uri="http://www.springframework.org/tags/form" prefix="form"%>
<form:form method="post" action="registration-submit.html">
    אנא מלא את הפרטים הבאים בכדי להשלים את הרשמתך:
    <table>
        <tr>
            <td>שם:</td>
            <td><form:input path="name" type="text" id="nameInput"/></td>
        </tr>
        <tr>
            <td>סיסמא:</td>
            <td><form:input path="password" type="password" id="passwordInput"/></td>
        </tr>
        <tr>
            <td>וידוא סיסמא:</td>
            <td><input type="password" id="passwordVerifyInput"/></td>
        </tr>
        <tr>
            <td>דואר אלקטרוני:</td>
            <td><form:input path="email" type="text" id="emailInput"/></td>
        </tr>
        <tr>
            <td colspan="2">
                <input type="submit" value="הירשם"/>
            </td>
        </tr>
    </table>
</form:form>

对于registration-submit.html我有控制器:

@RequestMapping(value = "/registration-submit", method = RequestMethod.POST)
    public ModelAndView showRegistrationSubmitPage(
            @ModelAttribute("registrationForm") final RegistrationForm registrationForm,
            BindingResult result) {
                ...
                ...
}

当我向手表添加registrationForm.name时,我看到:×××ר(不可读的字符)而不是希伯来名字。
jsp文件使用UTF-8字符集保存。为什么我从表单中获得不可读的字符?我怎样才能设置弹簧的字符集?

2 个答案:

答案 0 :(得分:2)

尝试在web.xml中注册CharacterEncodingFilter

答案 1 :(得分:2)

你需要做这样的事情

 <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-name>encodingFilter</filter-name> 

<url-pattern>/*</url-pattern>