[stringtemplate-interest] UTF-8 not displaying correctly

Leo R. Lundgren leo at finalresort.org
Mon Mar 15 10:11:07 PDT 2010


Hi,

I am building a small servlet application using Eclipse, Tomcat 6, JRE  
1.6, ST 3.2. Here is a ViewHandler I'm using to wrap ST functionality:

	public class ViewHandler {
		private StringTemplateGroup templateGroup;
		private Map<String, String> attributes = new HashMap<String,  
String>();

		public ViewHandler(String viewBasePath) {
			templateGroup = new StringTemplateGroup("default", viewBasePath);
		}

		public void setAttribute(String name, String value) {
			attributes.put(name, value);
		}

		public String getOutput(String viewName) {
			StringTemplate view = templateGroup.getInstanceOf(viewName);
			view.setAttributes(attributes);
			return view.toString();
		}

		public void render(Writer out, String viewName) throws IOException {
			out.write(getOutput(viewName));
		}
	}

The handler is used like this in a servlet:

	protected void doGet(HttpServletRequest request, HttpServletResponse  
response) throws ServletException, IOException {
		super.doGet(request, response);
		
		String viewBasePath = getServletContext().getRealPath("/WEB-INF/ 
view");
		ViewHandler viewHandler = new ViewHandler(viewBasePath);
		viewHandler.setAttribute("fileName", "test.png");
		viewHandler.setAttribute("contentTemplate", "uploadFile");

		viewHandler.render(response.getWriter(), "index");
	}

It does what it is supposed to; The output I get is the contents of  
the index.st template, with attributes replaced like they should be,  
and the content template included as expected.

However, swedish characters such as åäö that are part of static  
strings in the template files are shown in the browser(s) as question  
marks. I know this indicates coding/charset problems. An example  
string (from the template files) that is not displayed correctly is:

	<input type="button" class="cancelUploadButton" value="Avbryt  
insättning">

The 'ä' in the last word becomes a question mark in the browser.


So, I have:
- Checked the encoding settings in Eclipse, in all places I can find  
that seem to relate to the source files and/or template files.
- Checked the encoding of the related template files (both in their  
properties and using an external editor that loads them fine as UTF-8).
- Verified that the HTTP response headers say UTF-8 as the charset.  
The same goes for the HTML code itself, it's UTF-8 all the way.

The only thing I haven't found to be apparently fine is when I open  
the .java files from my project using another editor (TextMate, which  
has always handled encodings fine for me); Normally TextMate displays  
the encoding used/discovered from loading the file (for the template  
files it says UTF-8), but for the Java source files it doesn't display  
anything.
However there are no static strings in the source files other than  
template names and attributes, so I'm not sure that would matter. But  
maybe it does, assuming there's something wrong with how the source  
files are saved by eclipse.

Can someone shed some light on this issue? As I see it I've got UTF-8  
everywhere (apart from possibly the Java source files, which I guess  
could be the issue), and it should work. But maybe I need to change  
something with regards to ST to have it work with UTF-8? If not, any  
other ideas?

Thank you,

// Leo



More information about the stringtemplate-interest mailing list