spring mvc和ReloadableResourceBundleMessageSource

时间:2012-10-17 17:06:52

标签: spring spring-mvc apache-tiles

我正在尝试构建一个spring MVC应用程序(基本上是出于自我培训的原因)。这个应用程序有弹簧MVC,瓷砖,弹簧主题和jsp(抱歉编号所有,但因为我不知道我做错了什么,我只是希望编号所有我认为可以帮助你帮助我。)

我的spring mvc配置似乎有效,直到我尝试通过org.springframework.context.support.ReloadableResourceBundleMessageSource从属性文件中获取message属性。 所以这是我的配置

<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>

瓷砖和主题似乎工作,所以在我的瓷砖baselayout我有:

<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>
这是有效的! html使用正确的css呈现,我在css属性中找到了从ReloadableResourceBundleMessageSource加载的theme.properties文件,并且神奇地存在于tile内部!

所以,获得勇气,我去了我的jsp并插入 尝试1:

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

尝试2:

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

使用的标签:

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

(有区别吗?两者都有效吗?在网上看到......所以我尝试了第一次,然后是第二次)

并在messages.properties/messages_el_gr.properties文件中输入context = default和context = greek。

这在第一种情况下给出了异常,在第二种情况下只是字符串上下文的前缀和后缀为???。 我得到的例外是:

  

SEVERE:Servlet [dispatcher]的Servlet.service()在路径[/ homeAutomation]的上下文中引发了异常[请求处理失败;嵌套异常是org.apache.tiles.impl.CannotRenderException:ServletException包括具有根本原因的路径&#39; /WEB-INF/tiles/baseLayout.jsp'。]   javax.servlet.jsp.JspTagException:在代码&#39; context&#39;下找不到任何消息对于语言环境&#39; el_GR&#39;。       在org.springframework.web.servlet.tags.MessageTag.doStartTagInternal(MessageTag.java:184)       在org.springframework.web.servlet.tags.RequestContextAwareTag.doStartTag(RequestContextAwareTag.java:79)       在org.apache.jsp.WEB_002dINF.jsp.friends_jsp._jspx_meth_spring_005fmessage_005f0(friends_jsp.java:104)       在org.apache.jsp.WEB_002dINF.jsp.friends_jsp._jspService(friends_jsp.java:75)       在org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)       在javax.servlet.http.HttpServlet.service(HttpServlet.java:722)       在org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:419)       在org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:390)       在org.apache.jasper.servlet.JspServlet.service(JspServlet.java:333)       在javax.servlet.http.HttpServlet.service(HttpServlet.java:722)       在org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:304)       在org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)       在org.apache.catalina.core.ApplicationDispatcher.invoke(ApplicationDispatcher.java:684)       在org.apache.catalina.core.ApplicationDispatcher.doInclude(ApplicationDispatcher.java:593)       在org.apache.catalina.core.ApplicationDispatcher.include(ApplicationDispatcher.java:530)       在org.apache.jasper.runtime.JspRuntimeLibrary.include(JspRuntimeLibrary.java:927)       at org.apache.jasper.runtime.PageContextImpl.doInclude(PageContextImpl.java:684)       在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)       在org.apache.tiles.renderer.impl.AbstractBaseAttributeRenderer.render(AbstractBaseAttributeRenderer.java:106)       at org.apache.tiles.renderer.impl.ChainedDelegateAttributeRenderer.write(ChainedDelegateAttributeRenderer.java:76)       在org.apache.tiles.renderer.impl.AbstractBaseAttributeRenderer.render(AbstractBaseAttributeRenderer.java:106)       在org.apache.tiles.impl.BasicTilesContainer.render(BasicTilesContainer.java:670)       在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)       在org.apache.jsp.WEB_002dINF.tiles.baseLayout_jsp._jspService(baseLayout_jsp.java:108)       在org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)       在javax.servlet.http.HttpServlet.service(HttpServlet.java:722)       在org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:419)       在org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:390)       在org.apache.jasper.servlet.JspServlet.service(JspServlet.java:333)       在javax.servlet.http.HttpServlet.service(HttpServlet.java:722)       在org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:304)       在org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)       在org.apache.catalina.core.ApplicationDispatcher.invoke(ApplicationDispatcher.java:684)       在org.apache.catalina.core.ApplicationDispatcher.processRequest(ApplicationDispatcher.java:471)       在org.apache.catalina.core.ApplicationDispatcher.doForward(ApplicationDispatcher.java:402)       在org.apache.catalina.core.ApplicationDispatcher.forward(ApplicationDispatcher.java:329)       在org.apache.tiles.servlet.context.ServletTilesRequestContext.forward(ServletTilesRequestContext.java:241)       在org.apache.tiles.servlet.context.ServletTilesRequestContext.dispatch(ServletTilesRequestContext.java:222)       at org.apache.tiles.renderer.impl.TemplateAttributeRenderer.write(TemplateAttributeRenderer.java:44)       在org.apache.tiles.renderer.impl.AbstractBaseAttributeRenderer.render(AbstractBaseAttributeRenderer.java:106)       在org.apache.tiles.impl.BasicTilesContainer.render(BasicTilesContainer.java:670)       在org.apache.tiles.impl.BasicTilesContainer.render(BasicTilesContainer.java:690)       在org.apache.tiles.impl.BasicTilesContainer.render(BasicTilesContainer.java:644)       在org.apache.tiles.impl.BasicTilesContainer.render(BasicTilesContainer.java:627)       在org.apache.tiles.impl.BasicTilesContainer.render(BasicTilesContainer.java:321)       在.......

我认为我的上下文中没有messageSource,因此我认为下一步是搜索以下内容(在网上找到)

@Autowired
ReloadableResourceBundleMessageSource messageSource;
@RequestMapping("/friends.htm")
public ModelAndView getPage()
{   
    ModelAndView modelAndView=new ModelAndView("friends");
    modelAndView.addObject(messageSource);
    return modelAndView;
}
顺便说一句,我的前任控制器有如下方法:

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

这再次抛出相同的异常.. messageSource不为null ..它是自动装配的。 如果我在控制器内部执行:messageSource.getMessage(&#34; context&#34;,null,Locale.ENGLISH),这会在尝试获取它时抛出相同的异常..(希腊语语言环境不存在??用英语将转到默认的语言环境属性文件,即messages.properties,对吗?)

啊,我还能给你什么...是属性文件位于src / main / resources目录和homeAutomation.war \ WEB-INF \ classes \和homeAutomation.war中的war(为什么?我)以为这些只存在于homeautomation.war目录中......但是跳过这个,至少他们在那里......)我的poms war插件配置如下:

<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>

再次在目标目录中,我看到target \ classes目录和target \ homeAutomation中的属性文件。

在所有这些之后,我仍然不知道我做错了什么,我无法在我的jsp工作中获得本地化属性。我希望我给了必要的信息(抱歉太多信息......,不知道哪些是相关的,哪些不相关)。如果您需要更多信息,请询问。一直在寻找这五天,仍然没有任何线索。

1 个答案:

答案 0 :(得分:1)

你可以使用<p><spring:message code="context"/></p> 但是你应该在以下某个文件中有context= what you want

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