使用Spring MVC编码问题 [英] Encoding problem using Spring MVC

查看:55
本文介绍了使用Spring MVC编码问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个创建用户的演示Web应用程序.当我尝试以其他语言(例如法语)插入数据时,字符编码不正确.控制器上的代码为:

I have a demo web application that creates users. When I try to insert data in other languages (like french) the characters are not encoded correctly. The code on the controller is:

@SuppressWarnings("unchecked")
    @RequestMapping(value = "/user/create.htm", params={"id"}, method = RequestMethod.GET)
    public String edit(@RequestParam("id") Long id, ModelMap model) {
        System.out.println("id is " + id);
        User user = userService.get(id);

        model.put("user", user);
        return "user/create";
    }

    @RequestMapping(value = "/user/create.htm", method = RequestMethod.POST)
    public String save(@ModelAttribute("user") User user, BindingResult result) {

        System.out.println(user.getFirstName());
        System.out.println(user.getLastName());


        validator.validate(user, result);
        if(result.hasErrors()) {
            return "user/create"; 
        }

        userService.save(user);
        return "redirect:list.htm";
    }

我的web.xml是:

my web.xml is:

...

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

...

,页面为:

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %>
<%@ taglib prefix="spring" uri="http://www.springframework.org/tags" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">

...

<form:form method="post" commandName="user">

...

<form:input path="firstName" cssErrorClass="form-error-field"/>

...

当我在名字中输入一些法语字符时,system.out.println的输出为????+?????或类似的内容.

when I enter some french characters in the first name then the output from the system.out.println is ????+????? or something similar.

我看到其他人使用CharacterEncodingFilter修复了此问题,但这似乎不起作用.

I saw other people fixing this with the CharacterEncodingFilter but this doesn't seem to work.

非常感谢.

编辑过滤器值.

推荐答案

尝试使CharacterEncodingFilter成为web.xml中的第一个过滤器.

Try making CharacterEncodingFilter the first filter in web.xml.

我意识到这个问题有点老了,但是我遇到了同样的问题,移动CharacterEncodingFilter为我解决了这个问题.

I realize this question is a little old, but I just ran into the same problem, and moving CharacterEncodingFilter fixed it for me.

这篇关于使用Spring MVC编码问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆