title: ResourceBundle from database to get key
body: <p>I would like to retrieve database value from a table.</p>
  
  <p>let's say i have table_message that looks like this :</p>
  
  <pre><code>| ID  |    KEY       |  MESSAGE           | 
  | 1   | app.label1   |  Your First Name   |
  | 2   | app.label2   |  Your Last Name    |  
  </code></pre>
  
  <p>then I would like to get Your <strong>First Name</strong> and Your <strong>Last Name</strong> through <strong>app.label1</strong> and <strong>app.label2</strong> using a JSP ( i use struts 2 frameworks ) that might look like this.</p>
  
  <pre><code>&lt;s:textfield label="app.label1" name="firstName"/&gt;
  &lt;s:textfield label="app.label2" name ="lastName"/&gt;
  </code></pre>
  
  <p>those text fields should populate with label <strong>Your First Name</strong> and <strong>Your last Name</strong> without requiring *.properties file.  </p>
  
  <p>Here is what I have done so far: </p>
  
  <p>ResourceTest.java</p>
  
  <pre><code>public class ResourceTest extends java.util.ResourceBundle  { 
  
      private Properties properties;      
  
      public ResourceManager(Properties inProperties)
      {
          properties = inProperties;
      }   
  
      @Override
      @SuppressWarnings(value = { "unchecked" })
      public Enumeration&lt;String&gt; getKeys() {
          return properties != null ? ((Enumeration&lt;String&gt;) properties.propertyNames()) : null;
      }    
  
      @Override
      protected Object handleGetObject(String key) {          
          return properties.getProperty(key);
      }       
  
      public static ResourceBundle.Control getMyControl(){
  
           return new ResourceBundle.Control() {
  
               @Override
                  public List&lt;String&gt; getFormats(String baseName){
                      if (baseName == null)
                      {
                          throw new NullPointerException();
                      }
                      return Arrays.asList("appdb");
                  }
  
                  @Override
                  public ResourceBundle newBundle(String baseName, 
                                                  Locale locale, 
                                                  String format, 
                                                  ClassLoader loader, 
                                                  boolean reload) 
                  throws IllegalAccessException,InstantiationException, IOException
                  {
                      if ((baseName == null) || (locale == null) || (format == null) || (loader == null))
                          throw new NullPointerException();
                          ResourceBundle bundle = null;
                      if (format.equals("appdb"))
                      {
                          Properties p = new Properties();
                          Connection con = null;
                          Statement s = null;
                          ResultSet rs = null;
  
                          try
                          { Class.forName("com.mysql.jdbc.Driver").newInstance(); 
                              String url ="jdbc:mysql://localhost:3306/appdb"; 
                              con = DriverManager.getConnection(url, "root", "root"); 
                              String sql = "Select key, msg from tbl_message";                            
  
                              s = con.createStatement();
                              rs = s.executeQuery(sql.toString());
                              while (rs.next())
                              {
                                  p.setProperty(rs.getString(1), rs.getString(2));
                              }
                          }
                          catch (Exception e)
                          {
                              e.printStackTrace();
                              throw new RuntimeException("Can not build properties: " + e);
                          }
                          finally
                          {                               
                              if (con != null) {
                                  try {
                                     con.close();
                                  } catch (Exception e) {
                                  }
                               }
  
                          }
                          bundle = new ResourceManager(p);
                      }
                      return bundle;
                  }
           };
       }
  </code></pre>
  
  <p>struts.xml</p>
  
  <pre><code>&lt;struts&gt;
  
  &lt;constant name="struts.custom.i18n.resources" value="com.application.web.common.ResourceTest" /&gt; 
  
      &lt;package name="resource" namespace="/" extends="struts-default"&gt;                
              &lt;action name="resourcetest"
                      class="com.application.web.controller.ResourceTestAction"
                      method="doSearchRequest"&gt;
                      &lt;result name="search"&gt;base/resource_test.jsp&lt;/result&gt;
              &lt;/action&gt;
      &lt;/package&gt;
  &lt;/struts&gt;   
  </code></pre>
  
  <p>resource_test.jsp</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="ISO-8859-1" ?&gt;
  &lt;%@ page language="java" contentType="text/html; charset=ISO-8859-1"
      pageEncoding="ISO-8859-1"%&gt;
  &lt;%@ taglib prefix="s" uri="/struts-tags"%&gt;
  
  &lt;!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"&gt;
  &lt;html xmlns="http://www.w3.org/1999/xhtml"&gt;
  &lt;head&gt;
  &lt;meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" /&gt;
  &lt;title&gt;Insert title here&lt;/title&gt;
  &lt;/head&gt;
  &lt;body&gt;
  
  &lt;h1&gt;Resource Key Page&lt;/h1&gt;
  
  &lt;s:textfield key="app.label1" name="firstName" /&gt;
  
  &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  
  <p>and here is my action class: </p>
  
  <pre><code>    public class ResourceAction extends ActionSupport {        
          private static final long serialVersionUID = -773740596147386242L;
  
          public String doSearchRequest() throws Exception {
  
              return "search";
          }
     }
  </code></pre>
  
  <p>the label still doesn't populate with the msg from the database.  if anyone knows how to do this, please let me know.  thanks </p>