1

I am working on a Spring JSR 286 Portlet application. The locale is driven by a default language setting that is loaded upon login.

I then need to do currency formatting through the portlet. For example, I do the following:

<fmt:setLocale value="en_IE" />
result: <fmt:formatNumber type="currency" value="324.00" currencyCode="EUR" />

(Normally, all these values are set dynamically.)

This produces a fine result as I go through the normal operation.

result: €324.00 

Except, in the application, when I do a @ResourceMapping request. In this case, and here only, I get:

result: ?324.00

I've got the same two lines in the jsp's that render for RenderRequests and in the jsp's that render for ResourceRequests. But regardless of what I do, it always fails to produce clean output as part of a ResourceRequest.

Any ideas as to what could be causing the inconsistency?


2nd EDIT:

Following on +BalusC 's 2nd comment, I put the following into the jsp:

<% System.out.println("Response Char Encoding: " + response.getCharacterEncoding());  %>

For regular requests where, it works, I get:

SystemOut O Response Char Encoding: UTF-8

For Render Requests, where it doesn't work, I get:

SystemOut O Response Char Encoding: ISO-8859-1

So, yes, as predicted, this is a character encoding issue.

--

EDIT 3:

So, the issue is how to set a character encoding of UTF-8 in Spring on a ResourceResponse.

Putting in a page directive of <%@page pageEncoding="UTF-8"%> doesn't override the default encoding seen above. The methods for building this directly into Spring are complicated hacks (see Spring Encoding with CharacterEncodingFilter in web.xml ), and given that I have only a handful of Resource Request methods, it seems the easiest way is to just put

response.setCharacterEncoding("UTF-8");

into those methods.

Would appreciate any other suggestions, especially if I'm wrong about how to more easily put this into Spring.

Community
  • 1
  • 1
Mike Ryan
  • 4,234
  • 1
  • 19
  • 22
  • Question marks instead of "special characters" typically indicate a character encoding problem. A question mark in means in your particular example that the desired character is not covered by the character encoding the response is been told to use. You should try to verify if all possible character encoding settings are set to the one and same encoding, which should be UTF-8 these days. I can't post a more detailed answer how to configure it as I don't do Portlets. – BalusC May 24 '12 at 23:11
  • 1
    As per your edit, you should be interested in the **response** character encoding, not the request one. Thus, so `response.getCharacterEncoding()`. For JSPs in normal servlet environments you can control this value by placing `<%@page pageEncoding="UTF-8"%>` in top of JSP (or by an application-wide `web.xml` setting). But again, I'm not sure how portlets play a role. – BalusC May 28 '12 at 20:29
  • @BalusC Thanks -- dumb mistake on my part to switch request and response. And the result is as expected. Now trying to see if there is a "Spring" way to set character encoding on responses, or just put it in the method directly. – Mike Ryan May 29 '12 at 16:20
  • So `<%@page pageEncoding="UTF-8"%>` has solved the problem? And you need a way to apply this on all JSPs? Do portlets have a `web.xml`? – BalusC May 29 '12 at 16:23
  • @BalusC The `<%@ page` directive didn't work. It's a small enough use case that I'm just going to hardcode it -- not great, but it's very much a side issue at this client. See Edit above. – Mike Ryan May 29 '12 at 19:50

1 Answers1

0

My final resolution to this was in Edit #3 above. To just hardcode

response.setCharacterEncoding("UTF-8");

into the 2 ResourceResponse methods.

If you have more extensive resource requests and need a comprehensive solution, follow the links in the question.

Mike Ryan
  • 4,234
  • 1
  • 19
  • 22