当root URL命中时,Wildfly上的Spring不会呈现index.html

时间:2016-09-24 11:46:24

标签: java spring spring-mvc jboss

由于项目要求,我必须将Spring应用程序部署到无法运行Tomcat的服务器,并且只能运行WildFly。当我在Tomcat上运行一个非常简单的项目并且根URL被(localhost:8080)命中时,它呈现了我的index.html。自从迁移到WildFly并重构项目结构后,localhost:8080不再呈现index.html,但我 仍然可以访问其他网址。

我尝试在jboss-web.xml下实现BrassDucks/src/main/webapp/WEB-INF/jboss-web.xml这样的文件:

<jboss-web>
    <context-root>Brass-Ducks</context-root>
</jboss-web>

Brass-DucksartifactID,但无济于事。

考虑我的ApplicationConfig.java

package brass.ducks;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.support.SpringBootServletInitializer;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;


@Configuration
@ComponentScan
@EnableAutoConfiguration
public class ApplicationConfig extends SpringBootServletInitializer{

    public static void main(String[] args) {
        SpringApplication.run(ApplicationConfig.class, args);
    }

    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
        return application.sources(applicationClass);
    }

    private static Class<ApplicationConfig> applicationClass = ApplicationConfig.class;



}

@RestController
class GreetingController {

    @RequestMapping("/hello/{name}")
    String hello(@PathVariable String name) {
        return "Hello, " + name + "!";
    }
} 

并考虑我的Controller.java

package brass.ducks.application;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/")
public class Controller {

    @RequestMapping("/greet")
    @ResponseBody
    public String greeting() {
        return "Hello, there.";
    }
}

最后应该是相关的,我的文件夹结构:

hierarchy

localhost:8080/greet返回“Hello,there”,localhost:8080/hello/name返回“Hello name”。我该如何解决这个问题?

1 个答案:

答案 0 :(得分:0)

根据您的确切配置,有些内容应该有效:

@Controller
public class LandingPageController {

@RequestMapping({"/","/home"})
public String showHomePage(Map<String, Object> model) {
    return "/WEB-INF/index.html";   
   }
}

这将明确地将/映射到index.html