Unable to load static resources in spring mvc application

I am new to Spring MVC. I am creating a project that has an input form that takes some details and then displays them on another page. I have a controller class that has two methods for matching requests. One goes to the input page and the other goes to the display page. The app is working fine, but I can't load static resources like css. My css is under WebContent in a folder named css. Below is my project structure in eclipse

http://i.stack.imgur.com/74bM2.png

I have deployed my application to jboss. My context root is set as productCat

Below is my web.xml

    <?xml version="1.0" encoding="UTF-8"?>
    <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
    <servlet>
      <servlet-name>springmvc</servlet-name>
      <servlet-class>
        org.springframework.web.servlet.DispatcherServlet
      </servlet-class>
      <init-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/config/springmvc-config.xml</param-  value>
      </init-param>
      <load-on-startup>1</load-on-startup>    
      </servlet>

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

      

And my springmvc-config.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:p="http://www.springframework.org/schema/p" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="
    http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans.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.xsd">
<context:component-scan base-package="com.kaushik.controller" />
<mvc:annotation-driven />
<mvc:resources location="/css/**" mapping="/css/"/>
<bean id="viewResolver"
    class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    <property name="prefix" value="/WEB-INF/jsp/"></property>
    <property name="suffix" value=".jsp"></property>
</bean>

      

Below is my controller class

    package com.kaushik.controller;

    import org.apache.commons.logging.Log;
    import org.apache.commons.logging.LogFactory;
    import org.springframework.stereotype.Controller;
    import org.springframework.ui.Model;
    import org.springframework.web.bind.annotation.RequestMapping;

    import com.kaushik.domain.Product;
    import com.kaushik.form.ProductForm;

    @Controller
    public class ProductController
    {
       private static final Log logger = LogFactory.getLog(ProductController.class);

    @RequestMapping(value="/product_input.action")
    public String inputProduct()
    {
      logger.info("inputProduct called");
      return "ProductForm";
    }

    @RequestMapping(value="/product_save.action")
    public String saveProduct(ProductForm productForm, Model model)
    {
      logger.info("SaveProductController called");
      // create model
      Product product = new Product();
      product.setName(productForm.getName());
      product.setDescription(productForm.getDescription());
      try
      {
        product.setPrice(Float.parseFloat(productForm.getPrice()));
      }
      catch (NumberFormatException num)
      {

      }
      // TODO code to save product
      // store model in a variable for the view
      model.addAttribute("product",product);
      return "/ProductDetails";
     }

     }

      

Here is my ProductForm.jsp

    <!DOCTYPE HTML>
    <html>
    <head>
    <title>Add Product Form</title>
    <link href="css/main.css" rel="stylesheet" >

    </head>
    <body>

          <div id="global">
              <form action="product_save.action" method="post">
                <fieldset>
                   <legend>Add a product</legend>
                   <p>
                   <label for="name">Product Name: </label>
                   <input type="text" id="name" name="name" 
                tabindex="1">
                   </p>
                   <p>
                   <label for="description">Description: </label>
                   <input type="text" id="description" 
                name="description" tabindex="2">
                   </p>
                    <p>
                    <label for="price">Price: </label>
                    <input type="text" id="price" name="price" 
                tabindex="3">
                   </p>
                   <p id="buttons">
                   <input id="reset" type="reset" tabindex="4">
                   <input id="submit" type="submit" tabindex="5" 
                value="Add Product">
                   </p>
                </fieldset>
              </form>
          </div>
    <body>
    </html>

      

As you can see I used xml in my servlets but it still doesn't load css. Browser error

Failed to load resource: server responded with status 404 (not found)

The url it is trying to get is http: // localhost: 8080 / productCat / css / main.css

+3


source to share


2 answers


You are using Spring resource mapping

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

      

where you mapped /css/

to /css/**

. I think it is wrong and it should be something like this



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

      

For more information visit Spring MVC - How To Include JS or CSS Files in a JSP Page

+2


source


Your structure and display looks great , fixing this 007 is correct. Your additional problem is that you are accessing your css via a relative link, add the page directive to the top of the jsp

<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>

      



and link your css

as

 <link href="<c:url value="/css/main.css"/>" rel="stylesheet" >

      

+1


source







All Articles