Spring MVC - fetching wrong message bundle - java

I have a strange problem using Spring MVC message bundles: the wrong message bundle file is being fetched. I have double-checked, and in my Java controller class I have the fr_FR locale, but Spring tags (appContext.getMessage(code, null, locale); in the class as well) return me English messages!
What is going on?
I am developing portlets for Liferay Portal. Let me show you parts of my code:
in applicationContext.xml:
<bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource">
<property name="basenames">
<list>
<value>messages</value>
</list>
</property>
</bean>
in my JSPs I have code looking like this:
...
<spring:message code="button.help"/>
...
and the paths to my messages look like this:
development:
/src/main/webapp/WEB-INF/classes/messages.properties (English, default)
/src/main/webapp/WEB-INF/classes/messages_fr.properties
deployed in Tomcat
/webapps/MY_APP/WEB-INF/classes/messages.properties
/webapps/MY_APP/WEB-INF/classes/messages_fr.properties

Try setting the language (to fr_FR ) in your browser. In firefox version that I use, its at
Edit ->Preferences -> Content -> Langauges
And use the move-up or move-down buttons to have fr_FR as the top preference.
This makes the browser send requests with the prefered locale set.

I had a similar issue when my localized message file had a bad unicode character (the
\uXXX form).
Neither Java, Spring nor Tomcat did write an information about it, no feedback at all.
After fixing the file, everything suddenly started to work.
Regards

Frist you have to make sure that liferay supports the locale you want. In addition you have to add your supported locales in the portlet.xml for each portlet.
portlet.xml:
<portlet>
...
<supported-locale>en</supported-locale>
<supported-locale>fr</supported-locale>
...
</portlet>
If you miss one of both it just won't work. Check if you added the supported-locale for this portlet as well.

You must have an interceptor in applicationContext like
<mvc:interceptors>
<bean class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor">
<property name="paramName" value="lang" />
</bean>
</mvc:interceptors>
You also need
<bean id="messageSource" class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
<property name="basename" value="classpath:messages" />
<property name="defaultEncoding" value="UTF-8" />
</bean>
<bean id="localeResolver"
class="org.springframework.web.servlet.i18n.SessionLocaleResolver"/>
On the first JSP page of my project I gave a choice for languages like that:
<span style="float: right"> en | fr </span>
Please tell me if this helped you.
P.S. My messages_*.properties files are in a source folder src/main/resources, not in webapp, I don't know if this matters.
P.P.S. Useful tutorial here:
http://springbyexample.org/examples/basic-webapp-internationalization.html
I would like to add also that at the beginning of your xml you should have stuff like:
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:tx="http://www.springframework.org/schema/tx" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p" xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:util="http://www.springframework.org/schema/util"
xmlns:security="http://www.springframework.org/schema/security"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
http://www.springframework.org/schema/util
http://www.springframework.org/schema/util/spring-util-3.0.xsd
http://www.springframework.org/schema/security
http://www.springframework.org/schema/security/spring-security-3.0.xsd">
It is necessary in order to recognize the prefixes such as mvc. Make sure you have it.

Related

upgrading Apache Camel 2 to 3, where did Main.setApplicationContextUri() method go?

I have an old codebase that uses camel 2.something. We have to upgrade to camel 3.14 for Nexus-flagged security issues. I'm going through the Upgrade Camel 2 to 3 docs. I see that Main moved packages, which I did, but now I'm getting an error that Main.setApplicationContextUri("camel-context.xml") is no longer a method on Main. I can't find it, nor a replacement in the upgrade docs.
What am I missing?
Also, if there's a way to do a simple camel config via beans and annotations instead of xml, I'd love a pointer to that. I'm also running into an issue parsing the camel-context.xml, so if I can get rid of that whole mess, I'd love to.
We are using Java 8 so I think the latest that works with that is Camel 3.14.6 so that's what I'm trying.
Here's our camel-context.xml:
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
xmlns:util="http://www.springframework.org/schema/util" xmlns:cxf="http://cxf.apache.org/core"
xmlns:jaxws="http://cxf.apache.org/jaxws" xmlns:http="http://cxf.apache.org/transports/http/configuration"
xmlns:sec="http://cxf.apache.org/configuration/security" xmlns:camel="http://camel.apache.org/schema/spring"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
http://cxf.apache.org/core http://cxf.apache.org/schemas/core.xsd
http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd
http://cxf.apache.org/transports/http/configuration http://cxf.apache.org/schemas/configuration/http-conf.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd
http://camel.apache.org/schema/spring http://camel.apache.org/schema/spring/camel-spring.xsd
">
<!-- ================================== -->
<!-- Define camel context -->
<!-- ================================== -->
<camel:camelContext id="camel" trace="false">
<camel:propertyPlaceholder id="queriesConfig" location="lib/queries.properties" />
<!-- Location for route scan -->
<camel:package>com.blah.listener</camel:package>
</camel:camelContext>
<!-- configure the Camel SQL component to use the JDBC data source -->
<bean id="sqlComponent" class="org.apache.camel.component.sql.SqlComponent">
<property name="dataSource" ref="dataSource" />
</bean>
<bean id="bean1" class="com.foo.OurThing" />
<bean id="bean2" class="com.bar.OtherThing" />
Edit: Just found this. Are we supposed to convert camel-context.xml to application.properties? We also have an app-context.xml for basic Spring configuration, and there's a reference to the camel-context.xml there. I wonder if we replace camel-context.xml with application.properties, what to do with this reference? Just remove it, or change it?

primary attribute not allowed inside bean tag

I have tried to set the default validation message file to messages.properties file. It worked. However, for some reason the primary attribute inside validator bean is marked red in the applicationContext.xml file.
The error message, "Attribute primary is not allowed here".
I have tried to find the reason of this in many resources and cross-checked many possibilities, but everything seems fine with my configuration.
I use Intellij Idea, Spring version - 3.2.17 RELEASE, Hibernate version - 3.6.10.Final.
Can anyone explain the reason ?
applicationContext.xml -
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
https://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/mvc
https://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/context
https://www.springframework.org/schema/context/spring-context.xsd"
default-autowire="byName">
<context:component-scan base-package="com.test.leavemanagement"/>
<bean id="messageSource" class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
<property name="basename" value="classpath:messages"/>
</bean>
<mvc:annotation-driven validator="validator"/>
<bean id="validator" class="org.springframework.validation.beanvalidation.LocalValidatorFactoryBean" primary="true">
<property name="validationMessageSource" ref="messageSource"/>
</bean>
</beans>

Error occur when I start project that using DWR3.0 RC2 with Spring

I introduce DWR into my project these days. My project is using Spring 4, I was trying to integrate DWR with Spring by using annotation approch. Below are my code for integration work.
An error shown as below occured when I stratup my project on the Tomcat server.
the error:
java.lang.NoSuchMethodError: org.springframework.util.ClassUtils.forName(Ljava/lang/String;)Ljava/lang/Class;
at org.directwebremoting.spring.DwrAnnotationPostProcessor.getBeanDefinitionClass(DwrAnnotationPostProcessor.java:96)
at org.directwebremoting.spring.DwrAnnotationPostProcessor.postProcessBeanFactory(DwrAnnotationPostProcessor.java:52)
at org.springframework.context.support.PostProcessorRegistrationDelegate.invokeBeanFactoryPos
Processors(PostProcessorRegistrationDelegate.java:265)
at org.springframework.context.support.PostProcessorRegistrationDelegate.invokeBeanFactoryPostProcessors(PostProcessorRegistrationDelegate.java:177)
at org.springframework.context.support.AbstractApplicationContext.invokeBeanFactoryPostProcessors(AbstractApplicationContext.java:609)
web.xml
<servlet>
<servlet-name>dwr-dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>2</load-on-startup>
</servlet>
dwr-dispatcher-servlet.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:dwr="http://www.directwebremoting.org/schema/spring-dwr"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.0.xsd
http://www.directwebremoting.org/schema/spring-dwr
http://www.directwebremoting.org/schema/spring-dwr-3.0.xsd">
<context:annotation-config />
<context:component-scan base-package="com.xdfaint.webapp.apps.*.action" />
<dwr:configuration />
<dwr:annotation-config id="dwrAnnotationConfig" />
<dwr:url-mapping />
<dwr:controller id="dwrController" debug="true" />
<bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping">
<property name="order" value="1" />
</bean>
<bean class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping">
<property name="order" value="2" />
</bean>
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix">
<value>/core/jsp/</value>
</property>
<property name="suffix">
<value>.jsp</value>
</property>
</bean>
</beans>
I haven't found any solution yet. Then I try to use Spring 3.2 instead, the startup successed with no error any more. I am not sure if there are problems when Spring 4 meets DWR3. Can any body help? Thx~
DWR haven't updated for a long time, and I am not using it anymore. But it is still great helpful that worth to read source codes for studying, like concept of how rest api binding, object operating. About object operating, I found some useful functions like setObject(), was using massively when I did a frontend components encapsulation work. Both setObject function codes and using demo list below, with it I can init a filed in a any nested level of the object, even if there are number of parents object of that new field hasn't been declared yet.
In Spring 4 there is no method forName in ClassUtils that only accepts String as an argument. As you can see from the JavaDoc here that method was already deprecated in Spring 3
It seems that DWR has not yet upgraded their code base to support Spring 4. Seems like you'll have to wait for that to happen (although the project seems to be non-active so I wouldn't count on that happening any time soon).

Spring XWSS Message Signing

I am currently working on a client interface which connects to a third party web service.
This 3rd party web service requires that all messages sent to them are signed with the client's private key.
I am attempting to implement this using Spring's XWSS support as documented here:
http://docs.spring.io/spring-ws/site/reference/html/security.html
The issue I'm facing is that the messages I send out are not being signed despite what as far as I can tell is a correct configuration.
My applicationContext.xml is as follows:
<beans
xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:util="http://www.springframework.org/schema/util"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://www.springframework.org/schema/util
http://www.springframework.org/schema/util/spring-util-3.1.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.1.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.1.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.1.xsd">
^
|
|
B
E
A
N
S
|
|
V
<bean id="wsSecurityInterceptor"
class="org.springframework.ws.soap.security.xwss.XwsSecurityInterceptor">
<property name="policyConfiguration" value="classpath:securityPolicy.xml"/>
<property name="callbackHandlers">
<list>
<ref bean="keyStoreHandler"/>
</list>
</property>
</bean>
<bean id="keyStoreHandler"
class="org.springframework.ws.soap.security.xwss.callback.KeyStoreCallbackHandler">
<property name="keyStore" ref="keyStore"/>
<property name="privateKeyPassword" value="ckpass"/>
</bean>
<bean id="keyStore"
class="org.springframework.ws.soap.security.support.KeyStoreFactoryBean">
<property name="location" value="file:///C:/path/to/security/clientKeystore.jks"/>
<property name="password" value="cspass"/>
</bean>
</beans>
My securityPolicy.xml consists of the following:
<xwss:SecurityConfiguration dumpMessages="true" xmlns:xwss="http://java.sun.com/xml/ns/xwss/config">
<xwss:Sign>
</xwss:Sign>
</xwss:SecurityConfiguration>
However there are no messages being dumped to standard output when I send messages out and the messages I send out do not contain the signature elements I would expect.
I suspect I am missing something quite trivial here however I cannot tell what that is for the life of me!
In your configuration you only configure the interceptor. Currently it takes up only memory just hanging around and doing nothing. You should hook this interceptor up to your WebServiceTemplate (or class that extends WebServiceGatewaySupport.
Assuming you have one of those you should have something like this.
<bean id="yourClient" class="YourClientClass">
<property name="interceptors" ref="wsSecurityInterceptor"/>
// Your other properties here
</bean>
This wired your interceptor to the WebServiceTemplate used, without it the interceptor is basically not used.

Pure HTML pages app using Spring MVC

How can I build a spring MVC app using pure html pages? When I deployed jsps on my tomcat 7 server, it is able to map the pages fine. But for html, the pages dont get displayed. I get 404 errors. How can I not use jsps in spring mvc? Please elaborate the steps I need to take.
Thank you in advance.
In Spring MVC all request goes through FrontController - DispatcherServlet
There you need to tell Spring to allowe jsp and html both in your case
Example
dispatcher-servlet.xml:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd">
<bean class="org.springframework.web.servlet.mvc.support.ControllerClassNameHandlerMapping"/>
<bean id="urlMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
<property name="mappings">
<props>
<prop key="index.htm">indexController</prop>
</props>
</property>
</bean>
<bean id="viewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver"
p:prefix="/WEB-INF/jsp/"
p:suffix=".jsp" />
<bean name="indexController"
class="org.springframework.web.servlet.mvc.ParameterizableViewController"
p:viewName="index" />
<bean name="/*.htm" class="controller.MyController"/>
</beans>
Thymeleaf will be your friend
http://www.thymeleaf.org/
please change the view resolver suffix to .html,so that the viewResolver can able to render your view.

Categories