2015-05-04 74 views
0

我一直在網上搜索,並在我的頭髮上關於如何解決這個問題近3周,但我找不到原因爲什麼我的靜態文件在我的本地Windows中使用tomcat 7在Spring MVC中提供,而不是在我們的Linux(Ubuntu)服務器上。下面我描述我的項目文件的結構和我的所有相關文件:靜態文件在Spring MVC中使用tomcat 7在Windows中,但不在Linux上

enter image description here

web.xml中:

<context-param> 
    <param-name>contextConfigLocation</param-name> 
    <param-value>/WEB-INF/config/root-context.xml</param-value> 
</context-param> 

<listener> 
    <listener-class>com.bakuparkingaz.util.BakuParkingServletContext</listener-class> 
</listener> 

<servlet> 
    <servlet-name>loginServlet</servlet-name> 
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> 
    <init-param> 
     <param-name>contextConfigLocation</param-name> 
     <param-value>/WEB-INF/config/servlet-context.xml</param-value> 
    </init-param> 
    <load-on-startup>1</load-on-startup> 
</servlet> 

<servlet-mapping> 
    <servlet-name>loginServlet</servlet-name> 
    <url-pattern>/*</url-pattern> 
</servlet-mapping> 
<welcome-file-list> 
    <welcome-file>index.html</welcome-file> 
</welcome-file-list>  

APP-context.xml中:

<tx:annotation-driven /> 
<context:component-scan base-package="com.bakuparkingaz"/> 
<context:property-placeholder location="classpath:db.properties" /> 
<mvc:resources mapping="/resources/**" location="/resources/"/> 

<bean class="org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor" /> 

AdminController.java:

@Controller 
public class AdminController { 
/. 
    @RequestMapping(value = "/admin") 
    public String admin() { 

     return "resources/admin/index.html"; 
    } 
} 

所以,現在當我打電話http://localhost:8080/admin我得到漂亮的風格的頁面,但服務器http://8.8.8.8/project/admin在一個醜陋的styleless html頁面(我已經部署了我的戰爭文件project.war到雄貓WebApp文件夾)的結果。爲什麼我的靜態文件不能在linux服務器上運行?

UPDATE管理員/ index.html的:

<!DOCTYPE html> 
<html lang="en"> 
<head> 

    <base href="/resources/admin/"> 

    <meta charset="utf-8"> 
    <title>BP Admin</title> 
    <meta name="description" content=""> 
    <meta name="author" content=""> 

    <meta name="viewport" content="width=device-width, initial-scale=1"> 

    <link href="//fonts.googleapis.com/css?family=Raleway:400,300,600" rel="stylesheet" type="text/css"> 

    <link rel="stylesheet" href="css/normalize.css"> 
    <link rel="stylesheet" href="css/skeleton.css"> 
    <link rel="stylesheet" href="css/semantic.min.css"> 
    <link rel="stylesheet" href="css/my.css"> 
    <link rel="stylesheet" href="//cdn.datatables.net/1.10.4/css/jquery.dataTables.min.css"> 

    <link rel="icon" type="image/png" href="images/favicon.png"> 

</head> 
<body> 

    <div class="ui left vertical labeled icon menu sidebar" id="left-menu"> 

    <a class="item"> 
     <i class="home icon"></i> 
     Home 
    </a> 
    </div> 

    <div class="footer"> 
    <div class="text center"><p>Copyright (c) 2015 - BakuParking</p> </div> 
    </div> 
</div> 

<script type="text/javascript" charset="utf8" src="//code.jquery.com/jquery-2.1.3.min.js"></script> 
<script src="//cdn.datatables.net/1.10.4/js/jquery.dataTables.min.js" charset="utf-8"></script> 
<script src="js/semantic.min.js"></script> 
<script src="js/my.js"></script> 

</body> 
</html> 
+0

上下文路徑不同,也許您可​​以嘗試在索引的中添加[https://developer.mozilla.org/en-US/docs/Web/HTML/Element/base)標記html的。 – sp00m

+0

謝謝,我正在做你現在提出的建議,但我認爲這只是爲了簡單,我認爲它不會解決問題。 – gdrt

回答

1

在本地主機上,你的應用是在根上下文。在8.8.8.8上,您的應用位於上下文/project上。請參閱http://docs.oracle.com/javaee/7/api/javax/servlet/ServletContext.html#getContextPath()

嘗試使用您的<base>標籤的絕對值,那麼這將取決於環境:

對於本地主機:

<base href="http://localhost:8080/" /> 

爲8.8.8.8:

<base href="http://8.8.8.8/project/" /> 

然後,請在兩種環境中使用以下相對路徑:

<link rel="stylesheet" href="/resources/admin/css/normalize.css" /> 
<script src="/resources/admin/js/semantic.min.js"></script> 

您會在this answer中看到一個方法來動態設置<base>標記。

+0

「上下文路徑不同」 - 我一開始並不理解這個陳述,但實際上這是正確的答案。這真是一個愚蠢的錯誤。謝謝,我解決了這個問題! – gdrt

相關問題