0

我可以轻松地在 XML 中进行配置,但在 @Configuration 中却不行。

特性

spring.view.class=org.springframework.web.servlet.view.JstlView
spring.view.prefix=/WEB-INF/jsp/
spring.view.suffix=.jsp

代码

@Bean
public UrlBasedViewResolver viewResolver() throws ClassNotFoundException {
  //ViewResolver Setting
  Class viewResolverClass = env.getPropertyAsClass("spring.view.class", Class.class);
  String viewResolverPrefix = env.getProperty("spring.view.prefix");
  String viewResolverSuffix = env.getProperty("spring.view.suffix");
  UrlBasedViewResolver viewResolver = new UrlBasedViewResolver();
  viewResolver.setViewClass(viewResolverClass);
  viewResolver.setPrefix(viewResolverPrefix);
  viewResolver.setSuffix(viewResolverSuffix);
  return viewResolver;
}

错误

throws java.lang.ClassNotFoundException] threw exception; nested exception is java.lang.IllegalArgumentException: Cannot convert value [org.springframework.web.servlet.view.JstlView] from source type [String] to target type [Class]

提前致谢。

4

1 回答 1

0

错误信息太明显了,说明一切:

无法将值...从源类型 [String] 转换为目标类型 [Class]

所以,你应该手动转换StringClass这样:

viewResolver.setViewClass(Class.forName(viewResolverClass));
于 2012-10-15T20:01:20.777 回答