spring mvc and ReloadableResourceBundleMessageSource

耗尽温柔 提交于 2020-01-06 17:39:23

问题


I am trying to build a spring MVC application (basically for self trainning reasons). This application has spring MVC, tiles, spring thems and jsp (sorry for numbering all but since i am not sure what i do wrong, i just wish to number all i think may help you help me).

my spring mvc configuration seems to work, untill i try to get message property from property files through org.springframework.context.support.ReloadableResourceBundleMessageSource. so here is my configuration

<bean id="messageSource" class="org.springframework.context.support.ReloadableResourceBundleMessageSource" p:cacheSeconds="3000">
    <property name="basenames">
        <list>
            <value>messages</value>
            <value>dev</value>
            <value>theme</value>
        </list>         
    </property>
</bean>  


<bean id="tilesConfigurer" class="org.springframework.web.servlet.view.tiles2.TilesConfigurer" p:definitions="/WEB-INF/tiles-defs/tiles-defs.xml" />
<bean id="tilesViewResolver" class="org.springframework.web.servlet.view.ResourceBundleViewResolver" p:basename="views" />


<!-- Declare the Interceptor -->
<bean id="localeChangeInterceptor" class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor" p:paramName="language"/>
<bean id="themeChangeInterceptor" class="org.springframework.web.servlet.theme.ThemeChangeInterceptor" p:paramName="theme"/>

<!-- Declare the Resolver -->
<bean id="localeResolver" class="org.springframework.web.servlet.i18n.SessionLocaleResolver"/>
<bean id="themeResolver" class="org.springframework.web.servlet.theme.SessionThemeResolver"/>

<!-- Spring themes -->
<bean class="org.springframework.web.servlet.mvc.support.ControllerClassNameHandlerMapping">
    <property name="interceptors">
        <list>
            <ref local="themeChangeInterceptor" />
            <ref bean="localeChangeInterceptor" />                          
        </list>
    </property>
</bean>

tiles and theme seem to work, so in my tiles baselayout i have:

<title><tiles:insertAttribute name="title" ignore="true" /></title>
    <c:set var="css"><spring:theme code="css"/></c:set>
    <c:if test="${not empty css}">
        <link rel="stylesheet" href="<c:url value='${css}'/>" type="text/css"/>
    </c:if>

this works! html is rendered with the correct css that i have in css property located inside theme.properties file loaded from ReloadableResourceBundleMessageSource and magically existing inside tiles!

So, getting courage i go to my jsp and insert attempt 1:

 <p><spring:message code="context"/></p>

attempt 2:

<p><fmt:message key="context"/></p>

tags used:

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>

(is there a difference? both work? seen both over web... so i tried once the first and then the second one)

and in the messages.properties/messages_el_gr.properties files i type context=default and context=greek.

This gives me exception in the first case and in the second just the string context prefixed and postfixed with ???. Exception i get is:

SEVERE: Servlet.service() for servlet [dispatcher] in context with path [/homeAutomation] threw exception [Request processing failed; nested exception is org.apache.tiles.impl.CannotRenderException: ServletException including path '/WEB-INF/tiles/baseLayout.jsp'.] with root cause javax.servlet.jsp.JspTagException: No message found under code 'context' for locale 'el_GR'. at org.springframework.web.servlet.tags.MessageTag.doStartTagInternal(MessageTag.java:184) at org.springframework.web.servlet.tags.RequestContextAwareTag.doStartTag(RequestContextAwareTag.java:79) at org.apache.jsp.WEB_002dINF.jsp.friends_jsp._jspx_meth_spring_005fmessage_005f0(friends_jsp.java:104) at org.apache.jsp.WEB_002dINF.jsp.friends_jsp._jspService(friends_jsp.java:75) at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70) at javax.servlet.http.HttpServlet.service(HttpServlet.java:722) at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:419) at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:390) at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:333) at javax.servlet.http.HttpServlet.service(HttpServlet.java:722) at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:304) at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210) at org.apache.catalina.core.ApplicationDispatcher.invoke(ApplicationDispatcher.java:684) at org.apache.catalina.core.ApplicationDispatcher.doInclude(ApplicationDispatcher.java:593) at org.apache.catalina.core.ApplicationDispatcher.include(ApplicationDispatcher.java:530) at org.apache.jasper.runtime.JspRuntimeLibrary.include(JspRuntimeLibrary.java:927) at org.apache.jasper.runtime.PageContextImpl.doInclude(PageContextImpl.java:684) at org.apache.jasper.runtime.PageContextImpl.include(PageContextImpl.java:678) at org.apache.tiles.jsp.context.JspTilesRequestContext.include(JspTilesRequestContext.java:103) at org.apache.tiles.jsp.context.JspTilesRequestContext.dispatch(JspTilesRequestContext.java:96) at org.apache.tiles.renderer.impl.TemplateAttributeRenderer.write(TemplateAttributeRenderer.java:44) at org.apache.tiles.renderer.impl.AbstractBaseAttributeRenderer.render(AbstractBaseAttributeRenderer.java:106) at org.apache.tiles.renderer.impl.ChainedDelegateAttributeRenderer.write(ChainedDelegateAttributeRenderer.java:76) at org.apache.tiles.renderer.impl.AbstractBaseAttributeRenderer.render(AbstractBaseAttributeRenderer.java:106) at org.apache.tiles.impl.BasicTilesContainer.render(BasicTilesContainer.java:670) at org.apache.tiles.impl.BasicTilesContainer.render(BasicTilesContainer.java:336) at org.apache.tiles.template.InsertAttributeModel.renderAttribute(InsertAttributeModel.java:210) at org.apache.tiles.template.InsertAttributeModel.end(InsertAttributeModel.java:126) at org.apache.tiles.jsp.taglib.InsertAttributeTag.doTag(InsertAttributeTag.java:311) at org.apache.jsp.WEB_002dINF.tiles.baseLayout_jsp._jspx_meth_tiles_005finsertAttribute_005f3(baseLayout_jsp.java:307) at org.apache.jsp.WEB_002dINF.tiles.baseLayout_jsp._jspService(baseLayout_jsp.java:108) at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70) at javax.servlet.http.HttpServlet.service(HttpServlet.java:722) at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:419) at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:390) at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:333) at javax.servlet.http.HttpServlet.service(HttpServlet.java:722) at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:304) at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210) at org.apache.catalina.core.ApplicationDispatcher.invoke(ApplicationDispatcher.java:684) at org.apache.catalina.core.ApplicationDispatcher.processRequest(ApplicationDispatcher.java:471) at org.apache.catalina.core.ApplicationDispatcher.doForward(ApplicationDispatcher.java:402) at org.apache.catalina.core.ApplicationDispatcher.forward(ApplicationDispatcher.java:329) at org.apache.tiles.servlet.context.ServletTilesRequestContext.forward(ServletTilesRequestContext.java:241) at org.apache.tiles.servlet.context.ServletTilesRequestContext.dispatch(ServletTilesRequestContext.java:222) at org.apache.tiles.renderer.impl.TemplateAttributeRenderer.write(TemplateAttributeRenderer.java:44) at org.apache.tiles.renderer.impl.AbstractBaseAttributeRenderer.render(AbstractBaseAttributeRenderer.java:106) at org.apache.tiles.impl.BasicTilesContainer.render(BasicTilesContainer.java:670) at org.apache.tiles.impl.BasicTilesContainer.render(BasicTilesContainer.java:690) at org.apache.tiles.impl.BasicTilesContainer.render(BasicTilesContainer.java:644) at org.apache.tiles.impl.BasicTilesContainer.render(BasicTilesContainer.java:627) at org.apache.tiles.impl.BasicTilesContainer.render(BasicTilesContainer.java:321) at ......

I consider that i do not have the messageSource inside my context, so i considered as next step of searching what is going on to do the following (found on web)

@Autowired
ReloadableResourceBundleMessageSource messageSource;
@RequestMapping("/friends.htm")
public ModelAndView getPage()
{   
    ModelAndView modelAndView=new ModelAndView("friends");
    modelAndView.addObject(messageSource);
    return modelAndView;
}

by the way, my previeus controller had that method as follows:

@RequestMapping("/friends.htm")
public String getPage()
{   
    return friends;
}

This again throws same exception.. messageSource is not null.. it is autowired. if i do inside the controller:messageSource.getMessage("context", null, Locale.ENGLISH), this throws the same exception when trying to get it.. (Greek locale does not exist?? with english it will go to default locale properties file which is messages.properties, correct?)

Ah, what else can i give you... yes property files are located in the src/main/resources directory and in war inside homeAutomation.war\WEB-INF\classes\ and homeAutomation.war (why?? i thought these would exist only in homeautomation.war directory... but skip this, atleast they are there...) my poms war plugin configuration is as follows:

<plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-war-plugin</artifactId>
            <version>2.1.1</version>
            <configuration>
                <webResources>
                    <resource>
                        <directory>src/main/resources</directory>
                    </resource>
                </webResources>
            </configuration>
        </plugin>

in target directory again i see the property files in both target\classes directory and target\homeAutomation.

After all these, i still do not know what i do wrong and i cannot get localized properties inside my jsp working. I hope i gave al the necessary info (sorry for the too much info..., did not know which was relevant and which not). If you need more info please ask. Have been searching for this five days now and still have no clue.


回答1:


you can use <p><spring:message code="context"/></p> but you should have context= what you want in one of the following files :

  • messages_el_GR.properties
  • dev_el_GR.properties
  • theme_el_GR.properties


来源:https://stackoverflow.com/questions/12939619/spring-mvc-and-reloadableresourcebundlemessagesource

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!