spring boot theme无法启用

scl33 发布于 2015/07/07 22:36
阅读 1K+
收藏 0

【开源中国 APP 全新上线】“动弹” 回归、集成大模型对话、畅读技术报告”

希望在spring boot 1.2.4 中加入ResourceBundleThemeSource管理主题资源, 成功启动后访问freemarker视图, 读取主题资源报错, 日志中看到, spring仅查找了messageSource而未查找themeSource, 有人碰到过此问题吗?

我尝试过在WebMvcConfigurerAdapter中通过@Bean直接将ResourceBundleThemeSource拿如管理, 无论是加入了ResourceBundleThemeSource还是LocaleMessageSource启动都会报错.

随后仿造WebMvcAutoConfiguration那个类, 修改代码将ResourceBundleThemeSource纳入spring管理, 启动成功, 且看到autoconfig正确报告, 且找到我配置的资源, 但读取时, 日志中看出, 仅查找了MessageSource, 未查找ThemeSource

日志好像是这样的(全凭记忆)

could not found resource name defaultThemeName for zh_CN // 这里未查找theme资源而只查找了message资源


求大神解答!!

加载中
0
pantrick
pantrick
这个theme有啥用
0
scl33
scl33

经过摸索, 终于发现病灶....改太急....

@Bean
    public ThemeSource messageSource() {
        ResourceBundleThemeSource messageSource = new ResourceBundleThemeSource();
        if (StringUtils.hasText(this.basename)) {
            messageSource.setBasenamePrefix(trimAllWhitespace(this.basename));
        }
        return messageSource;
    }



改为

@Bean
	public ThemeSource themeSource() {
		ResourceBundleThemeSource themeSource = new ResourceBundleThemeSource();
		if (StringUtils.hasText(this.basename)) {
			themeSource.setBasenamePrefix(trimAllWhitespace(this.basename));
		}
		return themeSource;
	}



就好了

0
scl33
scl33

引用来自“逝水fox”的评论

得把你配置发出来,看起来像是什么配置有问题
@ 逝水fox, 配置已发
0
scl33
scl33

配置如下

spring.messages.basename=config/i18n
spring.messages.encoding="UTF-8"
spring.messages.cacheSeconds=-1
spring.theme.basename=config/theme-




package com.vip.ygd.common;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;
import org.springframework.web.servlet.i18n.CookieLocaleResolver;
import org.springframework.web.servlet.i18n.LocaleChangeInterceptor;
import org.springframework.web.servlet.theme.CookieThemeResolver;
import org.springframework.web.servlet.theme.ThemeChangeInterceptor;

@Configuration
public class MvcConfig extends WebMvcConfigurationSupport {
	@Bean
	public HandlerInterceptorAdapter requestDefValueInterceptor(){
		return new HandlerInterceptorAdapter() {
			@Override
		    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
		        throws ServletException {
		    	request.setAttribute("base", request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+request.getContextPath());
		    	request.setAttribute("user", request.getSession().getAttribute("user"));
		        return true;
		    }

			@Override
			public void afterCompletion(HttpServletRequest request,
					HttpServletResponse response, Object handler, Exception ex)
					throws Exception {
				if (ex!=null){
					//send mail and log this
				}
				super.afterCompletion(request, response, handler, ex);
			}
			
		};
	}
	
	@Bean
	public LocaleChangeInterceptor localeChangeInterceptor(){
		return new LocaleChangeInterceptor();
	}
	
	@Bean
	public ThemeChangeInterceptor themeChangeInterceptor(){
		return new ThemeChangeInterceptor();
	}

	@Override
	protected void addInterceptors(InterceptorRegistry registry) {
		registry.addInterceptor(requestDefValueInterceptor());
		registry.addInterceptor(themeChangeInterceptor());
		registry.addInterceptor(localeChangeInterceptor());
	}
	
	@Bean(name="themeResolver")
	public CookieThemeResolver themeResolver(){
		CookieThemeResolver resolver = new CookieThemeResolver();
		resolver.setCookieMaxAge(31536000);
		resolver.setCookieName("clienttheme");
		resolver.setDefaultThemeName("defaultThemeName");
		return resolver;
	}
	
	@Bean(name="localeResolver")
	public CookieLocaleResolver localeResolver(){
		CookieLocaleResolver resolver = new CookieLocaleResolver();
		resolver.setCookieName("locale");
		resolver.setCookieMaxAge(31536000);
		return resolver;
	}
	
//	@Bean(name="themeSource")
//	public ResourceBundleThemeSource themeSource(){
//		ResourceBundleThemeSource themeSource = new ResourceBundleThemeSource();
//		themeSource.setBasenamePrefix("classpath:config/theme-");
//		return themeSource;
//	}
	
//	@Bean(name="messageSource")
//	public ReloadableResourceBundleMessageSource messageSource(){
//		ReloadableResourceBundleMessageSource bundle = new ReloadableResourceBundleMessageSource();
//		bundle.setBasename("classpath:config/i18n");
//		bundle.setDefaultEncoding("UTF-8");
//		bundle.setCacheSeconds(0);
//		return bundle;
//	}
}



package com.vip.ygd.common;

import static org.springframework.util.StringUtils.commaDelimitedListToStringArray;
import static org.springframework.util.StringUtils.trimAllWhitespace;

import java.io.IOException;
import java.util.Iterator;
import java.util.Set;

import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.condition.ConditionOutcome;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.SpringBootCondition;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.MessageSource;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ConditionContext;
import org.springframework.context.annotation.Conditional;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.Ordered;
import org.springframework.core.annotation.Order;
import org.springframework.core.io.Resource;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.core.type.AnnotatedTypeMetadata;
import org.springframework.ui.context.ThemeSource;
import org.springframework.ui.context.support.ResourceBundleThemeSource;
import org.springframework.util.ConcurrentReferenceHashMap;
import org.springframework.util.StringUtils;

import com.vip.ygd.common.ThemeAutoConfig.ResourceThemeBundleCondition;

/**
 * {@link EnableAutoConfiguration Auto-configuration} for {@link MessageSource}.
 *
 * @author Dave Syer
 * @author Phillip Webb
 */
@Configuration
@ConditionalOnMissingBean(ThemeSource.class)
@Order(Ordered.HIGHEST_PRECEDENCE)
@Conditional(ResourceThemeBundleCondition.class)
@EnableConfigurationProperties
@ConfigurationProperties(prefix = "spring.theme")
public class ThemeAutoConfig {

	private static final Resource[] NO_RESOURCES = {};

	/**
	 * Comma-separated list of basenames, each following the ResourceBundle convention.
	 * Essentially a fully-qualified classpath location. If it doesn't contain a package
	 * qualifier (such as "org.mypackage"), it will be resolved from the classpath root.
	 */
	private String basename = "messages";

	/**
	 * Message bundles encoding.
	 */
	private String encoding = "utf-8";

	/**
	 * Loaded resource bundle files cache expiration, in seconds. When set to -1, bundles
	 * are cached forever.
	 */
	private int cacheSeconds = -1;

	@Bean
	public ThemeSource messageSource() {
		ResourceBundleThemeSource messageSource = new ResourceBundleThemeSource();
		if (StringUtils.hasText(this.basename)) {
			messageSource.setBasenamePrefix(trimAllWhitespace(this.basename));
		}
		return messageSource;
	}

	public String getBasename() {
		return this.basename;
	}

	public void setBasename(String basename) {
		this.basename = basename;
	}

	public String getEncoding() {
		return this.encoding;
	}

	public void setEncoding(String encoding) {
		this.encoding = encoding;
	}

	public int getCacheSeconds() {
		return this.cacheSeconds;
	}

	public void setCacheSeconds(int cacheSeconds) {
		this.cacheSeconds = cacheSeconds;
	}

	protected static class ResourceThemeBundleCondition extends SpringBootCondition {

		private static ConcurrentReferenceHashMap<String, ConditionOutcome> cache = new ConcurrentReferenceHashMap<String, ConditionOutcome>();

		@Override
		public ConditionOutcome getMatchOutcome(ConditionContext context,
				AnnotatedTypeMetadata metadata) {
			String basename = context.getEnvironment().getProperty(
					"spring.theme.basename", "messages");
			ConditionOutcome outcome = cache.get(basename);
			if (outcome == null) {
				outcome = getMatchOutcomeForBasename(context, basename);
				cache.put(basename, outcome);
			}
			return outcome;
		}

		private ConditionOutcome getMatchOutcomeForBasename(ConditionContext context,
				String basename) {
			for (String name : commaDelimitedListToStringArray(trimAllWhitespace(basename))) {
				for (Resource resource : getResources(context.getClassLoader(), name)) {
					if (resource.exists()) {
						return ConditionOutcome.match("Bundle found for "
								+ "spring.theme.basename: " + name);
					}
				}
			}
			return ConditionOutcome.noMatch("No bundle found for "
					+ "spring.theme.basename: " + basename);
		}

		private Resource[] getResources(ClassLoader classLoader, String name) {
			try {
				return new SkipPatternPathMatchingResourcePatternResolver(classLoader)
						.getResources("classpath*:" + name + "*.properties");
			}
			catch (Exception ex) {
				return NO_RESOURCES;
			}
		}

	}

	/**
	 * {@link PathMatchingResourcePatternResolver} that skips well known JARs that don't
	 * contain messages.properties.
	 */
	private static class SkipPatternPathMatchingResourcePatternResolver extends
			PathMatchingResourcePatternResolver {

		private static final ClassLoader ROOT_CLASSLOADER;
		static {
			ClassLoader classLoader = null;
			try {
				classLoader = ClassLoader.getSystemClassLoader();
				while (classLoader.getParent() != null) {
					classLoader = classLoader.getParent();
				}
			}
			catch (Throwable ex) {
			}
			ROOT_CLASSLOADER = classLoader;
		}

		private static final String[] SKIPPED = { "aspectjweaver-", "hibernate-core-",
				"hsqldb-", "jackson-annotations-", "jackson-core-", "jackson-databind-",
				"javassist-", "snakeyaml-", "spring-aop-", "spring-beans-",
				"spring-boot-", "spring-boot-actuator-", "spring-boot-autoconfigure-",
				"spring-core-", "spring-context-", "spring-data-commons-",
				"spring-expression-", "spring-jdbc-", "spring-orm-", "spring-tx-",
				"spring-web-", "spring-webmvc-", "tomcat-embed-", "joda-time-",
				"hibernate-entitymanager-", "hibernate-validator-", "logback-classic-",
				"logback-core-", "thymeleaf-" };

		public SkipPatternPathMatchingResourcePatternResolver(ClassLoader classLoader) {
			super(classLoader);
		}

		@Override
		protected void addAllClassLoaderJarRoots(ClassLoader classLoader,
				Set<Resource> result) {
			if (classLoader != ROOT_CLASSLOADER) {
				super.addAllClassLoaderJarRoots(classLoader, result);
			}
		}

		@Override
		protected Set<Resource> doFindAllClassPathResources(String path)
				throws IOException {
			Set<Resource> resources = super.doFindAllClassPathResources(path);
			for (Iterator<Resource> iterator = resources.iterator(); iterator.hasNext();) {
				Resource resource = iterator.next();
				for (String skipped : SKIPPED) {
					if (resource.getFilename().startsWith(skipped)) {
						iterator.remove();
						break;
					}
				}
			}
			return resources;
		}

	}

}



0
scl33
scl33

这两行很可疑, 为什么主题资源是这个类org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext

2015-07-08 09:29:07.612 DEBUG 13264 --- [nio-8080-exec-1] o.s.w.s.view.freemarker.FreeMarkerView   : Added model object 'org.springframework.web.servlet.DispatcherServlet.THEME_SOURCE' of type [org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext] to request in view with name 'common/test.html'


2015-07-08 09:29:07.667  WARN 13264 --- [nio-8080-exec-1] o.s.c.s.ResourceBundleMessageSource      : ResourceBundle [defaultThemeName] not found for MessageSource: Can't find bundle for base name defaultThemeName, locale en_US

0
scl33
scl33

日志

2015-07-08 09:29:07.611 DEBUG 13264 --- [nio-8080-exec-1] o.s.w.s.view.freemarker.FreeMarkerView   : Exposing request attribute 'base' with value [http://localhost:8080] to model
2015-07-08 09:29:07.612 DEBUG 13264 --- [nio-8080-exec-1] o.s.w.s.view.freemarker.FreeMarkerView   : Added model object 'message' of type [java.lang.String] to request in view with name 'common/test.html'
2015-07-08 09:29:07.612 DEBUG 13264 --- [nio-8080-exec-1] o.s.w.s.view.freemarker.FreeMarkerView   : Added model object 'request' of type [org.springframework.web.servlet.support.RequestContext] to request in view with name 'common/test.html'
2015-07-08 09:29:07.612 DEBUG 13264 --- [nio-8080-exec-1] o.s.w.s.view.freemarker.FreeMarkerView   : Added model object 'org.springframework.web.context.request.async.WebAsyncManager.WEB_ASYNC_MANAGER' of type [org.springframework.web.context.request.async.WebAsyncManager] to request in view with name 'common/test.html'
2015-07-08 09:29:07.612 DEBUG 13264 --- [nio-8080-exec-1] o.s.w.s.view.freemarker.FreeMarkerView   : Added model object 'org.springframework.web.servlet.DispatcherServlet.CONTEXT' of type [org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext] to request in view with name 'common/test.html'
2015-07-08 09:29:07.612 DEBUG 13264 --- [nio-8080-exec-1] o.s.w.s.view.freemarker.FreeMarkerView   : Added model object 'org.springframework.web.servlet.resource.ResourceUrlProvider' of type [org.springframework.web.servlet.resource.ResourceUrlProvider] to request in view with name 'common/test.html'
2015-07-08 09:29:07.612 DEBUG 13264 --- [nio-8080-exec-1] o.s.w.s.view.freemarker.FreeMarkerView   : Added model object 'characterEncodingFilter.FILTERED' of type [java.lang.Boolean] to request in view with name 'common/test.html'
2015-07-08 09:29:07.612 DEBUG 13264 --- [nio-8080-exec-1] o.s.w.s.view.freemarker.FreeMarkerView   : Added model object 'org.springframework.web.servlet.DispatcherServlet.THEME_SOURCE' of type [org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext] to request in view with name 'common/test.html'
2015-07-08 09:29:07.612 DEBUG 13264 --- [nio-8080-exec-1] o.s.w.s.view.freemarker.FreeMarkerView   : Added model object 'org.springframework.web.servlet.DispatcherServlet.LOCALE_RESOLVER' of type [org.springframework.web.servlet.i18n.CookieLocaleResolver] to request in view with name 'common/test.html'
2015-07-08 09:29:07.613 DEBUG 13264 --- [nio-8080-exec-1] o.s.w.s.view.freemarker.FreeMarkerView   : Added model object 'org.springframework.web.servlet.i18n.CookieLocaleResolver.LOCALE' of type [java.util.Locale] to request in view with name 'common/test.html'
2015-07-08 09:29:07.613 DEBUG 13264 --- [nio-8080-exec-1] o.s.w.s.view.freemarker.FreeMarkerView   : Added model object 'org.springframework.web.servlet.HandlerMapping.bestMatchingPattern' of type [java.lang.String] to request in view with name 'common/test.html'
2015-07-08 09:29:07.613 DEBUG 13264 --- [nio-8080-exec-1] o.s.w.s.view.freemarker.FreeMarkerView   : Added model object 'org.springframework.web.servlet.DispatcherServlet.OUTPUT_FLASH_MAP' of type [org.springframework.web.servlet.FlashMap] to request in view with name 'common/test.html'
2015-07-08 09:29:07.613 DEBUG 13264 --- [nio-8080-exec-1] o.s.w.s.view.freemarker.FreeMarkerView   : Added model object 'org.springframework.web.servlet.HandlerMapping.pathWithinHandlerMapping' of type [java.lang.String] to request in view with name 'common/test.html'
2015-07-08 09:29:07.613 DEBUG 13264 --- [nio-8080-exec-1] o.s.w.s.view.freemarker.FreeMarkerView   : Added model object 'org.springframework.web.servlet.DispatcherServlet.FLASH_MAP_MANAGER' of type [org.springframework.web.servlet.support.SessionFlashMapManager] to request in view with name 'common/test.html'
2015-07-08 09:29:07.613 DEBUG 13264 --- [nio-8080-exec-1] o.s.w.s.view.freemarker.FreeMarkerView   : Added model object 'org.springframework.web.servlet.HandlerMapping.uriTemplateVariables' of type [java.util.LinkedHashMap] to request in view with name 'common/test.html'
2015-07-08 09:29:07.613 DEBUG 13264 --- [nio-8080-exec-1] o.s.w.s.view.freemarker.FreeMarkerView   : Added model object 'org.springframework.web.servlet.DispatcherServlet.THEME_RESOLVER' of type [org.springframework.web.servlet.theme.CookieThemeResolver] to request in view with name 'common/test.html'
2015-07-08 09:29:07.613 DEBUG 13264 --- [nio-8080-exec-1] o.s.w.s.view.freemarker.FreeMarkerView   : Added model object 'org.springframework.core.convert.ConversionService' of type [org.springframework.format.support.DefaultFormattingConversionService] to request in view with name 'common/test.html'
2015-07-08 09:29:07.613 DEBUG 13264 --- [nio-8080-exec-1] o.s.w.s.view.freemarker.FreeMarkerView   : Added model object 'base' of type [java.lang.String] to request in view with name 'common/test.html'
2015-07-08 09:29:07.613 DEBUG 13264 --- [nio-8080-exec-1] o.s.w.s.view.freemarker.FreeMarkerView   : Added model object 'springMacroRequestContext' of type [org.springframework.web.servlet.support.RequestContext] to request in view with name 'common/test.html'
2015-07-08 09:29:07.615 DEBUG 13264 --- [nio-8080-exec-1] o.s.w.s.view.freemarker.FreeMarkerView   : Rendering FreeMarker template [common/test.html] in FreeMarkerView 'common/test.html'
2015-07-08 09:29:07.667  WARN 13264 --- [nio-8080-exec-1] o.s.c.s.ResourceBundleMessageSource      : ResourceBundle [defaultThemeName] not found for MessageSource: Can't find bundle for base name defaultThemeName, locale en_US


0
逝水fox
逝水fox
得把你配置发出来,看起来像是什么配置有问题
0
scl33
scl33
明天将日志和配置贴上来
OSCHINA
登录后可查看更多优质内容
返回顶部
顶部