mardi 3 mars 2015

How to import css file to my Java spring webapp?

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://ift.tt/GArMu6"
xmlns:xsi="http://ift.tt/ra1lAU"
xmlns:aop="http://ift.tt/OpNdV1"
xmlns:mvc="http://ift.tt/1bHqwjR"
xmlns:tx="http://ift.tt/OGfeU2"
xmlns:context="http://ift.tt/GArMu7"
xsi:schemaLocation="http://ift.tt/GArMu6 http://ift.tt/GAf8ZW
http://ift.tt/OGfeU2 http://ift.tt/OGffan
http://ift.tt/GArMu7 http://ift.tt/1bb5cbf
http://ift.tt/OpNdV1 http://ift.tt/OGfcLK
http://ift.tt/1bHqwjR http://ift.tt/JWpJWM">

<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://ift.tt/qzwahU"
xmlns:xsi="http://ift.tt/ra1lAU"
xsi:schemaLocation="http://ift.tt/qzwahU
http://ift.tt/16hRdKA">

<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://ift.tt/QfKAz6"%>
<%@taglib prefix="fn" uri="http://ift.tt/1lpNXqs" %>
<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.


Aucun commentaire:

Enregistrer un commentaire