简体   繁体   English

Java Spring MVC将.tag属性转换为.jsp

[英]Java Spring MVC get .tag properties to .jsp

I have a Java configured Spring MVC application. 我有一个Java配置的Spring MVC应用程序。 I would like to know, how to access properties defined in WEB-INF\\tags . 我想知道,如何访问WEB-INF\\tags定义的属性。
AppConfig.java: 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: usermsg_en.properties:
user.test=This is a test

WEB-INF\\tags\\test.tag: WEB-INF \\标签\\ 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: 在.jsp文件中,这不起作用:

<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 但是,如果我直接在.jsp中包含<spring:message code="user.test" var="user_test" text="default text"/> ,它就可以工作

<spring:message> should work in tag as well as in jsp files. <spring:message>应该在tagjsp文件中都起作用。

I think the problem is that you have not included the spring taglib in your tag file. 我认为问题在于您没有在tag文件中包含spring taglib。 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} 然后,您可以使用${user_test}访问.tag文件中定义的变量

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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