2015-04-23 45 views
4

我有土耳其字符...在我的JSP頁面的麻煩,有沒有問題......但是,當警報從Java端來,土耳其字符(ŞşİığĞüÜç...)好像即(A±,?,那張,§,A,...)字符編碼的Spring MVC

在JSP頁面中,我用這個代碼,我可以在Spring MVC配置解決土耳其人品問題

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

,我嘗試了很多方法,但我沒有成功...例如在我的mvc配置類中,我設置了我的MessageSource。

@Bean 
public MessageSource messageSource() { 
    ReloadableResourceBundleMessageSource messageSource = new ReloadableResourceBundleMessageSource(); 
    messageSource.setBasename("classpath:messages"); 
    messageSource.setUseCodeAsDefaultMessage(true); 
    messageSource.setDefaultEncoding("UTF-8"); 
    messageSource.setCacheSeconds(0); 
    return messageSource; 
} 

在這個程序中,我嘗試重置密碼,我輸入註銷電子郵件address..Finally我得到的例外,這下面是異常代碼博客。

@Autowired 
private MessageSource messages; 
... 

@ExceptionHandler({ UserNotFoundException.class }) 
public ResponseEntity<Object> handleUserNotFound(final RuntimeException exception, final WebRequest request) { 
    logger.error("404 Status Code", exception); 
    final GenericResponse bodyOfResponse = new GenericResponse(messages.getMessage("message.userNotFound", null, request.getLocale()), "UserNotFound"); 
    return handleExceptionInternal(exception, bodyOfResponse, new HttpHeaders(), HttpStatus.NOT_FOUND, request); 
} 

在我messages_tr_TR.properties文件,

... 
message.userNotFound=Kullanıcı Bulunamadı 
... 

但在JSP頁面此警報顯示那樣;

KullanıCA±Bulunamadı

我該如何解決這個問題..

+0

用編碼有幾種常見的原因,你可以嘗試添加過濾器和配置servlet容器:// stackoverflow.com/questions/28831145/polish-character-encoding-issue-in-ajax-call/28831361#28831361)。 [Here](http://balusc.blogspot.com/2009/05/unicode-how-to-get-characters-right.html)你可以找到一個常見問題的綜合列表,檢查第二點,如果第一個鏈接不起作用 –

+0

Hi @MasterSlave,感謝您的評論。我已經嘗試第二個鏈接之前......在第一個環節,有很多的方法,但我正在尋找Spring MVC的一個......我該如何解決Spring MVC中這個問題 – hsnclk

+0

我的賬戶只是增加一個答案格式化的,但實際上它只是另一個建議,讓我知道,如果它幫助 –

回答

1

評論跟進,你可以設置你的響應頭的編碼也是如此。如果你返回json一個例子是

HttpHeaders responseHeaders = new HttpHeaders(); 
responseHeaders.add("Content-Type", "application/json; charset=utf-8"); 
return handleExceptionInternal(exception, responseHeaders, HttpStatus.NOT_FOUND, request); 
+1

我試過,但它沒有工作:(我得到同樣的結果。 – hsnclk

1

在web.xml:

<jsp-config> 
    <!-- global JSP configuration --> 
    <jsp-property-group> 
     <url-pattern>*.jsp</url-pattern> 
     <page-encoding>UTF-8</page-encoding> 
    </jsp-property-group> 
</jsp-config> 

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

把實際文本的處理方法(臨時)。它現在看起來不錯嗎?您的郵件文件是否正確保存爲UTF-8?此外,作爲建議[這裏](HTTP我不能告訴,如果你使用JSON或不...

+0

感謝的答案,但它不工作...當我將這個添加到web.xml中,它只會影響一些信息消息......我的意思是,沒有必要在jsp頁面中使用此代碼博客了<%@ page contentType =「text/html; charset = UTF-8」語言=「java的」%> ....據我所知** message.userNotFound =KullanıcıBulunamadı**消息中使用不同的方式......我有麻煩只是一些信息消息不是所有的人。 – hsnclk

+0

順便說一句,我不太明白「你的郵件文件是否正確保存爲UTF-8?」你是什麼意思? – hsnclk