Daniel
Daniel

Reputation: 35

Can't link .jsp with .css in spring web mvc

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!

Upvotes: 0

Views: 1303

Answers (4)

Bui Thanh Tuan
Bui Thanh Tuan

Reputation: 19

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

Upvotes: 0

abdotalaat
abdotalaat

Reputation: 845

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

Upvotes: 3

Daniel
Daniel

Reputation: 35

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.

Upvotes: 0

dReAmEr
dReAmEr

Reputation: 7196

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 />

Upvotes: 1

Related Questions