java - 索引文件如何影响你的 spring mvc 项目?

标签 java spring spring-mvc thymeleaf

我有一个 Spring MVC Web 应用程序项目,我正在使用 Spring Tools Suite 进行此项目,我使用 Maven 来添加我的依赖项,并使用 thymeleaf 来执行我的 View 。

我的项目配置为基于代码的方法,但我没有 xml 文件。

“我没有可以配置的默认页面”

<welcome-file-list>
    <welcome-file></welcome-file>
</welcome-file-list>

我的 webapp 文件夹中有一个 index.html 文件,里面有我所有的 css、js、ajax 文件夹,我没有使用 web-inf 文件夹来放置我的 .html 文件

我的问题是,当我输入此 URL http://localhost:8080/myapp/ 时我的应用程序应该转到该 Controller 负责的页面

@RequestMapping(value = "/")
public String home() {
    logger.info("***HOME***");

    return "home.jsp";
}

但是它会转到我的index.html页面而不是转到home.jsp,如果我像这样为我的index.html页面创建一个 Controller

    @RequestMapping(value = "/index")
    public String showIndex() {
       logger.info("***INDEX***");

        return "index.html";
    }

给我以下错误:

“HTTP 500 - 请求处理失败;嵌套异常为 org.thymeleaf.exceptions.TemplateInputException:解析文档时出现异常:template="index.html",第 56 行 - 第 4 列”

我认为这个错误是因为我正在使用 thymeleaf 并且在该行中我有一个 <img>标签打开而不关闭 </img>标签。

我的问题是,为什么我的index.html页面可以工作,如果我不创建一个 Controller 并且它工作得很好,但是如果我创建一个像我想的那样的 Controller ,它就不起作用。

我的配置类中只有一个viewResolver,并且是一个thymeleaf View 解析器,如果我只有一个解析器,这个index.html如何显示而没有问题

这是我的配置类

@EnableWebMvc
@ComponentScan(basePackages = {"com.abc.myapp"})
@Configuration
public class ConfigApp extends WebMvcConfigurerAdapter{

@Override
public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {
    configurer.enable();
}
@Bean 
    public ServletContextTemplateResolver templateResolver() {
        ServletContextTemplateResolver resolver = new ServletContextTemplateResolver();
        resolver.setPrefix("/");
 //     resolver.setSuffix(".html");
        resolver.setTemplateMode("HTML5");
        resolver.setOrder(1);
        resolver.setCacheable(false);
        return resolver;
    }

   @Bean
   public MessageSource messageSource() {
     ReloadableResourceBundleMessageSource messageSource = new                           ReloadableResourceBundleMessageSource();
    messageSource.setBasenames("classpath:messages/messages");
    messageSource.setUseCodeAsDefaultMessage(true);
    messageSource.setDefaultEncoding("UTF-8");
    messageSource.setCacheSeconds(0);// # -1 : never reload, 0 always reload
    return messageSource;
}
    @Bean 
    public SpringTemplateEngine templateEngine() {
        SpringTemplateEngine engine = new SpringTemplateEngine();
        engine.setTemplateResolver(templateResolver());
        engine.setMessageSource(messageSource());

        return engine;
    }

    @Bean 
    public ThymeleafViewResolver thymeleafViewResolver() {
        ThymeleafViewResolver resolver = new ThymeleafViewResolver();
        resolver.setTemplateEngine(templateEngine());
        resolver.setCache(false);
        return resolver;
    }

这是我的 webInitializer 类

public class InitiaApp extends AbstractAnnotationConfigDispatcherServletInitializer {

@Override
protected Class<?>[] getRootConfigClasses() {
    return new Class[] { ConfigRoot.class };
}

@Override
protected Class<?>[] getServletConfigClasses() {
    return new Class[] { ConfigApp .class };
}

@Override
protected String[] getServletMappings() {
    return new String[]{"/"};
}
}

和我的 configRoot 类

@Configuration
public class ConfiguracionRoot {

}

最佳答案

当您不使用 Controller 时,您不使用 Thymeleaf View 解析器。这意味着浏览器是翻译 html 文件的人,它更加宽容,并且不需要 HTML 5 的严格性。在使用 Controller 时,您被迫使用 Thymeleaf View 解析器。在您的 View 解析器中,您将模板模式设置为 HTML5。 Thymeleaf 还要求您使用格式良好的 XML,这意味着每个开放标签都需要一个结束标签。阅读 Thymeleaf 文档的 1.2 以获得进一步的说明。 http://www.thymeleaf.org/doc/usingthymeleaf.html

关于java - 索引文件如何影响你的 spring mvc 项目?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25557319/

相关文章:

java - 如何在 AsyncTask 中捕获内存不足异常

java - Apache 公共(public)守护进程 - procrun。停止服务挂起

java - 为应用程序中的所有线程设置 ThreadContext

spring - 使用@Scheduled 注释时 Autowiring 的条目为空?

java - 在 Spring Autowiring 时指定 map 的键

Java - 在哪里放置应用程序数据?

java - 如何在 Spring Boot 中获取索引的 json 作为响应?

java - Spring,多个Hibernate Sessionfactories配置

java - Spring 保存语言环境并在用户登录时更改语言环境

java - 使用相同 ID 的 Hibernate DB 对象映射时出错