سؤال

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.

Can we do this in JSP and java?

هل كانت مفيدة؟

المحلول 3

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

${resourceBundleHelper.resolveMessage(YOUR_VARIABLE)}

نصائح أخرى

You can parameterize messages using JSTL's <fmt:message> and <fmt:param> tags together.

The basename attribute below refers to the base name of your ResourceBundle property files, which must be accessible on the webapp's classpath. 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

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

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top