1

I am using Spring and while running a page without css it is running perfectly. when I include my css file it looks plain without css. While clicking on the link in the View source it shows Error 404 . help me on this

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 MVC Application</display-name>

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

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

</web-app>

Servlet.xml

<beans xmlns="http://www.springframework.org/schema/beans"
  xmlns:mvc="http://www.springframework.org/schema/mvc"
   xmlns:context="http://www.springframework.org/schema/context"
   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/mvc 
   http://www.springframework.org/schema/mvc/spring-mvc.xsd
   http://www.springframework.org/schema/context 
   http://www.springframework.org/schema/context/spring-context-3.0.xsd
   ">


   <context:component-scan base-package="com.ksv" />

   <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
      <property name="prefix" value="/WEB-INF/jsp/" />
      <property name="suffix" value=".jsp" />
   </bean>
    <mvc:resources mapping="/resources/**" location="/resources/css/"  
    cache-period="31556926"/>
    <mvc:annotation-driven />

</beans>

Controller:

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

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

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


      return "hello";
   }

}

JSP:

<!DOCTYPE html>
<html >
  <head>
    <meta charset="UTF-8">
    <title>KSV</title>  
     <link href="${pageContext.request.contextPath}/resources/css/style.css" rel="stylesheet" >
    </head>
  <body>

    <div class="wrapper">
    <div class="container">
        <h1>Login</h1>

                <form class="form" action="login" method="post">
            <input type="text" name = "username" placeholder="Username">
            <input type="password" name ="userpass" placeholder="Password">
            <button type="submit" id="login-button">Login</button>
        </form>
    </div>

    <ul class="bg-bubbles">
        <li></li>
        <li></li>
        <li></li>
        <li></li>
        <li></li>
        <li></li>
        <li></li>
        <li></li>
        <li></li>
        <li></li>
    </ul>
</div>
    <script src='http://cdnjs.cloudflare.com/ajax/libs/jquery/2.1.3/jquery.min.js'></script>






  </body>
</html>

File Structure:

HelloWeb
    -WebContent
        -Meta-inf
        -resources
        -web-inf
            -jsp
            -hello.jsp
        -Servlet.xml
        -web.xml

1 Answer 1

0

Just remove css/ from location attribute of mvc resource cnfiguration.

Change :

<mvc:resources mapping="/resources/**" location="/resources/css/"  
    cache-period="31556926"/>

to

<mvc:resources mapping="/resources/**" location="/resources/"  
    cache-period="31556926"/>
Sign up to request clarification or add additional context in comments.

2 Comments

Great ! It worked ! Thanks a lot :) Can you please explain why the above method is not worked?
Because, in your example, any requests from this url pattern /resources/**, Spring will look for the resources from the /resources/css/ instead. So <link href="${pageContext.request.contextPath}/resources/css/style.css", Spring will try to read it from location "${pageContext.request.contextPath}/resources/css/css/style.css". (extra css is added to the path). With your configuration, you should either remove /css from configuration or from 'href' attribute. IMHO for more readability it is better to remove from configuration.

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.