Memanggil css di maven spring

mohon bantuanya gmn caranya panggil css di maven spring konfigurasi webapp pake java gak pake xml tolong bantuanya

avatar sawalbaelah
@sawalbaelah

1 Kontribusi 0 Poin

Diperbarui 5 tahun yang lalu

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> &lt;link href="&lt;c:url value="/resources/myCss.css" /&gt;" rel="stylesheet"&gt; </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> &lt;script type="text/javascript" src="&lt;c:url value="/resources/foo.js" /&gt;"&gt; </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> &lt;%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %&gt; &lt;html&gt; &lt;head&gt; &lt;link href="&lt;c:url value="/resources/bootstrap.css" /&gt;" rel="stylesheet" /&gt; &lt;script type="text/javascript" src="&lt;c:url value="/js/foo.js" /&gt;"&gt;&lt;/script&gt; &lt;title&gt;Home&lt;/title&gt; &lt;/head&gt; &lt;body&gt; &lt;h1&gt;This is Home!&lt;/h1&gt; &lt;img alt="bunny hop image" src="&lt;c:url value="files/myImage.png" /&gt;" /&gt; &lt;input type = "button" value="Click to Test Js File" onclick = "testing();" /&gt; &lt;/body&gt; &lt;/html&gt; </pre>

III. Additional Security Configuration

<pre> &lt;intercept-url pattern="/files/" access="permitAll" /&gt; &lt;intercept-url pattern="/other-files//" access="permitAll" /&gt; &lt;intercept-url pattern="/resources/" access="permitAll" /&gt; &lt;intercept-url pattern="/js/" access="permitAll" /&gt; </pre>

Sumber: <a href='https://www.baeldung.com/spring-mvc-static-resources'>https://www.baeldung.com/spring-mvc-static-resources</a>

avatar keccikun
@keccikun

368 Kontribusi 197 Poin

Dipost 5 tahun yang lalu

Login untuk ikut Jawaban