2016-04-29 131 views
1

我有一個非常簡單的web.xml的webapp。春季無法在webapp中提供靜態文件

<?xml version="1.0"?> 

<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"> 
    <context-param> 
     <param-name>contextConfigLocation</param-name> 
     <param-value> 
      /WEB-INF/applicationContext.xml 
     </param-value> 
    </context-param> 
</web-app> 

我設置的contextConfigLocation屬性,使類org.glassfish.jersey.server.spring.SpringWebApplicationInitializer我想手動執行此操作不會自動添加ContextLoadListener和RequestContextListener。

然後我有我自己的WebApplicationInitializer類以編程方式添加servlet而不是通過web.xml。

public class MyWebAppInitializer implements WebApplicationInitializer { 

    private static final Logger LOGGER = Logger.getLogger(SpringWebApplicationInitializer.class.getName()); 

    @Override 
    public void onStartup(ServletContext sc) throws ServletException { 
     //Setup spring listeners 
     { 
      LOGGER.config(LocalizationMessages.REGISTERING_CTX_LOADER_LISTENER()); 
      sc.addListener(ContextLoaderListener.class); 
      sc.addListener(RequestContextListener.class); 
     } 
     //Jersey Rest Servlet 
     { 
      final ServletRegistration.Dynamic registration = sc.addServlet(ServletContainer.class.getName(), ServletContainer.class); 
      registration.setInitParameter("javax.ws.rs.Application", com.tervita.portal.RestApplication.class.getName()); 
      registration.addMapping("/rest/*"); 
     } 
     //Normal Servlets 
     { 
      final ServletRegistration.Dynamic registration = sc.addServlet(LoginAction.class.getName(), LoginAction.class); 
      registration.addMapping("/LoginAction"); 
     } 
     //Apache Default Servlet 
     { 
      ServletRegistration.Dynamic dispatcher = sc.addServlet("dispatcher", DispatcherServlet.class); 
      dispatcher.setLoadOnStartup(1); 
      dispatcher.addMapping("/test"); 
     } 

    } 
} 

當我跑我的web應用程序,我無法訪問我已經加入諸如/css/app/ui-grid.css靜態文件。

war file structure

當我運行Tomcat我得到這個雖然...

enter image description here

所以我很困惑,我沒有在我的servlet配置覆蓋默認的servlet的任何條目這Tomcat應該是/ *。爲什麼默認的servlet不會被擊中並提供我的內容?

+0

在tomcat管理控制檯中,你能看到你的web應用程序被部署到的上下文(路徑)嗎?也許它被部署到根上下文('/')而不是'/ tervita360'? – ochi

+0

是的,它絕對是在/ tervita360。此外我的測試servlet是可達的。 – benstpierre

+0

你的WAR文件的內容怎麼樣?他們是否正確?你看過WAR文件來確認所有必要的文件在那裏嗎? - 現在只包括基礎知識 – ochi

回答