弹簧消息i18n与spring-mvc 3.2 + jsp + tiles 3

时间:2013-01-24 08:09:20

标签: jsp spring-mvc internationalization tiles

在“login.jsp”页面中,变量“title”的值不能转移到“default.jsp”页面中的title标签,更具体地说是代码“arguments”。当我访问登录URL时,页面的标题实际上是“{0} - CompanyName”。这是不正确的,它应该是“登录 - 公司名称”。请帮忙。

----layout/tiles.xml----
<tiles-definitions>
    <definition name="default" template="/WEB-INF/views/layout/default.jsp">
        <put-attribute name="header" value="/WEB-INF/views/layout/header.jsp" />
        <put-attribute name="footer" value="/WEB-INF/views/layout/footer.jsp" />
    </definition>
</tiles-definitions>

----users/tiles.xml----
<tiles-definitions>
    <definition extends="default" name="users/login">
        <put-attribute name="body" value="/WEB-INF/views/users/login.jsp" />
    </definition>
</tiles-definitions>

----layout/default.jsp----
<head>
    <title><spring:message code="title" arguments="${title}" />
</title>
</head>
<body>
    <tiles:insertAttribute name="header" ignore="true" />
    <tiles:insertAttribute name="body" />
    <tiles:insertAttribute name="footer" ignore="true" />
</body>

----users/login.jsp----
<spring:message code="title.login" var="title" />

----layout.properties----
title = {0} - CompanyName
title.login = Login 

1 个答案:

答案 0 :(得分:1)

固定。访问URL“/ login”时,标题显示“Login - CompanyName”。访问“/ blogs / 123”时,标题为“Name123 - Blog - CompanyName”。完美的解决方案!

----servlet-context.xml----
<bean id="messageSource" class="org.springframework.context.support.ReloadableResourceBundleMessageSource"
    p:fallbackToSystemLocale="false"
    p:basenames="WEB-INF/i18n/layout" />

----layout.properties----
title = {0} - CompanyName
title.login = Login
title.blog = {0} - Blog

----AccessController.java----
@Autowired
MessageSource messageSource;

@RequestMapping("/login")
public String login(Model model, Locale locale) {
    model.addAttribute("title",
            messageSource.getMessage("title.login", null, locale));
    return "access/login";
}

----BlogController.java----
@RequestMapping(value = "/{id}", method = RequestMethod.GET)
public String show(@PathVariable("id") Long id, Model model, Locale locale) {
    Blog blog = blogService.findById(id);
    model.addAttribute("title", messageSource.getMessage(
            "title.blog", new Object[] { blog.getName() },
            locale));
    return "blogs/show";
}