jerrysun0126
jerrysun0126

Reputation: 21

spring message i18n with spring-mvc 3.2 + jsp + tiles 3

In the page "login.jsp", the value of the variable "title" cannot be transferred to the title tag in the "default.jsp" page, more specifically the code "arguments". When I visit the login url, the page's title is actually "{0} - CompanyName". That's incorrect, it should be "Login - CompanyName". Please help.

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

Upvotes: 1

Views: 2058

Answers (1)

jerrysun0126
jerrysun0126

Reputation: 21

Fixed. When visit url "/login", the title shows "Login - CompanyName". When visit "/blogs/123", the title is "Name123 - Blog - CompanyName". Perfect solution!

----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";
}

Upvotes: 1

Related Questions