繁体   English   中英

在Windows中使用tomcat 7在Spring MVC中提供静态文件,但在Linux中不提供静态文件

[英]Static files are served in Spring MVC with tomcat 7 in Windows, but not on Linux

我已经在网上进行搜索,并且花了三周的时间寻找解决该问题的方法,但是我找不到在本地Windows中使用tomcat 7在Spring MVC中提供静态文件的原因,但在我们在Linux(Ubuntu)上的服务器。 下面,我描述了我的项目文件结构以及所有相关文件:

在此处输入图片说明

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 (我将war文件作为project.war部署到了tomcats webapp文件夹)导致一个丑陋的无样式html页面。 为什么我的静态文件不能在linux服务器上使用?

更新admin / 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>

在本地主机上,您的应用程序位于根上下文上。 在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>

您将在此答案中看到一种动态设置<base>标记的方法。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM