Spring MVC Where to Put CSS/Js/Img Files

How to include js and CSS in JSP with spring MVC

First you need to declare your resources in dispatcher-servlet file like this :

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

Any request with url mapping /resources/** will directly look for /resources/folder/.

Now in jsp file you need to include your css file like this :

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

Similarly you can include js files.

Hope this solves your problem.

Where to put static files such as CSS in a spring-boot project?

Anywhere beneath src/main/resources/static is an appropriate place for static content such as CSS, JavaScript, and images. The static directory is served from /. For example, src/main/resources/static/signin.css will be served from /signin.css whereas src/main/resources/static/css/signin.css will be served from /css/signin.css.

The src/main/resources/templates folder is intended for view templates that will be turned into HTML by a templating engine such as Thymeleaf, Freemarker, or Velocity, etc. You shouldn't place static content in this directory.

Also make sure you haven't used @EnableWebMvc in your application as that will disable Spring Boot's auto-configuration of Spring MVC.

Placement css, img and js of files after deploy spring mvc application

Yes, you can do this.

registry.addResourceHandler("/css/**").addResourceLocations("/resources/css/").setCachePeriod(31556926);

And you have this structure

webapp
- WEB-INF
- resources
- css
- somefile.css
- js
- img

You would access your resources at

localhost:8080/context/css/somefile.css

will get the resource from /resources/css/somefile.css. Do the same for all the other resources.

Add css file to resources but can not read it in Spring MVC 3.1

Finally, I changed spring mvc from 3.x to version 4, it worked.

Spring MVC import css and js in jsp page

I had faced similar issue, I tried to implement the spring part from the below page and it worked,

http://www.mkyong.com/spring-mvc/spring-mvc-how-to-include-js-or-css-files-in-a-jsp-page/

The mvc:resources mapping is the key concept.

Regards

can't find css, images and js static files in spring

If you have java project and your resources/img/** or resources/css/** folder comes under WebContent you can access the your resources like this

<link rel="stylesheet" href="${pageContext.request.contextPath}/resources/css/styleV.css"    
type="text/css" />

and similar way for images and js file.

Or second approach is you can add following mapping in web.xml for your servlet.

  <servlet-mapping>
<servlet-name>default</servlet-name>
<url-pattern>*.css</url-pattern>
</servlet-mapping>

<servlet-mapping>
<servlet-name>default</servlet-name>
<url-pattern>*.js</url-pattern>
</servlet-mapping>

<servlet-mapping>
<servlet-name>default</servlet-name>
<url-pattern>*.png</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>default</servlet-name>
<url-pattern>*.jpg</url-pattern>
</servlet-mapping>

and access the css as

<link rel="stylesheet" type="text/css" href="resources/css/jquery-ui-1.8.22.custom.css">


Related Topics



Leave a reply



Submit