开发者

How to add some Spring attributes when the application starts

I am new to Spring framework and I am working on a web application that must be multilanguage.

The tanslated values are in the database and I don't want that each time I am rendering a view to pick the values from the database.

The idea is to get them so开发者_JAVA百科mewhere from the application. So, I want to load this values when my application starts and I don't know where exactly and where to put them.

Thank you for your help!


The usual approach to this problem is to have a .properties file for your messages, one for each language, either letting the framework decide using the browser's locale which one to use, or specify it directly.

See the reference here for locale support in Spring MVC and two different examples here and here.

If you insist on using a database for your i18n data, it is also possible of course but to my knowledge there is no built-in support for this. See this thread though for ideas.


I found a solution. I created a bean in spring like this:

public class DictionaryAccessor implements ServletContextAware, InitializingBean{

    @Resource
    private DictionaryServices dictionaryServices;

    protected ServletContext context;
    @Override
    public void setServletContext(ServletContext servletContext) {
        context = servletContext;
    }

    @Override
    public void afterPropertiesSet() throws Exception {
        logger.debug("initializing dictionary");
        loadDictionary();
        logger.debug("done with dictionary");
    }

The DictionaryAccessor will be loaded when my application starts. In the method loadDictionary I put my values as attributes in the ServletContext object.

In my jsps, I created my own tag and access the translation or in .zul files I accessed a function from my tld.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜