1
我想在我的項目中使用freemarker
作爲我的模板引擎,我的視圖是.JSP文件,但裏面沒有任何jsp代碼,我可以將它們更改爲.html,但我也試過了,我也得到了同樣的錯誤Spring MVC和freemarker java註釋配置
這裏是我的JavaConfig類
@Configuration
@EnableWebMvc
@ComponentScan({"configuracion", "controladores", "seguridad"})
public class ConfigMVC extends WebMvcConfigurerAdapter {
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/resources/**").addResourceLocations("/WEB-INF/resources/");
}
@Bean
public FreeMarkerConfigurer freemarkerConfig() {
FreeMarkerConfigurer freeMarkerConfigurer = new FreeMarkerConfigurer();
freeMarkerConfigurer.setTemplateLoaderPath("/WEB-INF/views/");
freeMarkerConfigurer.setDefaultEncoding("UTF-8");
return freeMarkerConfigurer;
}
@Bean
public FreeMarkerViewResolver viewResolver() {
FreeMarkerViewResolver viewResolver = new FreeMarkerViewResolver();
viewResolver.setPrefix("/WEB-INF/views/");
viewResolver.setSuffix(".jsp");
viewResolver.setCache(false); //Set to true during production
viewResolver.setContentType("text/html;charset=UTF-8");
viewResolver.setOrder(0);
return viewResolver;
}
}
這裏是我的初始化器類
public class WebInicializar extends AbstractAnnotationConfigDispatcherServletInitializer {
@Override
protected Class<?>[] getRootConfigClasses() {
return new Class[]{RootConfig.class};
}
@Override
protected Class<?>[] getServletConfigClasses() {
return new Class[]{ConfigMVC.class};
}
@Override
protected String[] getServletMappings() {
return new String[]{"/"};
}
@Override
protected Filter[] getServletFilters() {
Filter[] singleton = {new CORSFilter()};
return singleton;
}
@Override
protected DispatcherServlet createDispatcherServlet(WebApplicationContext servletAppContext) {
final DispatcherServlet dispatcherServlet = (DispatcherServlet) super.createDispatcherServlet(servletAppContext);
dispatcherServlet.setThrowExceptionIfNoHandlerFound(true);
return dispatcherServlet;
}
我的根配置
@Configuration
@ComponentScan(basePackages = {"controladores", "seguridad"})
@Import({ConfigMVC.class, SeguridadConfig.class})
public class RootConfig {
}
我的春天版本是4.2.5.RELEASE
和我的Freemarker版本是2.3.21
這是錯誤,當我勞克我的web應用程序
Advertencia: StandardWrapperValve[dispatcher]: Servlet.service() for
servlet dispatcher threw exception
javax.servlet.ServletException: Could not resolve view with name 'login' in servlet with name 'dispatcher'
at org.springframework.web.servlet.DispatcherServlet.render(DispatcherServlet.java:1226)
將您的視圖解析器後綴更改爲.html –