Spring MVC:如何存储€字符? [英] Spring MVC: How to store € character?

查看:104
本文介绍了Spring MVC:如何存储€字符?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Spring 3 MVC,并且已经设置了一个表单来捕获用户的输入.此表单在我的模型对象Event中包含一个用于描述字符串的文本区域.我相应的控制器如下所示:

I am using Spring 3 MVC and I have setup a form to capture input from a user. This form includes a textarea for a description String in my model object, Event. My corresponding controller looks like this:

 @RequestMapping(value = "/admin/event/{eventId}/edit", method = RequestMethod.POST) 
 public String updateEvent(@ModelAttribute Event event) {

         logger.info("updateEvent(): Event description: " + event.getDescription());

         return "redirect:/admin/event/" + event.getId() + "/edit";
} 

每当我在表单的描述字段中输入€"字符并过帐表单时,记录的描述就会带有?"而不是€".

Whenever I enter a '€' character into the form's description field and POST the form, the logged description has a '?' instead of a '€'.

我在DispatcherServlet的前面使用了CharacterEncodingFilter,但这不能解决问题.作为参考,我的web.xml如下所示:

I am using a CharacterEncodingFilter in front of my DispatcherServlet but this has not resolved the problem. For reference, my web.xml looks like this:

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

<servlet>
    <servlet-name>baseApp</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <load-on-startup>1</load-on-startup>
</servlet>

<servlet-mapping>
    <servlet-name>baseApp</servlet-name>
    <url-pattern>/*</url-pattern>
</servlet-mapping>

关于如何正确捕获€字符的任何想法?

Any thoughts on how to properly capture the € character?

更新:

我问过一个类似的问题,以前涉及MySQL,但是从上面的演示中可以看到,€转换为?.完全不涉及MySQL,即在POST表单和记录事件描述之间.这就是为什么我再次问这个问题的原因.似乎这个问题与Spring无关.

I've asked a similar question previously involving MySQL, but as you can see from the demonstration above, the € is transformed to a ? with no MySQL involvement at all, i.e. between POST'ing the form and logging the event's description. This is why I've asked the question again.. it seems this problem is isolated to Spring.

推荐答案

您可以在此处检查一些内容.

There is a few things you could check here.

  • 首先是log4j附加程序的编码.

    • First is the encoding of the log4j appender.

      log4j.appender.emsSpringLogFile.encoding = UTF-8

      • 第二个在firefox上使用诸如firebug之类的插件在net选项卡上检查发布请求和参数值.

        • Second is using a plugin like firebug on firefox inspect on the net tab the post request and the param values.

          第三步是将以下元信息添加到jsp文件的开头:

          Third is add the below meta information to the head of your jsp file:

             < meta http-equiv = "Content-Type" content = "text/html; charset=UTF-8" >
          

          像完成操作一样,将编码过滤器添加到web.xml中.

          Add the encoding filter into your web.xml like you have done.

          这适用于部署在websphere 6.1上的我的webapp. spring的版本是spring 3.0.6.

          This works for my webapp deployed on websphere 6.1. Version of spring is spring 3.0.6.

          这篇关于Spring MVC:如何存储€字符?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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