从数据库获取ResourceBundle获取密钥

时间:2017-03-05 10:48:14

标签: java jsp struts2

我想从表中检索数据库值。

让我说我的table_message看起来像这样:

location /newapp {
    alias /var/www/html;
}

然后我希望通过 app.label1 app.label2 名字姓氏 strong>使用可能如下所示的JSP(我使用struts 2框架)。

| ID  |    KEY       |  MESSAGE           | 
| 1   | app.label1   |  Your First Name   |
| 2   | app.label2   |  Your Last Name    |  

这些文字字段应填充标签您的名字您的姓氏,而不需要* .properties文件。

这是我到目前为止所做的:

ResourceTest.java

<s:textfield label="app.label1" name="firstName"/>
<s:textfield label="app.label2" name ="lastName"/>

struts.xml中

public class ResourceTest extends java.util.ResourceBundle  { 

    private Properties properties;      

    public ResourceManager(Properties inProperties)
    {
        properties = inProperties;
    }   

    @Override
    @SuppressWarnings(value = { "unchecked" })
    public Enumeration<String> getKeys() {
        return properties != null ? ((Enumeration<String>) 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<String> 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;
                }
         };
     }

resource_test.jsp

<struts>

<constant name="struts.custom.i18n.resources" value="com.application.web.common.ResourceTest" /> 

    <package name="resource" namespace="/" extends="struts-default">                
            <action name="resourcetest"
                    class="com.application.web.controller.ResourceTestAction"
                    method="doSearchRequest">
                    <result name="search">base/resource_test.jsp</result>
            </action>
    </package>
</struts>   

这是我的动作类:

<?xml version="1.0" encoding="ISO-8859-1" ?>
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<%@ taglib prefix="s" uri="/struts-tags"%>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" />
<title>Insert title here</title>
</head>
<body>

<h1>Resource Key Page</h1>

<s:textfield key="app.label1" name="firstName" />

</body>
</html>

标签仍未填充数据库中的msg。如果有人知道怎么做,请告诉我。谢谢

0 个答案:

没有答案