Link / enable css in FreeMarker using Spring 3 MVC

I am currently trying to include a css file in my FreeMarker * .ftl. I also configured the resource folder in the xml file of the servlet file.

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

But how can I access my css file from my FreeMarker template?

I just tried the following, but to no avail.

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

The resource folder is in the root directory of my spring MVC 3.0 application.

 /web /resources /img /css /WEB-INF /templates 

My servlet root is defined as:

 <servlet> <servlet-name>springmvc</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>springmvc</servlet-name> <url-pattern>/web/*</url-pattern> </servlet-mapping> 

My FreeMarker files are in the templates folder.

+6
source share
2 answers

I found two solutions. One with spring macros and one without it in my FreeMarker file.

The easiest way is to use it without macros:

 <link rel="stylesheet" type="text/css" href="/springmvc/resources/css/style.css" /> 

In this solution, I have to determine the full path.

Using spring macros, you must put your spring.ftl in the templates directory and include it in every FreeMarker template where you want to use it.

 <#import "spring.ftl" as spring /> <html> <head> <title>...</title> <link rel="stylesheet" type="text/css" href="<@spring.url '/resources/css/style.css'/>"/> ... 

Spring macros can also be used for other things. This blog gives a good overview.

+7
source

You can try this,

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

At runtime, this code will return the exact path.

+1
source

Source: https://habr.com/ru/post/894306/


All Articles