我有一个带有 spring java-config 设置的单页 Web 应用程序。目前,我必须输入 localhost/resources/index.html 才能访问我的应用程序。我想对其进行设置,以便只需输入 localhost
即可看到 index.html
页面。
这是我的 WebConfig。我不知道这是否是我需要搞乱的地方,但我尝试添加 registry.addResourceHandler("/").addResourceLocations("resources/index.html")
但那没有'不工作。有任何想法吗?我是否以错误的方式处理这个问题?
@Configuration
@EnableWebMvc
@ComponentScan(basePackages = "com.example.web")
public class WebConfiguration extends WebMvcConfigurerAdapter
{
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry)
{
registry.addResourceHandler("/resources/**").addResourceLocations("resources/").setCachePeriod(31556926);
registry.setOrder(Ordered.HIGHEST_PRECEDENCE);
}
...
}
最佳答案
localhost 是您的本地服务器。您无法启动您的应用程序。每当您部署应用程序时,它都是本地主机上的上下文。例如您开发了一个名为 HelloWorld 的 Web 应用程序,那么访问该应用程序的最小 URL 将为 http://localhost :/ Hello World 为了默认使用index.html,请将以下内容(如果尚未存在)添加到您的web.xml
<welcome-file-list>
<welcome-file>index.html</welcome-file>
</welcome-file-list>
关于java - 作为 root 的 Spring 资源文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28905490/