我是Struts2瓷砖的新手。我想写有多个页面并支持多种语言的网站。许多页面将具有相同的布局(因此使用图块)只有登录页面具有不同的布局。
我已经搜索过从数据库中读取特定语言的数据,并准备好了一个方法。我已准备好基础布局,但我无法理解如何整合国际化和瓷砖。
附加基本布局和数据库方法
DBResourceBundle.java
package com.ruukki.webui.util;
import java.io.IOException;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.Arrays;
import java.util.Enumeration;
import java.util.List;
import java.util.Locale;
import java.util.Properties;
import java.util.ResourceBundle;
import com.ruukki.webui.vo.ConnectionType;
import com.ruukki.webui.vo.User;
public class DbResourceBundle extends ResourceBundle
{
private Properties properties;
public DbResourceBundle(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("db");
}
@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("db"))
{
Properties p = new Properties();
Connection con = null;
Statement s = null;
ResultSet rs = null;
try
{
con = LocalDbManager.getConnectionForUser(new User("sa","")) ;
StringBuilder query = new StringBuilder();
query.append("SELECT * FROM Language_Text");
int value =3;
if (locale != null)
{
System.out.println("locale has vlue " + locale.getLanguage());
if(locale.getLanguage().equals("en")){
value = 3;
}else if(locale.getLanguage().equals("fi")){
value = 4;
}
}
s = con.createStatement();
System.out.println("stmt:" + s);
rs = s.executeQuery(query.toString());
System.out.println("Executed Query and got data:" + query);
System.out.println("Column count" );
while (rs.next())
{
p.setProperty(rs.getString(1), rs.getString(value));
}
System.out.println("Is property Filleked" + p.isEmpty());
}
catch (Exception e)
{
System.out.println("failed to build properties file");
e.printStackTrace();
throw new RuntimeException("Can not build properties: " + e);
}
finally
{
ConnectionManager.closeQuietly(con, s, rs);
}
bundle = new DbResourceBundle(p);
}
return bundle;
}
@Override
public long getTimeToLive(String baseName, Locale locale)
{
return 1000 * 60 * 30;
}
@Override
public boolean needsReload(String baseName, Locale locale, String format, ClassLoader loader, ResourceBundle bundle, long loadTime)
{
return true;
}
};
}
}
tiles.xml
<!DOCTYPE tiles-definitions PUBLIC
"-//Apache Software Foundation//DTD Tiles Configuration 2.0//EN"
"http://tiles.apache.org/dtds/tiles-config_2_0.dtd">
<tiles-definitions>
<definition name="baseLayout" template="/baseLayout.jsp">
<put-attribute name="title" value="Template"/>
<put-attribute name="header" value="/header.jsp"/>
<put-attribute name="menu" value="/menu.jsp"/>
<put-attribute name="body" value="/body.jsp"/>
<put-attribute name="footer" value="/footer.jsp"/>
</definition>
</tiles-definitions>
header.jsp中
<%@ page contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags" %>
<head>
<link type="text/css" rel="stylesheet" href="ruukki.css"/>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
</head>
<body>
<div id="header">
<table width="100%">
<tr>
<td width="10%"><div id="Logo" > <img src="logo.png"> </div></td>
<td width="50%"><div id="Welcome_Portal"> Welcome to Portal !!! </div>
<td width="15%"><p align="left" style="vertical-align: bottom;">Welcome, <s:property value="username"/></p></td>
<td width="2%" align="center"><img alt="" src="Image/flag_en.png"></a></td>
<td width="2%" align="center"><img alt="" src="Image/flag_fin.png"></td>
<td width="2%" align="center"><img alt="" src="Image/flag_sw.png"></td>
<td width="5%"><s:url id="logout" action="logout"/>
<s:a href="%{logout}" target="_parent">Logout </s:a>
</td>
</tr>
</table>
</div>
</body>
strtus.xml
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<package name="default" extends="struts-default">
<result-types>
<result-type name="tiles" class="org.apache.struts2.views.tiles.TilesResult" />
</result-types>
<action name="*Link" method="{1}" class="com.tutorials4u.actions.LinkAction">
<result name="welcome" type="tiles">welcome</result>
<result name="struts" type="tiles">struts</result>
</action>
</package>
</struts>
baseLayout.jsp
<%@ taglib uri="http://tiles.apache.org/tags-tiles" prefix="tiles" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title><tiles:insertAttribute name="title" ignore="true" /></title>
</head>
<body>
<table border="0" cellpadding="2" cellspacing="2" align="center" width="100%" height="100%">
<tr>
<td height="10%" colspan="2">
<tiles:insertAttribute name="header" />
</td>
</tr>
<tr>
<td width="20%" height="80%">
<tiles:insertAttribute name="menu" />
</td>
<td width="80%" height="80%" style="min-height: 120px">
<tiles:insertAttribute name="body" />
</td>
</tr>
<tr>
<td height="10%" colspan="2">
<tiles:insertAttribute name="footer" />
</td>
</tr>
</table>
</body>
</html>
随着语言的改变,我想根据所选语言从标题中显示内容。
目前在页面中尝试使用此语言内容
<%
ResourceBundle lbls = null;
ResourceBundle msgs = null;
try {
String value = Globals.LOCALE_KEY.toString();
System.out.println("value: " + value);
Locale locale = new Locale("fi","");
lbls = ResourceBundle.getBundle("AwesomeBundle", locale, DbResourceBundle.getMyControl());
} catch (Exception ex) {
}
%>
但我希望它能在所有页面中使用......所以有点困惑。如果这是一个太愚蠢的问题,我很抱歉。