简体   繁体   中英

Java Spring MVC get .tag properties to .jsp

I have a Java configured Spring MVC application. I would like to know, how to access properties defined in WEB-INF\\tags .
AppConfig.java:

@Bean
public MessageSource messageSource() {
    ReloadableResourceBundleMessageSource messageSource = new ReloadableResourceBundleMessageSource();
    messageSource.setBasename("/i18/usermsg");
    messageSource.setDefaultEncoding("UTF-8");
    return messageSource;
}

@Bean
public LocaleResolver localeResolver() {
    CookieLocaleResolver resolver = new CookieLocaleResolver();
    resolver.setDefaultLocale(new Locale("en_US"));
    resolver.setCookieName("myLocaleCookie");
    resolver.setCookieMaxAge(4800);
    return resolver;
}

usermsg_en.properties:
user.test=This is a test

WEB-INF\\tags\\test.tag:

<%@taglib prefix="c"   uri="http://java.sun.com/jsp/jstl/core" %>
<%@taglib uri="http://www.springframework.org/tags" prefix="spring"%>
<%@attribute name="front"   required="false" description="some description" %>
<head>
    <spring:message code="user.test" var="user_test" text="default text"/>
</head>



In .jsp file this doesn't work:

<t:head front="true"/>
<body>
    ${user_test}
</body>


But it works if I include <spring:message code="user.test" var="user_test" text="default text"/> directly in .jsp

<spring:message> should work in tag as well as in jsp files.

I think the problem is that you have not included the spring taglib in your tag file. So add:

<%@taglib prefix="spring"   uri="http://www.springframework.org/tags" %>

I figured it out:

This
<spring:message code="user.test" var="user_test" text="default text"/>
should be
<spring:message code="user.test" var="user_test" text="default text" scope="request"/>

Then you can access variables defined in .tag files with ${user_test}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM