Where can I put i18n messages when using Struts2 annotation validations? - java

I'd want to use annotation validators provided by Struts2 in form
#RequiredStringValidator(key="required")
Where can I put properties file that will be looked up?

I18N messages/resources can go in a variety of locations depending on where they'll be used.
Resources are looked up hierarchically, starting with action-specific property files, package files, eventually in the global properties defined with the resource property in the other answer.
XML configuration is the preferred mechanism, however, if you decide to explicitly name resources.
See the S2 Localization docs for additional details.

In your Struts.properties insert the following;
struts.customs.i18n.resources=MyResources
And create a file called MyResources.properties to be resided in the same path as the Struts.properties and have your key/value based messages in that file

Related

Is it possible to load different property files to use in the jsp with Spring messages?

I've different messages.properties files used for normal messages in my jsps, and also some other property files used in my java code.
One of this file is configured with some property to call a webservice, as the address, port and stuff like that. Let's call it service.properties.
One of my jsp has to call this service and I have to map the form with some information, like the action and the parameters. Right now I've written down these informations right in the jsp, but I find this not really nice, I would like to keep all the information coupled toghether inside the service.properties.
I'm looking for something to change the
<spring:message code="service.action.form"/>
to a customizable thing, to use not the messages but a specified property file
<spring:messageservice code="service.action.form" />
and move the information from the messages.properties to the service.properties
I don't know if this is possible, but I'm open to different options! Thanks.
Yes you can do that.
Move your service related properties to file service.properties and then specify the path /WEB-INF/<properties folder path>/service in the basenames properties of bean ReloadableResourceBundleMessageSource declared in your configuration xml file. And then you can just access that message with <spring:message code='your.code'/>.
Hope this helps you.

How to load app-wide settings at startup (for Spring3 webapp)?

I am in the basic stages of writing a Spring3 MVC webapp with Hibernate. I want all of data model classes to be able to access basic configuration values for example, database table prefix name, etc. I want this option, so I (or other developers) can change things on the fly by modifying them in the .properties file.
Is my best bet to create a Config class in a util package with a static block that loads a bunch of properties from a .properties file? I suppose the class itself could be static with a variety of getters to access the values within.
If I choose the method above, how could I insure the application didn't load (Failed gently) if for some reason the .properties file I have specified was not able to be loaded? With exceptions?
If my way stinks, what might be a better scenario?
Thanks!
That's a fine approach IMHO. If you would explicitly declare a bean for this class, like
<bean id="myConfig" class="com.yourcompany.yourproject.Config"/>
spring will fail at startup if it cannot instantiate the bean. So if the properties file is unreadable/not available just throw an unchecked Exception from Configs constructor.
if -for some reason- you enabled lazy loading globally you have to explicitly disable it for this bean, otherwise you won't get a failfast solution
<bean id="myConfig" class="com.yourcompany.yourproject.Config" lazy-init="false"/>
EDIT:
another nice feature of this scenario is that you can tell maven to 'filter' the resource (the .properties file), and you can get all the maven variables. This is how my prop file looks (I use this info for the About dialog. Does anybody ever opens an about-dialog btw?)
project.version=${project.version}
project.name=${project.name}
project.organization.name=${project.organization.name}
project.url=${project.url}
project.description=${project.description}

Changing Spring PropertyPlaceholderConfigurer to read from another source

I'd like to extend/replace the Spring PropertyPlaceholderConfigurer to read from a web server as opposed to properties files.
A bit of background:
I work on a project, and we're finding the number of properties files located on the users systems is getting a little unwieldy. We'd like to replace these files with a 'config server' which will store basic key/value pairs and serve them when the user starts up the app.
To avoid making too many changes, I'd like to change the way the PropertyPlaceholderConfigurer finds properties - rather than implementing an entirely new way to manage properties. So on startup - Spring will read all properties from a url, and feed these into my spring config xml in the same way as it would have with actual files.
Bonus!
If anyone has any ideas how to do this where properties are reloaded from the server only when they change, will get bonus points (I have no idea if I have the ability to assign bonus points, but I'll try!). That would be a 'nice to have, if there's not too much effort involved' solution.
Spring's PropertyPlaceholderConfigurer (PPC) already uses the Resource interface to specfiy the location from where to read properties (via the setLocation(Resource) method inherited from PropertiesLoaderSupport.
There is an implementing class of this interface called URLResource which probably does what you want. You could simply create a PPC and set the location property with a bean of this type to load the properties from a URL instead of a file. This class also supports file:// type URLs, so you could switch between on- and offline properties loading depending on the URL you use.

Default data in Webapp?

how can I setup default data in a webapp? Eg default users: admin, test, etc.
I first thought of defining a static section in of of my session-beans, but that would still cause a new creation of users for every session. Which is not suitable.
How can I do else?
ty!
If you are using hibernate (I'll assume you are from the question tags), then all you need is to have an file called import.sql in your classpath, and hibernate will automatically execute it if you have the hibernate.hbm2ddl.auto property set to create or create-drop. Have a look at this link:
http://relation.to/Bloggers/RotterdamJBugAndHibernatesImportsql#H-ImportsqlEasilyImportDataInYourUnitTests
the best way is to put in your webapp's configuration section, there it will be loaded and read only once through the following steps :
an xml file of your data, just put it on your class path and read it by using java propery which will get an input stream of your class loader path

JBoss SystemPropertiesService inconsistently reloading properties

I have two xml files I'm looking at which define an mbean that uses org.jboss.varia.property.SystemPropertiesService. One is properties-service.xml and lives directly in the deploy directory, the other is further down within my application's ear - let's call it myapp-properties-service.xml.
This mean can define two attributes - a URLList which might take properties of the form ./conf/props/myapp.properties and a Properties attribute which just takes the properties directly (e.g. myproperty=myvalue).
The problem is that while both attributes in both files load properties into the System properties at startup, the behaviour differs when I make a change while JBoss is running.
The Properties attribute in properties-service.xml successfully reloads the properties. The URRList fails to reload the properties and both attributes in myapp-properties-service.xml fail to reload.
Am I mistaken in thinking all four cases should reload properties? My ideal solution would be to provide a URL to myapp-properties-service.xml.
Any suggestions? Thanks.
It will only reload them when you change *-service.xml file. Changing of the files it references is not enough. You must simply touch *-service.xml. I suspect the reason why it does not automatically detect changes is because this list can contain URLs and how do you expect it to know when these files have changed. Changing properties defined within the file works because your modifying the *-service.xml file itself which is watched by JBoss.

Categories