2017-02-09 10 views
0

我目前正在開發一個項目,它分爲前端(html5,jquery,css)和後端(Spring4 mvc java config -no web.xml-,暴露休息的視圖使用它),每個都有自己的POM,這取決於相同的父POM。Spring 4 java配置,爲前臺/後臺分開的戰爭,爲localhost設置主頁:port/myAppsName/

Project's tree structure

當我編譯主體工程,2戰產生(前端和後端),這是我後來部署到Tomcat 7

後端工作正常(我已經用它測試psotman)和前端工作正常,如果我打開從外部的Tomcat的HTML(當我打開我的電腦上的文件夾index.html)。但是,當我在tomcat中與te後端戰爭一起部署前端戰爭並輸入「localhost:8080/myAppsName /」時,會拋出http eror 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設置爲我應用程序的默認主頁?

回答

0

只需使用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> 
+0

謝謝! 我也必須將index.html從「/frontend/index.html」移動到「/index.html」 – fedelaper