gpt4 book ai didi

java - 通过实现 Spring 的 ThemeSource 类实现带有 hibernate 的 Spring 主题

转载 作者:行者123 更新时间:2023-12-04 06:12:54 25 4
gpt4 key购买 nike

有谁知道如何在 spring 中使用自定义 ThemeSource?我已经看到许多关于如何使用 ResourceBundleThemeSource 将主题与属性文件一起使用的示例。然而,我没有看到如何使用 hibernate 来存储各种属性(例如单个 css 属性),使用自定义 ThemeSource 读取属性,并且仍然能够在 jsp 中使用 spring 主题标签。

我知道我可以创建一个 Controller 并使用 hibernate 从数据库中获取这些属性,但我更想知道如何使用 Spring 的 ThemeSource 实现来做到这一点。

如果有人有任何想法或示例,我将不胜感激。

谢谢

最佳答案

要在您的 Web 应用程序中使用主题,您必须设置 org.springframework.ui.context.ThemeSource 的实现。界面。要使用自定义 ThemeSource 实现,您可以在应用程序上下文中使用保留名称 themeSource 注册一个 bean。 . Web 应用程序上下文会自动检测具有该名称的 bean 并使用它。

这是ThemeSource界面:

package org.springframework.ui.context;

public interface ThemeSource {
Theme getTheme(String themeName);
}

这里唯一的黑马是 Theme类型,实际上无非是:
package org.springframework.ui.context;

public interface Theme {
String getName();
MessageSource getMessageSource();
}

事实上, Theme 已经有了一个方便的实现。来自 Spring 的类型 => SimpleTheme

请注意,ThemeSource 需要 Spring 的 MessageSource ,这意味着存储在数据库中的主题属性,在您的情况下,需要“转换”以与 MessageSource 一起使用。界面。

你可以自己写 DatabaseDrivenMessageSource ,或者直接从 here 获取

现在,有了所有这些变量,这里是一个自定义 DatabaseThemeSource (这将成为 themeSource bean):
public class DatabaseThemeSource implements ThemeSource {

private YourThemeDao themeDao;

public Theme getTheme( String themeName ) {

if (themeName == null) { return null; }

MessageSource messageSource = new DatabaseDrivenMessageSource( themeDao );

theme = new SimpleTheme( themeName, messageSource );

return theme;
}

// init your themeDao
}

关于java - 通过实现 Spring 的 ThemeSource 类实现带有 hibernate 的 Spring 主题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7560002/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com