java - Spring 4 java配置,前后端分离,为localhost :port/myAppsName/设置主页

标签 java html rest spring-mvc tomcat

我目前正在开发一个项目,该项目分为前端(html5、jquery、css)和后端(带 java 配置的 Spring4 mvc - 无 web.xml-,暴露其余部分供 View 使用),每个有自己的 pom,依赖于同一个父 pom。

Project's tree structure

当我编译主项目时,会生成 2 个 war(前端和后端),稍后我将它们部署到 tomcat 7 中。

后端工作正常(我已经用 psotman 对其进行了测试),如果我从 tomcat 外部打开 html(当我从计算机上的文件夹中打开 index.html 时),前端也工作正常。但是,当我在 tomcat 中部署前端 war 和后端 war 并输入“localhost:8080/myAppsName/”时,会抛出 http 错误 404。我了解无法找到 html 以呈现索引页。

frontend tree structure

后端 AppConfig.java:

@Configuration
@EnableWebMvc
@ComponentScan(basePackages = "org.fedelaper.spring")
public class AppConfig {

    @Bean
    public ViewResolver internalResourceViewResolver() {
        InternalResourceViewResolver internalResourceViewResolver = new InternalResourceViewResolver();
        internalResourceViewResolver.setPrefix("/frontend/");
        internalResourceViewResolver.setSuffix(".html");
        return internalResourceViewResolver;
    }

}

后端AppInitializer.java:

    @SuppressWarnings("unchecked")
public class AppInitializer extends AbstractAnnotationConfigDispatcherServletInitializer {

    @SuppressWarnings("rawtypes")
    @Override
    protected Class[] getRootConfigClasses() {
        return new Class[] { AppConfig.class };
    }

    @SuppressWarnings("rawtypes")
    @Override
    protected Class[] getServletConfigClasses() {
        return null;
    }

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

}

我确实在项目前端有一个 web.xml,它实际上是空的:

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">

</web-app>

我的问题是: - 输入 localhost:8080/myAppsName 时,如何将 index.html 设置为我的应用程序的默认主页?

最佳答案

只需使用 welcome-file-list 更新您的前端模块 web.xml 文件即可。

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
<welcome-file-list>
    <welcome-file>index.html</welcome-file>
</welcome-file-list>
</web-app>

关于java - Spring 4 java配置,前后端分离,为localhost :port/myAppsName/设置主页,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42142141/

相关文章:

spring - 如何在 spring boot 中从同一个应用程序调用另一个 api

Java - 从两个不同数据类型的 ArrayLists 中获取公共(public)元素

java - RX : Run Zipped Observables in parallel?

javascript - 查找并删除我输入的正确名称 "class"的 <div>

html - 未应用网络字体! @font-face

PHP header() 重定向后 JavaScript 未加载

Django rest framework - 在 POST 后将新创建的对象的 id 传回响应

java - Spring Rest 数据 - 外键对象 URL 未在 @RequestBody 中转换

java - 用于在 Eclipse 中快速注释/取消注释代码的任何菜单或热键

java - 关于java native2ascii工具的困惑