0

I have been searching on the net and ripping my hair on how to solve this issue for almost 3 weeks, but I can't find the reason why my static files are served in Spring MVC with tomcat 7 in my local Windows, but not in our server on Linux(Ubuntu). Below I have described my projects file structure and all my relevant files:

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";
    }
}

So now when I call http://localhost:8080/admin I get the pretty styled page, but on the server http://8.8.8.8/project/admin (I have deployed my war file as project.war to tomcats webapp folder) results in an ugly styleless html page. Why my static files won't serve on the linux server?

UPDATE: 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>
gdrt
  • 3,160
  • 4
  • 37
  • 56
  • The context paths differ, maybe you could try to add a [](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/base) tag in the of your index.html. – sp00m May 04 '15 at 14:01
  • Thanks, I am doing what you have suggested right now, but I think it's only for simplicity, I don't think it will solve the issue. – gdrt May 04 '15 at 14:28

1 Answers1

1

On localhost, your app is on the root context. On 8.8.8.8, your app is on the context /project. See http://docs.oracle.com/javaee/7/api/javax/servlet/ServletContext.html#getContextPath().

Try to use an absolute value in your <base> tag, which will then depend on the environment:

For localhost:

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

For 8.8.8.8:

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

Then, use the following relative paths for both environments:

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

You'll see a method in this answer to set the <base> tag dynamically.

Community
  • 1
  • 1
sp00m
  • 47,968
  • 31
  • 142
  • 252
  • "The context paths differ" - I didn't understand the statement at first, but that was actually the correct answer. What a stupid mistake was that. Thank you, I have solved the issue! – gdrt May 04 '15 at 14:56