How can I check if a resource bundle key does not exist using JSTL tags?

断了今生、忘了曾经 提交于 2019-12-18 05:50:38

问题


I have a resource file that will have some optional keys. If the optional resource key is not present, I set a default instead. It appears that there is no easy way to determine if a key exists in the resource bundle. So this is what I'm doing to get around it.

<fmt:message var="title" key="login.reg.signup.${signupForm.regfrom}.title" />
<c:if test='${fn:startsWith(title, "??")}'>
    <fmt:message var="title" key="login.reg.signup.default.title" /> 
</c:if>

Is there a better way?


回答1:


You could write your own JSP tag that does this, so you can then just do:

<my:message var="title" key="${form}.title" default="default.title"/>

The tag implementation could either be your current JSP syntax, or a Java class.




回答2:


You can use the #{messagesFactory.messages.containsKey('key')} to check.



来源:https://stackoverflow.com/questions/739170/how-can-i-check-if-a-resource-bundle-key-does-not-exist-using-jstl-tags

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!