乐者为王

Do one thing, and do it well.

让Struts-Menu能访问WebWork的ResourceBundle

Struts-Menu默认是通过JSTL或者Struts来读取ResourceBundle的,这就需要在web.xml中配置要读取的properties文件(JSTL方式)或在struts-config.xml中配置(Struts方式)。 WebWork也有自己的ResourceBundle配置方式,而且Struts-Menu也不支持访问WebWork的ResourceBundle的功能。所以参考着DisplayTag的实现对Struts-Menu进行了一番手术,实现了可访问WebWork的ResourceBundle的功能。

下面是修改步骤:

  • 在Struts-Menu项目中增加net/sf/navigator/localization目录,将org.displaytag.localization中的*.java移到该目录中;
  • 将org.displaytag下的Message.java和messages.properties移到net/sf/navigator目录下;
  • 给MenuDisplayerMapping添加两个属性localeResover和ResourceProvider;
  • 在struts-menu.xml的Displayer标签下添加:
1
2
<setProperty property="localeResover" value="net.sf.navigator.localization.I18nWebworkAdapter" />
<setProperty property="resourceProvider" value="net.sf.navigator.localization.I18nWebworkAdapter" />
  • 在MenuDisplayer中添加以下四个接口方法,并在AbstractMenuDisplayer里实现它们:
1
2
3
4
public LocaleResolver getLocaleResolver();
public void setLocaleResolver(LocaleResolver localeResolver);
public I18nResourceProvider getResourceProvider();
public void setResourceProvider(I18nResourceProvider resourceProvider);
  • 在AbstractMenuDisplayer里添加protected的PageContext对象,并在init方法里初始化它;
  • 覆写MessageResourcesMenuDisplayer.getMessage方法,代码如下:
1
2
3
4
5
6
7
8
9
10
11
12
public String getMessage(String key) {
    String message = null;

    if (resourceProvider != null) {
        message = resourceProvider.getResource(key, "???", null, pageContext);
    }
    if (message == null) {
        message = key;
    }

    return message;
}
  • 在UseMenuDisplayerTag文件里添加代码(+后的代码是增加的):
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
  // get an instance of the menu displayer
  MenuDisplayer displayerInstance = null;
+ LocaleResolver localeResolver = null;
+ I18nResourceProvider resourceProvider = null;

  // default to use the config on the mapping
  if (displayerMapping.getConfig() != null) {
      // this value (config) is set on the displayer below
      setConfig(displayerMapping.getConfig());
  }
+ localeResolver =
+         (LocaleResolver) Class.forName(displayerMapping.getLocaleResover()).newInstance();
+ resourceProvider =
+         (I18nResourceProvider) Class.forName(displayerMapping.getLocaleProvider()).newInstance();

  displayerInstance.setConfig(config);
+ displayerInstance.setResourceProvider(resourceProvider);
+ displayerInstance.setLocaleResolver(localeResolver);

Comments