Haozhun
Haozhun

Reputation: 6511

Stop Strut2 from automatically converting non-ascii characters to unicode value

Suppose name contains non-ascii characters, when I use <s:property value="name" />, Struts automatically converts those characters to things like &#20165;.

This is completely unnecessary as I declares

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

and

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

and

struts.i18n.encoding=UTF-8

It makes the html source completely unreadable on client side while increasing the size of the page tremendously.

I know <s:property value="name" escapeHtml="false" /> could solve my problem. But that's not the point. This option is intended to be used when I don't want escapeHtml at all. What I want is escape Html properly while leaving UTF-8 characters alone! I guess setting a property somewhere should solve the problem, but where? I searched for quite a long time and can't find a solution.

Upvotes: 1

Views: 2354

Answers (1)

Steven Benitez
Steven Benitez

Reputation: 11055

This is something specific to the <s:property/> tag. You can use <c:out/> instead, which will escape XML characters without altering the Unicode characters. Try this:

<c:out value="${action.name}"/>

This is the JSTL equivalent of <s:property value="name"/>.

Upvotes: 3

Related Questions