我已经在DispatcherServlet xml文件中配置了视图解析器。但如何在spring启动时使用注释来配置视图解析器?如何在spring启动时使用注释配置视图解析器?
0
A
回答
4
If you want to keep Spring Boot MVC features, and you just want to add additional MVC configuration (interceptors, formatters, view controllers etc.) you can add your own @Configuration class of type WebMvcConfigurerAdapter, but without @EnableWebMvc. If you wish to provide custom instances of RequestMappingHandlerMapping, RequestMappingHandlerAdapter or ExceptionHandlerExceptionResolver you can declare a WebMvcRegistrationsAdapter instance providing such components.
If you want to take complete control of Spring MVC, you can add your own @Configuration annotated with @EnableWebMvc.
你可以这样做:
@Configuration
public class WebConfig extends WebMvcConfigurerAdapter {
@Bean
public ViewResolver getViewResolver() {
InternalResourceViewResolver resolver = new InternalResourceViewResolver();
resolver.setPrefix("/");
resolver.setSuffix(".html");
return resolver;
}
}
当然,适应前缀,并根据您的实际配置的后缀。
编辑处理重定向页面时/
是要求:
@Configuration
public class WebConfig extends WebMvcConfigurerAdapter {
@Bean
public ViewResolver getViewResolver() {
InternalResourceViewResolver resolver = new InternalResourceViewResolver();
resolver.setPrefix("/");
resolver.setSuffix(".html");
return resolver;
}
// add a mapping for redirection to index when/requested
@Override
public void addViewControllers(ViewControllerRegistry registry) {
registry.addViewController("/").setViewName("forward:/index");
}
}
相关问题
- 1. 如何在Spring 3中使用注释配置RESTful控制器?
- 2. Spring 3注释配置启动@Configuration和@Component但不启动@Controller
- 3. Spring如何解析视图?
- 4. Spring和视图解析器
- 5. 如何在添加mvc:注释驱动的同时在Spring MVC中解析XmlBeanDefinitionStoreException?
- 6. NoUniqueBeanDefinitionException在Spring注解驱动的配置
- 7. 使用spring-java-config设置弹簧视图解析器
- 8. 如何使用配置解析器
- 9. 如何在spring4中配置多个视图解析器?
- 10. 如何在使用JDOM2解析XML时忽略注释内容
- 11. 如何在使用@Transactional注释时配置我的Spring-JUnit测试?
- 12. @Transaction @Service在Spring注释配置
- 13. 如何在解释器启动时使Julia变得冗长?
- 14. spring使用带注释的端点时找不到适配器
- 15. Spring注解和XML配置
- 16. 使用Jsoup HTML解析器解析注释标记
- 17. XML解析器的配置与Spring
- 18. Spring XML查看解析器配置
- 19. Spring MVC-如何解析视图?
- 20. 在Pharo中使用PetitParser解析注释
- 21. 如何使用VBA在Word启动时设置视图
- 22. Swift:如何启用和禁用地图视图上的注释?
- 23. 在启动集群时在EMR上配置Zeppelin的Spark解释器
- 24. JPA与Spring MVC通过注释配置
- 25. Spring MVC和freemarker java注释配置
- 26. Spring Mongo DB基于注释的配置
- 27. 有关web.xml的Spring Security注释配置
- 28. Spring MVC注释vs xml配置
- 29. 在spring boot @activeprofile注释中配置maven配置文件
- 30. 在tinyXML2中解析注释
我在根文件夹中的index.html。当我运行localhost:8080时,我想要打开index.html。我如何做到这一点 –
你可以添加一个映射来做重定向。我只是在我的答案中添加了这个来说明如何去做。但是,如果index.html位于根目录下,通常不需要它。 – davidxxx
但它显示我这个错误:此应用程序没有明确的映射/错误,所以你看到这是一个后备。 –