Postingan lainnya
Memanggil css di maven spring
mohon bantuanya gmn caranya panggil css di maven spring konfigurasi webapp pake java gak pake xml tolong bantuanya
1 Jawaban:
Halo @sawalbaelah, Izin Menjawab,
Ada beberapa cara Serve Static Resources with Spring:
I. Menggunakan The ResourceHttpRequestHandler (Spring 3.1+)
a. Serving a Resource Stored in the WAR
<pre> @Configuration @EnableWebMvc public class MvcConfig implements WebMvcConfigurer { @Override public void addResourceHandlers(ResourceHandlerRegistry registry) { registry .addResourceHandler("/resources/**") .addResourceLocations("/resources/"); } } </pre>
in html:
<pre> <link href="<c:url value="/resources/myCss.css" />" rel="stylesheet"> </pre>
b. Serving a Resource Stored in the File System
<pre> @Override public void addResourceHandlers(ResourceHandlerRegistry registry) { registry .addResourceHandler("/files/**") .addResourceLocations("file:/opt/files/"); } </pre>
in html:
<pre> @Override public void addResourceHandlers(ResourceHandlerRegistry registry) { registry .addResourceHandler("/files/**") .addResourceLocations("file:/opt/files/"); } </pre>
c. Configuring Multiple Locations for a Resource
<pre> @Override public void addResourceHandlers(ResourceHandlerRegistry registry) { registry .addResourceHandler("/resources/**") .addResourceLocations("/resources/","classpath:/other-resources/"); } </pre>
II. Menggunakan The New ResourceResolvers(Spring 4.1+)
a. The PathResourceResolver
<pre> @Override public void addResourceHandlers(ResourceHandlerRegistry registry) { registry .addResourceHandler("/resources/**") .addResourceLocations("/resources/","/other-resources/") .setCachePeriod(3600) .resourceChain(true) .addResolver(new PathResourceResolver()); } </pre>
in html:
<pre> <script type="text/javascript" src="<c:url value="/resources/foo.js" />"> </pre>
b. The EncodedResourceResolver
<pre> registry .addResourceHandler("/other-files/**") .addResourceLocations("file:/Users/Me/") .setCachePeriod(3600) .resourceChain(true) .addResolver(new EncodedResourceResolver()); </pre>
c. Chaining ResourceResolvers
<pre> @Override public void addResourceHandlers(ResourceHandlerRegistry registry) { registry .addResourceHandler("/js/**") .addResourceLocations("/js/") .setCachePeriod(3600) .resourceChain(true) .addResolver(new GzipResourceResolver()) .addResolver(new PathResourceResolver()); } </pre>
in html:
<pre> <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %> <html> <head> <link href="<c:url value="/resources/bootstrap.css" />" rel="stylesheet" /> <script type="text/javascript" src="<c:url value="/js/foo.js" />"></script> <title>Home</title> </head> <body> <h1>This is Home!</h1> <img alt="bunny hop image" src="<c:url value="files/myImage.png" />" /> <input type = "button" value="Click to Test Js File" onclick = "testing();" /> </body> </html> </pre>
III. Additional Security Configuration
<pre> <intercept-url pattern="/files/" access="permitAll" /> <intercept-url pattern="/other-files//" access="permitAll" /> <intercept-url pattern="/resources/" access="permitAll" /> <intercept-url pattern="/js/" access="permitAll" /> </pre>
Sumber: <a href='https://www.baeldung.com/spring-mvc-static-resources'>https://www.baeldung.com/spring-mvc-static-resources</a>