0

I have read most of the related posts I have found on this problem but I could not find any working solution for my situation. Basicly my jsp file can't seem to find the css file.

Here are my files: The jsp page:

 <%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %>
    <!DOCTYPE HTML>
    <html xmlns:th="http://www.thymeleaf.org">
    <head>
        <title>Search for flight</title>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
        <link href="${pageContext.request.contextPath}/WEB-INF/pages/theme.css" rel="stylesheet" type="text/css" >
    </head>

the css page:

#title
{
    font-family: "Times New Roman";
    color: aqua;
}

mvc-dispatcher-servlet.xml:

<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="
        http://www.springframework.org/schema/beans     
        http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
        http://www.springframework.org/schema/context 
        http://www.springframework.org/schema/context/spring-context-3.0.xsd
        http://www.springframework.org/schema/mvc
        http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">

    <context:component-scan base-package="com.mkyong.common.controller" />

    <mvc:annotation-driven />

    <bean
        class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix">
            <value>/WEB-INF/pages/</value>
        </property>
        <property name="suffix">
            <value>.jsp</value>
        </property>
    </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>Spring Web MVC Application</display-name>

    <servlet>
        <servlet-name>mvc-dispatcher</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>

    <servlet-mapping>
        <servlet-name>mvc-dispatcher</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>

    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/mvc-dispatcher-servlet.xml</param-value>
    </context-param>

    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>

</web-app>

And the controller works fine so I wouldn't find any real use in posting it.

The jsp file is visible from the browser but the styling is totally absent.

Thank you in advance!

1
  • Also, the css file is in the same directory as the jsp. Commented Jan 3, 2015 at 14:20

4 Answers 4

3

you need to declare your resources in dispatcher-servlet file

<mvc:resources mapping="/resources/**" location="/resources/css/" />

Any request with url mapping /resources/** will directly look for /resources/css/

Now in jsp file you need to include your css file like this :

<link href="<c:url value="/resources/css/your file.css" />" rel="stylesheet">

complete example https://github.com/abdotalaat/SpringMVCCSS

Sign up to request clarification or add additional context in comments.

5 Comments

I seem to get an error because of some missing />. Are some headers needed? Or imports?
replace this using <link href='<c:url value="/resources/css/your file.css" />' rel="stylesheet"> and add this tag <%@ taglib prefix="c" uri="java.sun.com/jsp/jstl/core" %> in jsp page
The uri cannot be resolved. Should I include something in web.xml or some dependency?
i create full project for you can clone it from github.com/abdotalaat/SpringMVCCSS you need only put spring libirary
Thank you very much! It's very kind of you to do this.
1

I would suggest to add below lines in your spring configuration file(mvc-dispatcher-servlet.xml).

<mvc:resources mapping="/pages/*.css" location="/pages/"/>
<mvc:default-servlet-handler />

3 Comments

It's not working. Also, InteliJ signals me the directory "/pages/" can't be resolved, so I put "pages/".
Then use below ... <mvc:resources mapping="/pages/*.css" location="/pages"/>
I can't get it working and it starts to annoy me really hard. 3 hours have passed already.
0

So I have resolved my problem.

<dependency>
    <groupId>javax.servlet</groupId>
    <artifactId>jstl</artifactId>
    <version>1.2</version>
</dependency>

After adding this dependency the rest was pretty much flawless. Thank you for your explanations! I have a much better understanding of how stuff works now.

Comments

0

I have the same problem: - Check you add JSTL in pom.xml and declare in the jsp file - Check with resources folders inside the webapp folder - Check the href for your link css correct

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.