extending FilterDispatcher struts2 - utf-8

In my project code I can see FilterDispatcher being extended to set the default encoding as "UTF-8". My question is there any other better way we can do the same or this is the best way.
PS: The initial development was done in a country on Non-English windows PCs.

Actually there is struts.i18n.encoding constant which sets default locale and encoding scheme. You can set it in struts.properties file:
struts.i18n.encoding=UTF-8
or in struts.xml file:
<constant name="struts.i18n.encoding" value="UTF-8" />
BTW: struts.i18n.encoding should be set to UTF-8 by default in Struts2 anyway.
BTW no.2: FilterDispatcher is deprecated since Struts 2.1.3. So if you using version higher than that use StrutsPrepareAndExecuteFilter instead.

Related

context:property-placeholder comma-separated list of resources don't work with system property placeholder

I can't entirely configure property-placeholder from system property because I can't give comma-separated list of resources.
I'm trying to do like:
<context:property-placeholder location="${config-location}" />
I use system property to configure this. It works if I give one location only, like "classpath:main.properties", but it does not if I'm trying this: "classpath:main1.properties,classpath:main2.properties".
If I use this latter exact value directly in xml configuration it works fine. I guess it resolves comma-separation earlier than placeholders. It should be the other way around.
P.S : version 4.3.4
Another possible worth tryout would be,
<context:property-placeholder location="#{systemProperties['config-location']}" />
how to read System environment variable in Spring applicationContext

UTF-8 encoding issues with Spring resourcebundle for special characters

I am using Spring ResourceBundle to retrieve message bundle from my .properties file. It constains special european characters like so:
Ü, ß. ä, ö, ü
MessageSouce bean is as below (I am making sure UTF-8 encoding is followed)
<bean id="messageSource"
class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
<property name="basename" value="classpath:/META-INF/i18/messages" />
<property name="defaultEncoding" value="UTF-8" />
</bean>
When I try to retrieve the message in my Java code I am getting Junk characters.
If I use below code, it helps to recognize few characters, but rest are still showing as ??
return new String (bundleString.getBytes(), "UTF-8")
Then I used below to encode my properties file, but still no effect
native2ascii -encoding utf8 resources.utf8 resources.properties
I also tried to manually open my properties file in Notepad++ and set UTF-8 encode, but no effect.
I see a post here, having exact same problem as mine. But the solution uses PropertiesBundle, whereas I have to use Spring based solution only. However even the accepted answer in that link is not working for me, and giving junk characters.
Please suggest any possible solution.
I was having the same issue and #sunny_dev's answer worked for me.
I don't understand why there is not answer for this question yet so I'm updating the question.
#sunny_dev answer:
Karol, I solved the problem by opening and saving the .properties file
in TextPad as "UTF-8" encoding and "Unix" platform. I had taken that
same approach, however in Notepad++ without the positive outcome
earlier. – sunny_dev
Thanks again to #sunny_dev

How to retrieve context parameters in Spring 3.1 xml context

It seems like there's been a few iterations of property support in spring it's hard to tell what's best practice and the manuals are written from the point of view of someone who is familiar with every other iteration. I feel like this should be a simple and common requirement but given how hard it's been please correct me if there's a more idiomatic way.
What I want is to pass an additional properties file to my spring web app based on a context property which the client is setting using a tomcat descriptor like so
<Context path="/foo" reloadable="true">
<Parameter name="foo.config" value="file:${catalina.base}/conf/foo.properties"/>
</Context>
In spring for the live profile I have this
<beans profile="live">
<context:property-placeholder location="classpath:timetabling.live.properties,${timetabling.config}"
ignore-resource-not-found="true" />
</beans>
So I'd assumed this doesn;t work because I'm trying to configure placeholder suppport with a placeholder. If I use a system property however then this works fine. I know that spring 3.1 has baked in support for system and environment properties so I guess my question is how can I augment this support with something context aware before the placeholder is resolved?
--Update--
looking at http://blog.springsource.org/2011/02/15/spring-3-1-m1-unified-property-management/ particularly at footnote 1, I would expect to have a DefaultWebEnvironment which should already have aceess to context init params. Now I am more confused, can someone provide me with a concrete example of context property retrieval? At this point I feel like I've read every javadoc available and they are just not helpful.
<context:property-placeholder /> sets up a PropertyPlaceholderConfigurer which reads from .properties, system properties and environment variables. A Tomcat context.xml however sets up a servlet context init parameter. So what you need is a ServletContextPropertyPlaceholderConfigurer.

Character Encoding Issue with spring application

I am building an application using spring mvc and jpa using jboss7 and mysql in eclipse ide. I am having a strange problem. All my jsp pages are encoded with charset: utf8, which I think is working correctly. But whenever I try to post a data from the jsp to the controller, my data gets encoded with a different encoding style. I tried to look for the header using firebug and was astonished to see that the post request has a header with content-type : "text/plain;charset=ISO-8859-1". I have already configured the SetCharacterEncodingFilter for UTF-8 in my web.xml (it is the first filter). But still the problem exists.
I also set "org.apache.catalina.connector.URI_ENCODING" to value="UTF-8".But in vain .
Also I have added bean messageSource with property defaultEncoding set to "UTF-8".
<bean id="messageSource" class="org.springframework.context.support.ReloadableResourceBundleMessageSource" >
The problem still exists. Please help
Thanks in advance.
The request header is set by the browser, so your application can't control it. Usually, in your HTML form you could put an accept-charset=utf-8 attribute to specify the encoding, but that doesn't necessarily work. See this question Setting the character encoding in form submit for Internet Explorer.
you should need to set the encoding of the JVM like follow :
-Dfile.encoding=UTF-8 -Dfile.io.encoding=UTF-8 -DjavaEncoding=UTF-8
thus there wont be any doubt at all.

Accessing proper resource bundle in Spring Framework

I am trying to access a resource bundle using Spring framework (WebFlow). A messages.properties file and accordingly messages_ar_AE.properties file are kept in the classpath from where the Spring Framework access the resource bundle.
The code in invoked from a xhtml file using the JSTL resourceBundle attribute.
<myCustom:includedInSetValidator set="5.0, 5.0.1, 5.1"
validationMessage="#{resourceBundle['jboss.version.error']}" />
But irrespective of locale, the "#{resourceBundle['jboss.version.error']}" always fetches the default text, i.e; from English;
As I learned from some forums I got an hint that I need to handle this using LocaleChangeInterceptor or some other predefined classes. Once the Spring Locale is set, the proper resource bundle will be loaded by default, and hence solving my problem.
I need a way to change the Spring Framework Locale programatically to set the Locale. How do I achieve this programatically ?
Thanks.
Reached the solution for the problem.
Continuing from my question, when Spring Framework encounters a JSTL expression like "#{resourceBundle['jboss.version.error']}" by default it looks for message.properties file in the classpath, unless a resource bundle is defined explicitly.
When trying to fetch the proper resource bundle, the framework looks at the at the locale it is set to. As the locale of Spring Framework was not set in my case, it was not fetching me the expected resource bundle. Out of available options i chose Spring LocaleResolver
I modified existing JSF Custom ViewHandler in my application, where I added code to set the locale of Spring Framework.
public Locale calculateLocale(FacesContext arg0) {
HttpServletRequest request = (HttpServletRequest)arg0.getExternalContext().getRequest();
HttpServletResponse response = (HttpServletResponse)arg0.getExternalContext().getResponse();
LocaleResolver localeResolver = RequestContextUtils.getLocaleResolver(request);
localeResolver.setLocale(request, response, **setYourLocaleHere**);
}
The story just doesn't end here, setting the locale in locale resolver this way would throw the error:
Cannot change HTTP accept header – use a different locale resolution strategy
Refer Cannot change HTTP accept header error
To overcome this, one should include
<bean id="localeResolver"
class="org.springframework.web.servlet.i18n.SessionLocaleResolver">
<property name="defaultLocale" value="en" />
</bean>
in the Spring configuration file.
And now the desired locale of the Spring Framework is set.
There could possibly a better solution than what I did. One can also suggest their solutions if any.
Thanks.
You could do this multiple ways as outlined here in the doc.

Resources