{"id":1413,"date":"2011-07-19T22:14:15","date_gmt":"2011-07-19T22:14:15","guid":{"rendered":"http:\/\/salientsoft.co.uk\/?p=1413"},"modified":"2011-07-19T23:01:17","modified_gmt":"2011-07-19T23:01:17","slug":"accessing-jsf-resource-bundles-from-java-code","status":"publish","type":"post","link":"https:\/\/salientsoft.co.uk\/?p=1413","title":{"rendered":"Accessing JSF Resource Bundles from Java Code"},"content":{"rendered":"<p>This is useful to be able to do. Typically you will want to declare a resource bundle in <em>faces-config.xml <\/em>to load it once for the application. You then refer to it from any JSF page that needs it.<\/p>\n<p>However, you often want to also load messages from code. An example might be the generation of custom faces messages or other error messages generated by the code.<\/p>\n<p>It is clearly therefore desirable to access the same message bundle that JSF has loaded, so that it is specified centrally, loaded once for the application, and reused by both JSF and Java.<\/p>\n<p><a href=\"http:\/\/www.andygibson.net\/blog\/article\/resource-bundles-in-jsf-2-0-applications\/\"><strong>This post here<\/strong><\/a> by Andy Gibson details how to do this. The code fragments are shown below, in case the link ever becomes broken:-<\/p>\n<p>\u00a0<\/p>\n<p><strong><span style=\"text-decoration: underline;\">Resource File<\/span><\/strong><\/p>\n<blockquote><p>firstName=First Name<br \/>\nlastName=Last Name<br \/>\nforgotPassword=Forgot Password?<br \/>\nusernameTaken={0} is already taken<\/p><\/blockquote>\n<p>\u00a0<\/p>\n<p><strong><span style=\"text-decoration: underline;\">faces-config.xml<\/span><\/strong><\/p>\n<blockquote><p>&lt;application&gt;<br \/>\n\u00a0\u00a0\u00a0 &lt;resource-bundle&gt;<br \/>\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 &lt;base-name&gt;org.fluttercode.resourcedemo.MessageResources&lt;\/base-name&gt;<br \/>\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 &lt;var&gt;msgs&lt;\/var&gt;<br \/>\n\u00a0\u00a0\u00a0 &lt;\/resource-bundle&gt;<br \/>\n&lt;\/application&gt;<\/p><\/blockquote>\n<p>\u00a0<\/p>\n<p><strong><span style=\"text-decoration: underline;\">Class MessageProvider<\/span><\/strong><\/p>\n<blockquote><p>public class MessageProvider {<\/p>\n<p>\u00a0\u00a0\u00a0 private ResourceBundle bundle;<\/p>\n<p>\u00a0\u00a0\u00a0 public ResourceBundle getBundle() {<br \/>\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 if (bundle == null) {<br \/>\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 FacesContext context = FacesContext.getCurrentInstance();<br \/>\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 bundle = context.getApplication().getResourceBundle(context, &#8220;msgs&#8221;);<br \/>\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 }<br \/>\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 return bundle;<br \/>\n\u00a0\u00a0\u00a0 }<\/p>\n<p>\u00a0\u00a0\u00a0 public String getValue(String key) {<\/p>\n<p>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 String result = null;<br \/>\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 try {<br \/>\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 result = getBundle().getString(key);<br \/>\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 } catch (MissingResourceException e) {<br \/>\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 result = &#8220;???&#8221; + key + &#8220;??? not found&#8221;;<br \/>\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 }<br \/>\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 return result;<br \/>\n\u00a0\u00a0\u00a0 }<\/p>\n<p>}<\/p><\/blockquote>\n<p>\n<strong><span style=\"text-decoration: underline;\">Backing Bean<\/span><\/strong><\/p>\n<blockquote><p>@Named<br \/>\n@RequestScoped<br \/>\npublic class SomeBean {<\/p>\n<p>\u00a0\u00a0\u00a0 public String getMessage() {<br \/>\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 String msg = new MessageProvider().getValue(&#8220;someMessage&#8221;);<br \/>\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 return MessageFormat.format(msg, &#8220;SomeValue&#8221;);<br \/>\n\u00a0\u00a0\u00a0 }<br \/>\n}<\/p><\/blockquote>\n<p>\n<strong><span style=\"text-decoration: underline;\">Example Code to generate a custom FacesMessage (from CoreJSF Edition 3, Page 332)<\/span><\/strong><\/p>\n<blockquote><p>public void validateDate(ComponentSystemEvent event) {<br \/>\n\u00a0\u00a0\u00a0 UIComponent source = event.getComponent();<br \/>\n\u00a0\u00a0\u00a0 UIInput dayInput = (UIInput) source.findComponent(&#8220;day&#8221;);<br \/>\n\u00a0\u00a0\u00a0 UIInput monthInput = (UIInput) source.findComponent(&#8220;month&#8221;);<br \/>\n\u00a0\u00a0\u00a0 UIInput yearInput = (UIInput) source.findComponent(&#8220;year&#8221;);<br \/>\n\u00a0\u00a0\u00a0 int d = ((Integer) dayInput.getLocalValue()).intValue();<br \/>\n\u00a0\u00a0\u00a0 int m = ((Integer) monthInput.getLocalValue()).intValue();<br \/>\n\u00a0\u00a0\u00a0 int y = ((Integer) yearInput.getLocalValue()).intValue();<\/p>\n<p>\u00a0\u00a0\u00a0 if (!isValidDate(d, m, y)) {<br \/>\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 <em><strong>FacesMessage message = com.corejsf.util.Messages.getMessage(<br \/>\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 &#8220;com.corejsf.messages&#8221;, &#8220;invalidDate&#8221;, null);<br \/>\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 message.setSeverity(FacesMessage.SEVERITY_ERROR);<br \/>\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 FacesContext context = FacesContext.getCurrentInstance();<br \/>\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 context.addMessage(source.getClientId(), message);<br \/>\n<\/strong><\/em>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 <em><strong>context.renderResponse();<br \/>\n<\/strong><\/em>\u00a0\u00a0\u00a0 }<\/p>\n<p>}<\/p><\/blockquote>\n","protected":false},"excerpt":{"rendered":"<p>This is useful to be able to do. Typically you will want to declare a resource bundle in faces-config.xml to load it once for the application. You then refer to it from any JSF page that needs it. However, you often want to also load messages from code. An example might be the generation of [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[22],"tags":[181,184,16,15],"_links":{"self":[{"href":"https:\/\/salientsoft.co.uk\/index.php?rest_route=\/wp\/v2\/posts\/1413"}],"collection":[{"href":"https:\/\/salientsoft.co.uk\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/salientsoft.co.uk\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/salientsoft.co.uk\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/salientsoft.co.uk\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1413"}],"version-history":[{"count":11,"href":"https:\/\/salientsoft.co.uk\/index.php?rest_route=\/wp\/v2\/posts\/1413\/revisions"}],"predecessor-version":[{"id":1423,"href":"https:\/\/salientsoft.co.uk\/index.php?rest_route=\/wp\/v2\/posts\/1413\/revisions\/1423"}],"wp:attachment":[{"href":"https:\/\/salientsoft.co.uk\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1413"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/salientsoft.co.uk\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1413"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/salientsoft.co.uk\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1413"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}