SSH SSH - 1 year ago 132
Java Question

How to use "css" in the "jsp" in Spring MVC project?

I am working on a spring MVC project in which I am trying to use

style sheets by referencing it to design my JSP page. But somehow my css files are not being picked up once I hit my method in the controller..

Below is my JSP file -

<link rel="stylesheet" type="text/css" href="test.css" />


<h1>All header 1 elements will be red</h1>
<h2>All header 2 elements will be blue</h2>
<p>All text in paragraphs will be green.</p>


And below is my
file -

h1 {color:red;}
h2 {color:blue;}
p {color:green;}

And below is my method in controller class -

@RequestMapping(value = "testing", method = RequestMethod.GET)
public Map<String, String> testing() {

final Map<String, String> model = new LinkedHashMap<String, String>();

return model;

Directory structure is like this -

|-- resources/
| +-- css/
| test.css
+-- views/

But somehow it's not working for me.. Is there anything wrong I am doing here?


Here is my web.xml file-

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="" xmlns="" xmlns:web="" xsi:schemaLocation="" id="WebApp_ID" version="3.0">

And below is my
file -

<?xml version="1.0" encoding="UTF-8"?>
<beans:beans xmlns=""

<!-- DispatcherServlet Context: defines this servlet's request-processing infrastructure -->

<!-- Allow proxys -->
<aop:aspectj-autoproxy />

<!-- Enables the Spring MVC @Controller programming model -->
<!-- Support AJAX processing with progressive rendering. Overrides HttpOutputMessage with RaptorResponseWriter -->
<beans:bean class=""/>

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

<!-- Handles HTTP GET requests by efficiently serving up static resources
in the corresponding directory -->
<resources mapping="/js/**" location="/js/" />
<resources mapping="/css/**" location="/css/" />

<!-- Resolves views selected for rendering by @Controllers to .jsp resources in the /WEB-INF/views directory -->
<beans:bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<beans:property name="prefix" value="/WEB-INF/views/" />
<beans:property name="suffix" value=".jsp" />

This is exception I am getting on my console but browser shows the data without any css -

404 /testweb/test.css Not Found /testweb/test.css Not Found }, Correlations : {} }

Answer Source

this css resource issue

please try below

 <link rel="stylesheet" type="text/css" href="css/test.css" />

and you can see this resource if be load by bowaser.