开发者

Spring MVC: How to store € character?

开发者 https://www.devze.com 2023-01-07 15:43 出处:网络
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 t

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 '€'.

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?

Update:

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.

  • First is the encoding of the log4j appender.

    log4j.appender.emsSpringLogFile.encoding=UTF-8

Open the log file for example in notepad ++ and make sure the encoding is UTF-8.

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

  • 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" >

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

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


Are you sure it's not a problem with your logger configuration? Check it as follows:

logger.info("updateEvent(): isCorrect=" + 
    event.getDescription().contains("\u20ac")); 


Try adding the following to the head of your jsp file.

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %>

in the following question it solved the problem. In the same question, I added some reference to other issues related to text encoding.

0

精彩评论

暂无评论...
验证码 换一张
取 消