Argument to message in properties file not being resolved by Spring - spring

I have the following key/message in a properties file:
kadjoukor.registration.form.successMessage=You've been successfully registered. An email has been sent to <b>{0}</b>. Please activate your registration by consulting your email.
In my template, I try to display it as follows in a Thymeleaf template:
<div id="alert-success" th:if="${#bools.isTrue(member)}" class="alert alert-success" th:utext="#{kadjoukor.registration.form.successMessage(${member.email})}"></div>
All I get is:
Youve been successfully registered. An email has been sent to {0}. Please activate your registration by consulting your email.
Notice the argument has not been replaced by its value i.e. I get this: {0}. Also notice the apostrophe has been removed by Spring...
EDIT:
Here is how I've configured the message source:
<bean class="org.springframework.context.support.ResourceBundleMessageSource" id="messageSource">
<property name="basenames" value="/META-INF/i18n/application,/META-INF/i18n/messages" />
</bean>

In order to prevent this problem, the apostrophe has to be escaped by another apostrophe.
See: Why Spring MessageSource arguments are not filled correctly in some locales?
However, note that this only occurs when there's one or several arguments in the message.

Related

Expression in service activator doesn't execute (Spring Integration)

I debug my app and the expression doesn't execute.
<bean id="remover" class="org.example.Remover"/>
<bean id="key" class="java.lang.String">
<constructor-arg value="KEY"/>
</bean>
<integration:service-activator input-channel="middleChannel"
expression="#remover.remove(key)"/>
No Exception. method doesn't call.
That’s not true: there must be an exception. You don’t show the whole flow to determine the part who swallows your exception on the matter.
Your expression is like:
#remover.remove(key)
You correctly call remover bean, but there is no such a property like key in the Message object - just only headers and payload. That’s why I’m making a conclusion that you have problems.
You can turn on debug logs for the Spring Integration category and follow you the trace of the call.

JSF custom message for validation error

I'm using GlassFish 3.1.2.2 and I want to define a generic validation message template to not include field identifier/label.
I've found couple of related topics on StackOverflow but none of these solutions (all involving javax.faces.validator.BeanValidator.MESSAGE) is working.
1) placing ValidationMessages.properties in root of classpath (in my case it was placed in WEB-INF/classes of my WAR file) - no effect
2) defining the ValidationMessages.properties file in <message-bundle> in faces-config.xml - no effect
3) defining the ValidationMessages.properties file in <resource-bundle> in faces-config.xml - no effect
I tested that resource bundle is correctly working as I can use it in EL, example: #{text['javax.faces.validator.BeanValidator.MESSAGE']} where text is my var from resource-bundle definition.
NOTE: I don't want to give validatorMessage attribute on each of input fields in my application. I just want to setup my custom message once for whole application.
IMPORTANT: the solution presented here Mkyong.com is not working as well.
NOTE: I'm declaring to use JSF 2.0 and Glassfish 3.1.2.2 certainly supports that.
NOTE: I don't want to implement validation in managed bean instead of using JSF validation/Bean Validation.
I'm answering myself: to set custom message for required-field validation you need to set following properties in messages bundle file (message bundle is defined in faces-config.xml by <message-bundle> tag):
1) javax.faces.component.UIInput.REQUIRED_detail - a message that is by default presented in h:message or h:messages tags - or when showDetail=true (it is by default). GENERALLY IT'S ENOUGH.
2) javax.faces.component.UIInput.REQUIRED - a message that is presented when showSummary=true in h:message and h:messages tags. Set this additionally if you display summary in your h:message/h:messages.
NOTE: this is not true that javax.faces.validator.BeanValidator.MESSAGE is responsible for required-field-error messages.
Add <h:messages> in your jsp/xhtml
and to display error message from Managed Bean depending upon condition
Use
FacesContext ctx = FacesContext.getCurrentInstance();
FacesMessage msg =new FacesMessage(FacesMessage.SEVERITY_INFO, errorMessage,detailMessage);
ctx.addMessage(null, msg);

Multiple ResourceBundles with Spring and JSTL

Here's my problem: I want to organize my resource bundle files so that I have the messages in one file and the labels in another. For this I created two .properties files which I declared in my spring configuration files. The actual declaration is this:
<bean id="messageSource" class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
<property name="basenames">
<array>
<value>strings.gui</value>
<value>strings.messages</value>
</array>
</property>
</bean>
The folder layout goes like this:
/WEB-INF/classes/ro/** - source files
/WEB-INF/classes/strings/* - i18n files (gui_ro.properties and messages_ro.properties, along with their _en versions)
Everything works great when I display labels from gui_xx.properties but it fails to display messages from messages_xx.properties files. The error message in the server console is
ResourceBundle [strings.messages] not found for MessageSource: Can't find bundle for base name strings.messages, locale ro
I'm using JSTL with TilesView, and the problem occurs regardless of what tag I use to display the text - fmt:message key=... or spring:message code=....
Can anybody help pe with this?
Thank you very much and have a good day.
I think the correct basenames should be:
<value>classpath:/ro/gui</value>
<value>classpath:/strings/messages</value>
Can you please try these values for basenames.

Can I use a property placeholder in a Spring 3 bean's id attribute in the XML configuration?

I have an application that is in the process of being migrated to a new setup. The current interim is that the application is being deployed onto the same GlassFish server but with different configuration (property files, jndi, etc). In my Spring 3 configuration, I am using the task namespace for scheduling, but I want to know if there is a problem if all deployments of the application can have the same id for the scheduler, ie:
<task:scheduler id="appScheduler" pool-size="10" />
<task:scheduled-tasks scheduler="appScheduler">
<task:scheduled ref="notificationSender" method="sendNotifications" cron="${email.time.cron}" />
</task:scheduled-tasks>
If not, is there a way to put a placeholder in the scheduler's id attribute so each application has its own, ie:
<task:scheduler id="${app.name}Scheduler" pool-size="10" />
<task:scheduled-tasks scheduler="${app.name}Scheduler">
...
I did try the above, but the error message kept coming back about not being able to find the referenced scheduler after the property was substituted. Thus raises the question, will Spring substitute a property inside the id attribute?

Unwanted i18n in Spring Security's error message

I have a problem with Spring Security 3 and probably with i18n. I use Velocity for my view templates and I have the following code in one of them:
#if($SPRING_SECURITY_LAST_EXCEPTION)
<div class="error"><p>#springMessage($SPRING_SECURITY_LAST_EXCEPTION.message)</p></div>
#end
When user tried to login and he passed bad credentials for example, there should be error message shown on the page. The problem is that application try to resolve message in locale 'pl' and I receive the following exception:
org.springframework.context.NoSuchMessageException: No message found under code 'Bad credentials' for locale 'pl'.
In my configuration files I don't have any informations about i18n because I just don't need it.
I noticed that information about 'pl' locale can be retrieved from request (header Accept-Language: pl,en-us;q=0.7,en;q=0.3).
How to ignore this and serve pages only in defult language?
If you don't want i18n, don't use #springMessage. $SPRING_SECURITY_LAST_EXCEPTION.message is a message itself, not message code, thus you need to output it as is (though I'm not sure about HTML escape in Velocity):
#if($SPRING_SECURITY_LAST_EXCEPTION)
<div class="error"><p>${SPRING_SECURITY_LAST_EXCEPTION.message}</p></div>
#end
I've had fun with il8n in the past. What I do is to force spring to only use the default language, I've done this by using a FixedLocaleResolver in the config files. Change the value to which ever locale you want to have as default.
<bean id="localeResolver" class="org.springframework.web.servlet.i18n.FixedLocaleResolver">
<property name="defaultLocale" value="en_US" />
</bean>

Resources