Spring Framework Localization always reads from en (English) file - spring

I have followed the tutorial below to set up localization for my web app.
http://viralpatel.net/blogs/spring-3-mvc-internationalization-i18n-localization-tutorial-example/
I have three properties files: messages_en_properties, messages_es.properties, and messages_zh_CN.properties
Each file has the following line:
loging.name=(Username translated to the corresponding language)
In my .jsp file I set my table data to display the username using the following line:
<td><spring:message code = "login.name" text = "Default Text"/></td>
This works, but it only will ever read from my English properties file. I have tried setting the locale manually in the .jsp file to zh_CN and es but I still receive the English file values. I have even tried setting the locale to a country for a file I do not have and it still displays the text from the English file, and not the default text. I am positive the locale is actually being changed since the browser will ask if I would like the page translated.
Here is the part of my code that sets up the localeChangeInterceptor.
<beans:bean id="localeChangeInterceptor"
class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor">
<beans:property name="paramName" value="lang" />
</beans:bean>
<beans:bean id="handlerMapping"
class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping">
<beans:property name="interceptors">
<beans:ref bean="localeChangeInterceptor" />
</beans:property>
</beans:bean>
Also tried changing to the following...
<mvc:interceptors>
<beans:bean id="localeChangeInterceptor"
class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor">
<beans:bean id="localeResolver"
class="org.springframework.web.servlet.i18n.CookieLocaleResolver">
<beans:property name="defaultLocale" value="en" />
</beans:bean>
<beans:bean id="handlerMapping"
class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping">
</beans:bean>
Any help would be greatly appreciated.

Surround
> <bean id="localeChangeInterceptor"
> class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor">
> <property name="paramName" value="lang" /> </bean>
with <mvc:interceptors>
as
<mvc:interceptors>
<bean class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor" >
<property name="paramName" value="lang" />
</bean>
</mvc:interceptors>

Related

Spring Security and custom Remember me filter: logout issue

I need to define a custom RememberMeAuthenticationFilter so that I can override the onSuccessfulAuthentication(HttpServletRequest request, HttpServletResponse response, Authentication authResult) method to put some custom logic.
I've configured the XML in order to use my custom filter:
<security:http disable-url-rewriting="true" request-matcher-ref="excludeUrlRequestMatcher" entry-point-ref="authenticationEntryPoint">
<security:custom-filter position="FORM_LOGIN_FILTER" ref="usernamePasswordAuthenticationFilter"/>
<security:custom-filter position="REMEMBER_ME_FILTER" ref="extRememberMeProcessingFilter"/>
<security:anonymous username="anonymous" granted-authority="ROLE_ANONYMOUS"/>
<security:session-management session-authentication-strategy-ref="fixation" />
<!-- Intercepts url HERE: removed for brevity -->
<!--<security:form-login: using custom filter -->
<!--login-page="/login"-->
<!--authentication-failure-handler-ref="loginAuthenticationFailureHandler"-->
<!--authentication-success-handler-ref="loginGuidAuthenticationSuccessHandler"/>-->
<security:logout logout-url="/logout" success-handler-ref="logoutSuccessHandler"/>
<security:port-mappings>
<security:port-mapping http="#{configurationService.configuration.getProperty('tomcat.http.port')}"
https="#{configurationService.configuration.getProperty('tomcat.ssl.port')}"/>
<security:port-mapping http="80" https="443"/>
<!--security:port-mapping http="#{configurationService.configuration.getProperty('proxy.http.port')}"
https="#{configurationService.configuration.getProperty('proxy.ssl.port')}" /-->
</security:port-mappings>
<security:request-cache ref="httpSessionRequestCache"/>
<security:access-denied-handler ref="b2bAccessDeniedHandler"/>
<!-- RememberMe: using custom filter -->
<!--<security:remember-me key="comtestrememberme" services-ref="rememberMeServices"/>-->
</security:http>
<security:authentication-manager alias="authenticationManager">
<security:authentication-provider ref="myAuthenticationProvider"/>
<security:authentication-provider ref="rememberMeAuthenticationProvider"/>
</security:authentication-manager>
<bean id="myAuthenticationProvider"
class="com.test.security.MyAuthenticationProvider">
<property name="bruteForceAttackCounter" ref="bruteForceAttackCounter"/>
<property name="customerService" ref="customerService"/>
<aop:scoped-proxy/>
</bean>
<bean id="rememberMeServices"
class="com.test.security.MyRememberMeServices">
<property name="key" value="comtestrememberme"/>
<property name="cookieName" value="myRememberMe"/>
<property name="alwaysRemember" value="false"/>
<property name="customerService" ref="customerService"/>
<property name="useSecureCookie" value="false"/>
<aop:scoped-proxy/>
</bean>
<bean id="rememberMeAuthenticationProvider"
class="org.springframework.security.authentication.RememberMeAuthenticationProvider">
<property name="key" value="comtestrememberme"/>
<aop:scoped-proxy/>
</bean>
<bean id="usernamePasswordAuthenticationFilter" class="org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter">
<property name="authenticationManager" ref="authenticationManager"/>
<property name="filterProcessesUrl" value="/j_spring_security_check"/>
<property name="rememberMeServices" ref="rememberMeServices"/>
<property name="authenticationSuccessHandler" ref="loginGuidAuthenticationSuccessHandler"/>
<property name="authenticationFailureHandler" ref="loginAuthenticationFailureHandler"/>
</bean>
<bean id="authenticationEntryPoint" class="org.springframework.security.web.authentication.LoginUrlAuthenticationEntryPoint">
<property name="loginFormUrl" value="/login"/>
</bean>
<bean id="extRememberMeProcessingFilter" class="com.test.security.filters.ExtRememberMeAuthenticationFilter">
<property name="rememberMeServices" ref="rememberMeServices"/>
<property name="authenticationManager" ref="authenticationManager"/>
</bean>
The remember me cookie is getting created and my custom filter is being used, but the problem is that the logout never happens.
When I click on the logout button it looks like I'm going through the authentication process again and the customer is logged in again.
If I revert back to the standard Spring Filters everything is working fine.
Have I missed something in the configuration?
What may be happening here is - your logout is working fine but you haven't deleted myRememberMe cookie on logout. So, when your session is getting invalidated on logout, remember me services is creating a new session by using myRememberMe cookie.
Solution: You can modify your configuration by adding delete-cookies attribute in your <security:logout> tag.
<security:logout logout-url="/logout" success-handler-ref="logoutSuccessHandler" delete-cookies="JSESSIONID,myRememberMe" />

Why isn't hibernate creating my tables from hbm.xmls?

I have several .hbm.xml mapping files set up in Spring, and I can tell from the logs that Spring sees them and does something with them, but no tables are created in the DB.
Bean setup:
<beans:bean id="dataSourceEmbedded" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<beans:property name="driverClassName" value="org.h2.Driver" />
<beans:property name="url" value="jdbc:h2:~/myDbName" />
<beans:property name="username" value="sa" />
<beans:property name="password" value="" />
</beans:bean>
<beans:bean id="sessionFactory"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"
p:dataSource-ref="dataSourceEmbedded">
<beans:property name="mappingLocations">
<beans:list>
<beans:value>classpath:path/to/schemas/Page.hbm.xml</beans:value>
<beans:value>classpath:path/to/schemas/Navigation.hbm.xml</beans:value>
</beans:list>
</beans:property>
<beans:property name="hibernateProperties">
<beans:value>
hibernate.show_sql=true
hibernate.dialect=${hibernate.dialect}
hibernate.hbmToDdlAuto=create
</beans:value>
</beans:property>
</beans:bean>
I know that the path to the Page and Navigation mapping files is correct - an error is thrown if it's not, and I can see the fields being mapped during the tomcat startup:
DEBUG: org.hibernate.cfg.Configuration - Processing hbm.xml files
INFO : org.hibernate.cfg.HbmBinder - Mapping class: Page -> pages
DEBUG: org.hibernate.cfg.HbmBinder - Mapped property: Page ID -> pageid
DEBUG: org.hibernate.cfg.HbmBinder - Mapped property: title -> title
Similarly, the database is clearly accessed / locked while tomcat is loading, so something is happening in there! But once it's loaded and I try to query it or inspect it, no tables exist.
Question: Why aren't my tables being created / what have I done wrong?
The property you used is wrong, it should be 'hibernate.hbm2ddl.auto' instead of 'hibernate.hbmToDdlAuto'... However instead of that you can also set the schemaUpdate property of the LocalSessionFactoryBean.
<beans:bean id="sessionFactory"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"
p:dataSource-ref="dataSourceEmbedded">
<beans:property name="mappingLocations">
<beans:list>
<beans:value>classpath:path/to/schemas/Page.hbm.xml</beans:value>
<beans:value>classpath:path/to/schemas/Navigation.hbm.xml</beans:value>
</beans:list>
</beans:property>
<beans:property name="schemaUpdate" value="true" />
<beans:property name="hibernateProperties">
<beans:value>
hibernate.show_sql=true
hibernate.dialect=${hibernate.dialect}
</beans:value>
</beans:property>
</beans:bean>
Related answer/info: Hibernate hbm2ddl.auto possible values and what they do?
Correct property name is hibernate.hbm2ddl.auto

What this running error means?

Hello, I`m trying to run a job in spring batch but its giving me this error which i dont know how to solve:
`$` C:\Program Files\Microsoft Visual Studio 8\VC>java -jar C:\Workspacetest\testpro
ject\target\ams-eod-0.0.1-SNAPSHOT.jar copyJob 1 `$`
Exception in thread "main" java.lang.NullPointerException
at sun.launcher.LauncherHelper.getMainClassFromJar(LauncherHelper.java:399)
at sun.launcher.LauncherHelper.checkAndLoadMain(LauncherHelper.java:463)
However this is my applicationcontext. can anyone help me regarding this error please?
few words to discribe what the job does, this job reads from a database some records and then it performs a calculation on each record (row * 3.75) and then update the record.
<?xml version="1.0" encoding="UTF-8"?>
<beans:beans xmlns="http://www.springframework.org/schema/batch"
xmlns:beans="http://www.springframework.org/schema/beans"
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.xsd
http://www.springframework.org/schema/batch
http://www.springframework.org/schema/batch/spring-batch-2.2.xsd">
<beans:bean id="itemReader" class="org.spr...JdbcCursorItemReader">
<beans:property name="dataSource" ref="dataSource"/>
<beans:property name="sql" value="select * from Trans WHERE id =?"/>
<beans:property name="rowMapper" ref= "CustomerCreditRowMapper"/>
<beans:property name="preparedStatementSetter" ref="idSetter"/>
</beans:bean>
<beans:bean id="transactionProcessor" class="">
<beans:property name="targetObject" ref="Processing"/>
<beans:property name="targetMethod" value="process"/>
</beans:bean>
<beans:bean id="jdbcWriter"
class="org.springframework.batch.item.database.JdbcBatchItemWriter">
<beans:property name="dataSource" ref="dataSource"/>
<beans:property name="sql" value="update Trans set (amount) values (?)"/>
<beans:property name="preparedStatementSetter" ref="idSetter"/>
</beans:bean>
<step id="CopyFileStep">
<tasklet>
<chunk reader="itemReader"
processor="transactionProcessor"
writer="jdbcWriter"
commit-interval="10"/>
</tasklet>
</step>
<job id="copyJob">
<step id="step1" parent="CopyFileStep"/>
</job>
<beans:bean id="preparedStatementSetter"
class="sa.com.anb.itg.dev.settlement.batch.Itemwriter"/>
<beans:bean id="idSetter" class="sa.com.anb.itg.dev.settlement.batch.idSetter"
scope="step">
<beans:property name="id" value="#{jobParameters[id]}"/>
</beans:bean>
<beans:bean id="CustomerCreditRowMapper"
class="sa.com.anb.itg.dev.settlement.batch.CustomerCreditRowMapper"/>
<beans:bean id="AmountToTransactionItemProcessor"
class="sa.com.anb.itg.dev.settlement.batch.AmountToTransactionItemProcessor">
<beans:property name="TransactionDAO" ref="TransactionDAO"/>
</beans:bean>
<beans:bean id="TransactionDAO" class="sa.com.anb.itg.dev.settlement.batch.TransactionDAO">
<beans:property name="datasource" ref="dataSource"/>
</beans:bean>
<beans:bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
<beans:property name="driverClassName" value="org.hsqldb.jdbcDriver" />
<beans:property name="url" value="jdbc:oracle:thin:#192.168.84.208:1556:pay10g" />
<beans:property name="username" value="ams_recon" />
<beans:property name="password" value="ams_recon" />
</beans:bean>
<beans:bean id="jobRepository"
class="org.springframework.batch.core.repository.support.JobRepositoryFactoryBean">
<beans:property name="dataSource" ref="dataSource"/>
<beans:property name="transactionManager" ref="transactionManager"/>
</beans:bean>
<beans:bean id="jobLauncher"
class="org.springframework.batch.core.launch.support.SimpleJobLauncher">
<beans:property name="jobRepository" ref="jobRepository" />
</beans:bean>
<beans:bean id="transactionManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager" lazy-init="true">
<beans:property name="dataSource" ref="dataSource"/>
</beans:bean>
</beans:beans>
Well, when launching an App you REALLY need a Main Class.
Spring Batch can provide one for you : CommandLineJobRunner.
In your example it seems that your are trying to execute your app like a regular one with the commandLine. Here Java try to locate you Manifest File because the JVM NEEDS a Main class and it's not able to find it in your JAR which cause your error.
It's not your app that you have to launch with the command line, it's the CommandLineJobRunner which provide a Main method able to launch your batch.
The documentation show how to use it : http://static.springsource.org/spring-batch/reference/html/configureJob.html#runningJobsFromCommandLine

Spring MVC ThemeChangeInterceptor do not change theme

I am stuck at something really painful.
I am trying to use the theming of Spring WebMVC.
This is the end of my web-context.xml file :
<beans:bean id="themeChangeInterceptor" class="org.springframework.web.servlet.theme.ThemeChangeInterceptor" >
<beans:property name="paramName" value="theme" />
</beans:bean>
<beans:bean id="themeSource" class="org.springframework.ui.context.support.ResourceBundleThemeSource">
<beans:property name="basenamePrefix" value="theme-" />
</beans:bean>
<beans:bean id="themeResolver" class="org.springframework.web.servlet.theme.CookieThemeResolver" >
<beans:property name="defaultThemeName" value="default" />
</beans:bean>
<beans:bean id="handlerMapping" class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping">
<beans:property name="interceptors">
<beans:list>
<beans:ref bean="themeChangeInterceptor" />
</beans:list>
</beans:property>
</beans:bean>
In my header file, I have something like :
<span style="float: right">
Default
|
Other
</span>
And to finish, my html head has (imported in all views) :
<link rel="stylesheet" href="<spring:theme code="css"/>" type="text/css" />
I got the two properties files in src/main/resources, which leads to my css. For example in theme-default.properties, there is :
css=static/css/dev.css
My css are in src/webapp/static/css/.
When I load the website for the first time, the theme is default, so I think that the property files and the css are correctly found by the Theme resolver.
But when I click on Default or Other in the header, nothing happens.
Do you know where the problem is ?
Thank you very much beforehand !
Olivier.
I think you are registering the interceptor wrongly. If you are configuring Spring-MVC by using <mvc:annotation-driven/> then having a separate handlerMapping will not work as a default handlerMapping is register by the custom mvc namespace also. Try registering it this way instead:
<mvc:interceptors>
<ref bean="themeChangeInterceptor"/>
</mvc:interceptors>
You should have <mvc:interceptors> declaration. And your ThemeChangeInterceptor bean should be there
<mvc:interceptors>
<beans:bean class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor">
<beans:property name="paramName" value="lang" />
</beans:bean>
<beans:bean class="org.springframework.web.servlet.theme.ThemeChangeInterceptor">
<beans:property name="paramName" value="theme" />
</beans:bean>
</mvc:interceptors>

problem with i18n (internationalization) with Spring and Velocity

I am having a problem in setting up internationalization with Spring.
Here is my config.
<bean id="messageSource"
class="org.springframework.context.support.ResourceBundleMessageSource">
<property name="basename" value="localization" />
</bean>
<!-- declare the resolver -->
<bean id="localeResolver"
class="org.springframework.web.servlet.i18n.CookieLocaleResolver">
<property name="defaultLocale" value="sv" />
</bean>
<mvc:interceptors>
<bean class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor" />
</mvc:interceptors>
<mvc:annotation-driven />
It always showing me English even when I request with ?locale=sv (Swedish).
I am using Spring with Velocity.
any idea?
thanks
This is how I did:
First copied messages_xx.properties in src/main/resources
I could not get it to work with name messages_xx_xx (messages_en_us)
Then just added following configs to xxxx_servlet.xml context
<bean id="messageSource"
class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
<property name="basename" value="classpath:messages" />
<property name="defaultEncoding" value="UTF-8" />
</bean>
<mvc:interceptors>
<bean id="localeChangeInterceptor"
class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor">
<property name="paramName" value="lang"/>
</bean>
</mvc:interceptors>
<mvc:annotation-driven/>
Used #springMessages('message.title') in my *.vm
that was it.

Resources