java - Unable to load static resources in spring mvc application -
i new spring mvc. creating project has input form taking details , displaying them on page. have controller class has 2 request mapping methods. 1 goes input page , display page. application working fine unable load static resources css. css located under webcontent in folder named css. below project structure in eclipse
http://i.stack.imgur.com/74bm2.png
i deployed application in jboss. context root set productcat
below 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 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 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 save product // store model in variable view model.addattribute("product",product); return "/productdetails"; } }
here 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 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 can see used in servlet xml still not loading css. error in browser
failed load resource: server responded status of 404 (not found)
the url trying access http://localhost:8080/productcat/css/main.css
you using spring's resource mapping
<mvc:resources location="/css/**" mapping="/css/"/>
where mapped /css/
/css/**
. think it's wrong & should this
<mvc:resources mapping="/css/**" location="/css/" />
for more information check out spring mvc – how include js or css files in jsp page
Comments
Post a Comment