Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Spring JSP自定义标记中的i18n转换_Spring_Jsp_Internationalization_Jstl_Jsp Tags - Fatal编程技术网

Spring JSP自定义标记中的i18n转换

Spring JSP自定义标记中的i18n转换,spring,jsp,internationalization,jstl,jsp-tags,Spring,Jsp,Internationalization,Jstl,Jsp Tags,是否可以编写一个定制JSP标记来获取i18n消息键并输出给定请求的翻译短语 通常在JSP/JSTL中,我会: <fmt:message key="${messageKey}"><fmt:param>arg1</fmt:param></fmt:message> 但我认为我不能将messageSource注入到自定义标记中,对吗?否则我可以实例化一个新的,但它会为每个调用加载成千上万的翻译吗?我不想使messageSource成为静态类的静态成员。我

是否可以编写一个定制JSP标记来获取i18n消息键并输出给定请求的翻译短语

通常在JSP/JSTL中,我会:

<fmt:message key="${messageKey}"><fmt:param>arg1</fmt:param></fmt:message>
但我认为我不能将messageSource注入到自定义标记中,对吗?否则我可以实例化一个新的,但它会为每个调用加载成千上万的翻译吗?我不想使messageSource成为静态类的静态成员。

我不使用Spring,但在“普通”JSP中,您可以借助
过滤器或
Servlet将
ResourceBundle
实例放入会话范围

ResourceBundle bundle = ResourceBundle.getBundle(basename, request.getLocale());
request.getSession().setAttribute("bundle", bundle);
并在JSP中像对待EL中的任何其他bean一样对待它

${bundle[messageKey]}

必须有可能让Spring将其作为bean放在会话范围中。

Spring中有一个实用程序来访问web应用程序上下文。然后,您可以通过名称或类型查找bean。 要获得资源包,您可以执行以下操作:

WebApplicationContext springContext = WebApplicationContextUtils.getWebApplicationContext(pageContext.getServletContext());
messageResource = springContext.getBean("messageResource");

这个问题很古老,但我认为值得分享解决这个问题的另一种方法

要访问自定义标记中的Spring消息源,只需扩展类而不是TagSupport

在这种情况下,您必须实现doStartTagInternal()方法,而不是doStartTag(),但在此方法中,您将可以通过getRequestContext().getMessageSource()方法访问MessageSource

因此,您的类看起来像:

public class CreateCustomFieldTag extends RequestContextAwareTag{
   //variables (key, arg...), getters and setters
   @Override
   protected int doStartTagInternal() throws Exception {
      getRequestContext().getMessageSource().getMessage(
          key, new Object[] {arg}, getRequestContext().getLocale());
   }
}

同样的问题-我在一些不同的JSP中有逻辑,它们根据bean中的值确定项的状态,并且更愿意将其作为一个自定义标记,但我不确定如何从messages.properties文件中获取人性化的“状态”。我想一个选项是让标记返回消息键,然后在标记中使用它,但这看起来非常混乱…这实际上就是我最后要做的,返回消息键和参数数组作为两个独立的新pageContext属性。然后我做一个嗯。。如何从具有参数的ResourceBundle获取消息?比起附加到每个会话,将其作为模型属性附加到每个页面可能更容易。哦,对不起,我忽略了这一部分。你可以用电脑来做。当然,你也可以把它放在请求范围内。毕竟,在每个HTTP请求上创建一个新的只会稍微贵一点。
WebApplicationContext springContext = WebApplicationContextUtils.getWebApplicationContext(pageContext.getServletContext());
messageResource = springContext.getBean("messageResource");
public class CreateCustomFieldTag extends RequestContextAwareTag{
   //variables (key, arg...), getters and setters
   @Override
   protected int doStartTagInternal() throws Exception {
      getRequestContext().getMessageSource().getMessage(
          key, new Object[] {arg}, getRequestContext().getLocale());
   }
}