Passing localized javascript messages from resource bundles using JSF

后端 未结 3 1174
面向向阳花
面向向阳花 2020-12-21 02:01

I have built an application with JSF and all messages issued by the server are localized with resource bundles.

My question is: how to get messages issued in the cli

3条回答
  •  一生所求
    2020-12-21 02:28

    the wutzebaer answer is right but it has a problem when then the variables of a literal has any dot, like "person.name"

      
    

    that worked for me, but the netbeans shows this error:

    Error: The prefix "c" for the "c: forEach" element is not linked.
    

    cause it had put a JSTL tag insida a script, but it works fine, however

    also there is another way to do it

    @ManagedBean(name = "ResouceBundle")
    @ApplicationScoped
    public class ResouceBundle implements Serializable {
    
        private static final long serialVersionUID = 1L; //needed because the bean is application|session|view and it needs to be Serializable
        public String msg;
    
        @PostConstruct
        public void init() {
            this.msg = createResourceBundleJSON("resourceName");
        }
    
        public String createResourceBundleJSON(String resourceName) {
    
            FacesContext context = FacesContext.getCurrentInstance();
            ResourceBundle bundle = context.getApplication().getResourceBundle(context, resourceName);
            JSONObject jsonObj = new JSONObject();
            Set keys = bundle.keySet();
            for (String key : keys) {
                jsonObj.put(key, JSONObject.wrap(bundle.getString(key)));
            }
            return jsonObj.toString(); 
        }
    
        public String getMsg() {
            return msg;
        }
    
        public static long getSerialVersionUID() {
            return serialVersionUID;
        }
    
    }
    

    and then, in the XHTML, just write:

    
    

提交回复
热议问题