Spring Mvc Css Js Files
Posted : admin On 13.09.2019Include in JSP Page. To include CSS or JS in a JSP page, you can use JSTL tag c:url or Spring tag spring:url. 3.1 JSTL tag c:url example. In Spring 3 Web MVC applications every request is sent through an @Controller method. This is undesirable for CSS, JS and image files. Overcome the problem as so.
I would put things in a /resources directory rather than /, This is a little more specific on where you static resources are located and it is also a fairly standard practice at this point. See this blog for another possible project directory structure,: Do note if you choose this one that images and styles are not under WEB-INF that will cause issues. Changing what you have a now to this might make a little more sense and is fairly common: /src/main/resources/css /src/main/resources/js /src/main/resources/images In any event the annotation that does the magic is the one you have here: This configures a handler for static resources. In this case, requests for /resources/.
are mapped to files inside the / directory. Looking at you JSP and your directory structure I think you need to change it to this: ' rel='stylesheet' type='text/css' / ' I am not sure why you are putting.js and images under the css directory though, seems a bit confusing to me. If you go to the src/main/resources/css etc. Approach I mentioned above change your resource mapping to the following: mvc:resources mapping='/resources/.' location='/resources/' / Then access it like this: ' rel='stylesheet' type='text/css' /.
Spring Mvc Css
Hi Thank you for your suggesiton. It is still failed with 405: HTTP Status 405 - Request method 'GET' not supported I have changed resources path as you suggested: src/main/resources/img/ src/main/resources/tcal.css src/main/resources/tcal.js then changed link to: ' rel='stylesheet' type='text/css' / ' in my addDeliveries.jsp file. Here is the site access path: click the button 'Add Delivery'. Noticed that the popup page 'addDelivery.jsp' display as normal html without any css and js. I 'd expect the field 'Delivery Date' shown a js calendar for a user to pick a date, but currently there is nothing applied to this field.
Well you said you changed your resource path to src/main/resources/img/ src/main/resources/tcal.css src/main/resources/tcal.js as I suggested. What I actually suggested is: src/main/resources/css/tcal.css src/main/resources/js/tcal.js Did you also change your mvc:resources tag as I suggested to mvc:resources mapping='/resources/.' location='/resources/' / If you did then you are still not accessing them correctly. You said you used ' rel='stylesheet' type='text/css' / ' Using what you posted above (without the additional js folder) it should be ' You can find an example on the mvc showcase project One other thing in your web.xml do you have the dispatcher mapped to /?
See More On Stackoverflow
So the question is, how to include external CSS file or Bootstrap CSS file properly into JSP?? It's easy as adding few lines in your SpringMVC 3 XML file! Before that, let's see what the issue is. When you keep your CSS/ images/ JS files inside your web folder and try to load it in JSP, it goes to Spring's Dispatcher Servlet.
There is no mapping for those files and Spring won't load them. Add the following line to the dispatcher-servlet.xml and put all the static resources like CSS, images & JavaScript files inside a folder inside your web folder as mentioned below. And the JSP file needs to access the external CSS file as follows. In this example, I've used Bootstrap, but same applicable for your own CSS as well. Now a final tip before finishing- did you notice the first line of the spring config file?
Make sure you have the line in place when you are using annotation based controller definition in your source. Try without that line and you'll have a frustrating situation where the resources will work fine and the JSPs won't be loading properly! The reason is this. When you add the line, SpringMVC adds its own handler mapping and the default DispatcherServlet's mappings are broken. Therefore other handler mappings should be declared explicitly, either by or manually as beans.
So adding does the trick for you.