Spring Mvc Css Js Files

Posted on  by 

Active8 years, 1 month ago

I used Netbeans to create a Spring MVC 3.0 app. I have a simple controller and JSP view. The JSP view appears correctly except for an image that doesn't render. Set Annotation based Configuration for Spring 4 MVC tutorial. For this Spring 4 MVC tutorial we are going to use Spring’s Java based configuration or annotation based configuration instead of old XML configuration. So now lets add the Java Configuration required to bootstrap Spring 4 MVC example in our webapp. Create AppConfig.java file under /src folder. Give appropriate package name to your file. Mvc:resources configures a handler for serving static resources such as images, js, and, css files with cache headers optimized for efficient loading in a web browser. Allows resources to be served out of any path that is reachable via Spring’s Resource handling. This tutorial explains the implementation of Datatables with Spring MVC. Most of you all will be aware of usage of data tables in a web application, here we will see how to integrate it with Spring MVC application. We need to include all the required CSS and JS files which are required for datatables. Same is bundled in the attached source. Spring MVC – How To Include JS Or CSS Files In A JSP Page. 时间 2013-12-09. In this tutorial, we will show you how to include JavaScript and CSS files in a JSP page, with the Spring MVC framework environment. Project Directory. Review the final project directory structure.

Adding a CSS file to the JSP for my Spring app. The file is in the WEB-INF/resources/css directory. The file exists and is named correctly but the CSS isn't being applied when it is in the external file. You can look at this blog post by Gordon Dickens for an alternative directory structure.

I am having trouble linking to a style sheet from a jsp page. I believe it has something to do with my directory structure which is:

I have tried various forms of the standard html link tag such as:

I have also tried including the css file in the jsp folder and linking to it directly. Nothing works. When I view the source after deployment, and try to access the CSS file directly, it's not there, but this is no surprise to me since it is in the WEB-INF directory.

I have also verified that it is getting deployed with the rest of the application. The jsp source is:

Thanks!

CaseyCasey
7,20815 gold badges60 silver badges103 bronze badges

3 Answers

Files in /WEB-INF are not directly public accessible. Only an intermediating (controller) Servlet can access and stream them for you with help of ServletContext#getResourceAsStream(). That's exactly what Spring (as any other decent MVC framework) does with JSP files. You can't access JSP files directly by URL. That would potentially leak source code or break the application behaviour.

So you have basically 2 options here:

  1. Put CSS files in public webcontent (just move one folder above WEB-INF, so that /css is at same level as /WEB-INF).

  2. Create a Servlet which listens on an url-pattern of /css/*, gets the requested CSS file by HttpServletRequest#getPathInfo() and basically gets an InputStream from it using the aforementioned ServletContext#getResourceAsStream() and writes it to the OutputStream of the response along a correct set of response headers with at least Content-Type and Content-Length.

After all I think option 1 is easier and more suitable for your requirement ;)

BalusCBalusC
884k312 gold badges3255 silver badges3288 bronze badges

Spring-style implementation of 2nd approach from @BalusC is to using mvc:resources, like that:

and after this your main.css file should be available at /css/main.css

(NOTE: if it does not work, check that DispatcherServlet is mapped to /)

Slava SemushinSpringSlava Semushin
11.8k6 gold badges40 silver badges62 bronze badges

or try this

<%@ taglib prefix='spring' uri='http://www.springframework.org/tags' %>

Bosch esi tronic crack 2012 ford. The spare parts catalog and repair manuals Bosch ESI tronic supports all languages including Russian, has the good interface, allows to search under number of detail Bosch or any other manufacturer, gives out the information on application concrete spare parts on marks and models.

' type='text/css'/>

it may work.

josephjoseph

Not the answer you're looking for? Browse other questions tagged javaspringjspspring-mvc or ask your own question.

Active5 years, 3 months ago

I have created MVC application.

I want to include js or css file into jsp.

My static files ar under:

My code to include jquery is:

and i cant load the js file into view.

I see the logs with info:

what means, that MVC try to map url to js file.

I think that there is something with my configuration, but i don't know what.

my web.xml is: Taeyang solar album download mp3.

http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd'>

IlkarIlkar
8209 gold badges30 silver badges62 bronze badges

7 Answers

Change your DispatcherServlet mapping to e.g:

Or some other not conflicting url-pattern like *.htm or /controllers/*. Remember that from now on all your controllers will be available only through this pattern.

Now it is intercepting everything in your web application, including .js files, images, etc.

The same thing with hibernateFilter - you don't really need an open Hibernate session when fetching .js files, don't you?

Mvc Css Themes

Tomasz NurkiewiczTomasz Nurkiewicz
280k56 gold badges618 silver badges628 bronze badges

add this to you confing and modify the location according to your need.

see this How to handle static content in Spring MVC?

Community
Josef ProcházkaJosef Procházka
9952 gold badges7 silver badges26 bronze badges
Mentor RekaMentor Reka
6,2711 gold badge8 silver badges8 bronze badges

Use spring JSTL tags to include external script files or style sheets.First you should include the the taglib in JSP as follows.

Js css before

Then you can include extenal script file using,

SurvivorSurvivor

I agree with your answer. But in the style.css file declare url that relate to path of image.

--style.css--

How to use tag <spring:url></spring:url> in style.css file to see in browser IE/Firefox

--jsp file ---

Mvc Css Templates

bluechipbluechip

add mvc:resources in your config file(*-servlet.xml), you can find it works

Mvc
JesseJesse

I just followed Mkyong Tutorial to place css, js, jquery & image files. Its working for me.

In servlet-context.xml

Spring Mvc Css Js Files Free

In JSP , import tag library

Spring Mvc Css Js Files Download

and add like

Master MindMaster Mind

Mvc Css File

1,3842 gold badges22 silver badges43 bronze badges

Not the answer you're looking for? Browse other questions tagged springmodel-view-controller or ask your own question.

Coments are closed