简体   繁体   English

JSP是否支持对资源束文本进行运行时字符串替换?

[英]Do JSP support runtime string substitution for resource bundle text?

In the resource bundle properties file I have following text 在资源包属性文件中,我有以下文本

system.invalidID = This accountID $ is not valid for current session.

I want to replace dynamically $(this will be the actual ID at runtime) from the above resource text in my JSP and java Class. 我想从我的JSP和Java类中的上述资源文本中动态替换$(这将是运行时的实际ID)。

Can we do this in JSP and java? 我们可以在JSP和Java中做到这一点吗?

You can parameterize messages using JSTL's <fmt:message> and <fmt:param> tags together. 您可以一起使用JSTL的<fmt:message><fmt:param>标记对<fmt:message> <fmt:param>

The basename attribute below refers to the base name of your ResourceBundle property files, which must be accessible on the webapp's classpath. 下面的basename属性指的是ResourceBundle属性文件的基本名称,必须在webapp的类路径上可以访问它。 Putting everything together looks like this: 将所有内容放在一起看起来像这样:

<%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt" %>
<fmt:setBundle basename="com.y.app.Messages" var="bundle" />
<fmt:message bundle="${bundle}" key="system.invalidID">
  <fmt:param value="${attemptedID}" />
</fmt:message>

You could try the JSTL Core <fmt:bundle> Tag 您可以尝试JSTL Core <fmt:bundle> Tag

see http://www.tutorialspoint.com/jsp/jstl_format_bundle_tag.htm 参见http://www.tutorialspoint.com/jsp/jstl_format_bundle_tag.htm

You can actually create a simple class with a method something like 您实际上可以使用类似以下方法的方法来创建一个简单的类

public class ResourceBundleHelper{

    public String resolveMessage(String key){
     //code
    }

} 

and then create instance of this class and put it to ServletContext and then from jsp el 然后创建此类的实例,并将其放入ServletContext ,然后从jsp el

${resourceBundleHelper.resolveMessage(YOUR_VARIABLE)}

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

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