如何从 JSP 自定义标记中读取上下文中的包?
我有一个 JSP 2.0 文件,其中包含对自定义标记的调用,该标记需要知道页面上当前使用的包,以便它可以查找一些资源。自定义标签是用 Java 编写的。捆绑包可以根据使用自定义标签的页面进行更改,但资源键将始终相同,因此我想使用现有的 fmt:bundle
标签来指定这一点,例如:
<fmt:bundle basename="myBundle">
<custom:tag title="text.title"/>
</fmt:bundle>
I'我们一直假设 fmt:bundle
标签可以从内部标签读取(或以其他方式提供其环境),其中自定义标签位于上面的示例中(与其交互的方式相同)这fmt:message
标记),但我无法弄清楚如何访问 fmt:bundle
据称在内部定义的 LocalizationContext
定义自定义标签的 Java。
我已经尝试过
LocalizationContext lc = (LocalizationContext)Config.get(
pageContext.getRequest(),Config.FMT_LOCALIZATION_CONTEXT);
ResourceBundle rb = lc.getResourceBundle();
String s =rb.getKey(title);
,但我只是得到
java.util.MissingResourceException: Can't find resource for bundle
java.util.PropertyResourceBundle, key text.title
这似乎表明这不是正确的查找位置(我猜这已经落到了默认捆绑包中?)。
一种解决方法可能是将包名称传递到自定义标记中,但我确信我想要实现的目标应该是可能的,只要我可怜的 JSP 知识没有让我失望,所以我希望有人可以帮助我得到更好的理解!
我应该做什么?
I have a JSP 2.0 file containing calls to a custom tag that needs to know what bundle is currently in use on the page, so it can look up some resources. The custom tag is written in Java. The bundle can change depending what page the custom tag is used on, but the resource key will always be the same, so I wanted to use the existing fmt:bundle
tag to specify this, e.g.:
<fmt:bundle basename="myBundle">
<custom:tag title="text.title"/>
</fmt:bundle>
I've been assuming that the fmt:bundle
tag can be read from (or otherwise provides its environment to) the inner tags, where the custom tag is in the above example (in the same way that it interacts with the fmt:message
tag), but I haven't been able to figure out how to access the LocalizationContext
that fmt:bundle
supposedly defines from within the Java that defines the custom tag.
I've tried
LocalizationContext lc = (LocalizationContext)Config.get(
pageContext.getRequest(),Config.FMT_LOCALIZATION_CONTEXT);
ResourceBundle rb = lc.getResourceBundle();
String s =rb.getKey(title);
but I just get
java.util.MissingResourceException: Can't find resource for bundle
java.util.PropertyResourceBundle, key text.title
which seems to indicate that is not the right place to look (I guess that has fallen through to the default bundle?).
One workaround might be to pass the bundle name into the custom tag, but I'm sure what I want to achieve should be possible, if only my poor JSP know-how didn't let me down, so I hope someone can help me get a better understanding!
What should I be doing?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
根据 JSTL 规范,有一个名为 LocaleSupport 实现捆绑查找,并且可以由任何需要生成本地化消息的标记处理程序实现使用。
According to the JSTL specification there is a class named LocaleSupport that implements the bundle lookup and may be used by any tag handler implementation that needs to produce localized messages.