【问题标题】:WelcomeController can't find path to index.htmlWelcomeController 找不到 index.html 的路径
【发布时间】:2016-02-08 13:13:19
【问题描述】:

我正在使用 SpringBoot MVC 模式开发一些 WebApplication。我有四个 maven 项目(DAO 项目、REST 项目(有用于启动应用程序的 SpringBoot 类)、SERVICE 项目和 CLIENT 项目)。这些项目通过依赖关系连接。

我的问题在于 CLIENT 项目。我有 WelcomeController,它看起来像这样:

package com.itengine.scoretracker.client.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
public class WelcomeController {

    @RequestMapping("/")
    public String welcome(){
        return "static/html/index.html";
    }
}

而我的 html 就在这条路径上:

当我将我的静态文件夹从 REST 项目中的 CLIENT 项目重新定位到同一位置时,我的 WelcomeController 会看到 index.html 并且一切正常。

所以请有人帮我解决这个问题,我真的需要这个 html 在 CLIENT 项目中。我没有配置 xml 的经验,因为我在没有那些 xml 的 SpringBoot 课程上学到了。

我的 web.xml 是空的,他们只有这个:

<!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application    2.3//EN" "java.sun.com/dtd/web-app_2_3.dtd"; > <web-app> <display-name>Archetype Created Web Application</display-name> </web-app>

我的主班是这样的:

package com.itengine.scoretracker.rest.init;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.context.web.SpringBootServletInitializer;
import org.springframework.boot.orm.jpa.EntityScan;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;


@ComponentScan("com.itengine")
@EntityScan("com.itengine")
@EnableJpaRepositories("com.itengine.scoretracker.dao.repository")
@SpringBootApplication
public class ScoreTrackerApplication
    extends SpringBootServletInitializer{

    @Override
    protected SpringApplicationBuilder configure(
            SpringApplicationBuilder builder){

        return builder.sources(ScoreTrackerApplication.class);
    }

    public static void main(String[] args) {

        SpringApplication.run(ScoreTrackerApplication.class, args);

    }
}

提前致谢!

【问题讨论】:

  • 这个类 ScoreTrackerApplication 在你的客户项目中吗?
  • 不,她在 Rest 项目中
  • 所以到目前为止,客户端还不是 Spring Boot 项目。只需在客户端创建一个类并使用 @SpringBootApplication 注释它
  • 我这样做了,这解决了我的问题。 Tnx @BalajiKrishnan

标签: java maven spring-mvc spring-boot


【解决方案1】:

static 文件夹移动到src/main/resources,以便它最终部署在类路径上。然后可以删除/ 的映射。

【讨论】:

    【解决方案2】:

    如果您的index.html 是纯html,则不需要您的控制器。

    只需将index.html 放入src/main/resources/static

    这就是你所需要的。 Spring Boot 负责其余的工作。

    您也不需要 web.xml。

    【讨论】:

      【解决方案3】:

      现在,就像你说的@Reimeus,我有这种情况:

      那是行不通的。那么移除/映射呢??

      【讨论】:

      • 可能查看解析器问题.. 就像@Reimeus 说的,src/main/resources 下的静态文件夹应该在spring boot 中工作
      • 在我的 web.xml 我只有这个:ttp://java.sun.com/dtd/web-app_2_3.dtd" rel="nofollow" target="_blank">java.sun.com/dtd/web-app_2_3.dtd" > 原型创建的 Web 应用程序
      • 用@SpringBootApplication注解的配置类在哪里
      猜你喜欢
      • 2019-06-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-09-06
      • 1970-01-01
      • 2013-05-27
      相关资源
      最近更新 更多