gpt4 book ai didi

java - Spring MVC 和 freemarker java 注解配置

转载 作者:行者123 更新时间:2023-11-30 07:19:46 24 4
gpt4 key购买 nike

我正在尝试使用 freemarker 作为我的项目中的模板引擎,我的 View 是 .JSP 文件,但其中没有任何 jsp 代码,我可以将它们更改为 .html 但我也尝试了一下,我得到了同样的错误

这是我的 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 {

}

我的 Spring 版本是 4.2.5.RELEASE,我的 freeMarker 版本是 2.3.21

这是我启动网络应用程序时出现的错误

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)

最佳答案

Freemarker 使用 FreemarkerConfigurer 中设置的前缀。您需要从 FreeMarkerViewResolver 中删除 setPrefix

关于java - Spring MVC 和 freemarker java 注解配置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37780457/

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