FAQ

[Struts-user] Unicode Encoding request parameters

Li Ying
Sep 25, 2011 at 4:27 am
You don't need an extra-filter.

The S2 default encoding is UTF-8.

The code of S2 filter is:

org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter:
public void doFilter(......) {
......
prepare.setEncodingAndLocale(request, response);
......
}

and the code of PrepareOperations is :
org.apache.struts2.dispatcher.ng.PrepareOperations
public void setEncodingAndLocale(HttpServletRequest request,
HttpServletResponse response) {
dispatcher.prepare(request, response);
}

and the code of Dispatcher is:
org.apache.struts2.dispatcher.Dispatcher

public void prepare(HttpServletRequest request,
HttpServletResponse response) {
String encoding = null;
if (defaultEncoding != null) {
encoding = defaultEncoding;
}

......

if (encoding != null) {
try {
request.setCharacterEncoding(encoding);
} catch (Exception e) {
LOG.error("Error setting character encoding to '" +
encoding + "' - ignoring.", e);
}
}
......
}

and the code of property [defaultEncoding] is:
@Inject(StrutsConstants.STRUTS_I18N_ENCODING)
public void setDefaultEncoding(String val) {
defaultEncoding = val;
}

the value of [defaultEncoding] will be injected by the property
[StrutsConstants.STRUTS_I18N_ENCODING]
which is "struts.i18n.encoding".

And this property is defined in [struts-default.properties], which's
value is [UTF-8]:
struts.i18n.encoding=UTF-8

You can read the following S2 documents:
http://struts.apache.org/2.x/docs/strutsproperties.html

If you want to change the default encoding, you can change the value
of property [struts.i18n.encoding].


By the way.
This encoding setting only works for the POST params (which are part
of the form element).
For the GET params(which are part of URL), the encoding can not be set
in J2EE context.
It will be parsed by the J2EE container (e.g: Tomcat) BEFORE your code
is executed.

In this case, the solution are:
(1)do not use non-ascii chars in URL params
or
(2)change the Tomcat setting about url param encoding: [URIEncoding].
It is descriped in this document:
http://tomcat.apache.org/tomcat-6.0-doc/config/http.html


2011/9/20 Zoran Avtarovski <zoran@sparecreative.com>:
We have a struts2 multilingual web app that requires request parameters be
encoded as Unicode (UTF-8).

In order to get the POST parameters to work we had to implement a character
encoding filter to encode all HTML parameters as UTF-8.

I'd like to know if it's possible to do the encoding via S2 and avoid the
use of another filter. We'e set the struts.i18n.encoding property to UTF-8
and I don't know what else to do.

As always I'd appreciate any suggestions you guys might have.

Z.


---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org
reply

Search Discussions

Discussion Posts

Previous

Related Discussions

Discussion Navigation
viewthread | post
posts ‹ prev | 2 of 2 | next ›

2 users in discussion

Zoran Avtarovski: 1 post Li Ying: 1 post