我目前正在开发一个项目,该项目分为前端(html5、jquery、css)和后端(带 java 配置的 Spring4 mvc - 无 web.xml-,暴露其余部分供 View 使用),每个有自己的 pom,依赖于同一个父 pom。
当我编译主项目时,会生成 2 个 war(前端和后端),稍后我将它们部署到 tomcat 7 中。
后端工作正常(我已经用 psotman 对其进行了测试),如果我从 tomcat 外部打开 html(当我从计算机上的文件夹中打开 index.html 时),前端也工作正常。但是,当我在 tomcat 中部署前端 war 和后端 war 并输入“localhost:8080/myAppsName/”时,会抛出 http 错误 404。我了解无法找到 html 以呈现索引页。
后端 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/