0

How can I import css file into Java webapp? I deployed app on Tomcat and when I load page the css file wont load and in my firebug i got error : Failed to load resource: the server responded with a status of 404 (Not Found)

I have this folder structure :

WebProject
--src.hr.juka.webapp.controller
----MainController.java
--WebContent
----css
------main.css
----WEB-INF
------pages
--------main.jsp
------applicationContext.xml
------web.xml

applicationContext.xml :

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:aop="http://www.springframework.org/schema/aop"
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
                        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd
                        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd
                        http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
                        http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd">

    <context:annotation-config />
    <context:component-scan base-package="hr.juka" />

    <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/pages/" />
        <property name="suffix" value=".jsp" />
    </bean>

    <bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource">
        <property name="basename" value="messages" />
    </bean>

</beans>

web.xml :

<web-app id="WebApp_ID" version="2.4" 
    xmlns="http://java.sun.com/xml/ns/j2ee" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee 
    http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">

  <display-name>Juka webapp</display-name>


  <!-- The definition of the Root Spring Container shared by all Servlets and Filters -->
  <context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>/WEB-INF/applicationContext.xml</param-value>
  </context-param>

  <!-- Creates the Spring Container shared by all Servlets and Filters -->
  <listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>

  <!-- Processes application requests -->

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

  <servlet-mapping>
    <servlet-name>main</servlet-name>
      <url-pattern>/</url-pattern>
  </servlet-mapping>


</web-app>

main.jsp :

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %>
<html>
    <head>
        <link rel="stylesheet" href="${pageContext.request.contextPath}/WebContent/css/main.css" type="text/css">
    </head>
<body>
    <h1 class="test">TEST ${message}</h1>
</body>
</html>

MainController :

package hr.juka.webapp.controller;

import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

@Controller
@RequestMapping("/")
public class MainController {


    @RequestMapping(method = RequestMethod.GET)
    public String index(ModelMap model) {

        model.addAttribute("message", "Main Controller");
        return "main";

    }
}

I tried putting css file into different folders, tried absolute and relative paths but none seems to work.

kryger
  • 12,906
  • 8
  • 44
  • 65
Juka
  • 37
  • 1
  • 9
  • Have you tried `href="${pageContext.request.contextPath}/css/main.css"` – yate Mar 03 '15 at 18:27
  • 1
    In your web.xml, you've specified that all the requests to your application have to go through the dispatcher servlet. So the spring servlet is trying to find a controller that is mapped to handle the requests for css files. You need to update your servlet mapping to handle resource files separately. Change your url-pattern in web,xml to something like `/*.do` and update the request mapping at the controller to have the exact path - `/index.do` for example – RKodakandla Mar 03 '15 at 18:33
  • yes. it wont work. i tried all combinations :( – Juka Mar 03 '15 at 18:34
  • i'll try what rrkwells said . Thanks – Juka Mar 03 '15 at 18:37
  • 1
    If you are not planning to change the URL mapping in web.xml, you can add an entry in the spring configuration file to handle static resources like .js, .css and image files. Look at the discussion here. http://stackoverflow.com/questions/1483063/spring-mvc-3-and-handling-static-content-am-i-missing-something – RKodakandla Mar 03 '15 at 18:38
  • Thank you! I added this and it helped : default /css/* – Juka Mar 03 '15 at 18:50

0 Answers0