Skip to content

Commit 99c1f96

Browse files
committed
ExamplesApplication: docs, disable @EnableWebMvc use spring boot default conventions, redirect in index.html; /click/* is served by spring/container
1 parent 827bcf2 commit 99c1f96

File tree

1 file changed

+53
-27
lines changed

1 file changed

+53
-27
lines changed

examples/src/main/java/org/apache/click/examples/ExamplesApplication.java

Lines changed: 53 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -14,50 +14,76 @@
1414
import org.springframework.context.Lifecycle;
1515
import org.springframework.context.annotation.Bean;
1616
import org.springframework.context.annotation.ComponentScan;
17-
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
18-
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
19-
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
20-
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
2117

2218
import java.util.Collections;
2319
import java.util.HashMap;
2420
import java.util.Map;
2521

22+
/**
23+
spring-boot-starter-web
24+
25+
⛔ If you want to take complete control of Spring MVC, you can add your own `@Configuration` annotated with `@EnableWebMvc`.
26+
If you want to keep Spring Boot MVC features, and you just want to add additional MVC configuration (interceptors, formatters, view controllers etc.)
27+
you can add your own `@Bean` of type WebMvcConfigurerAdapter, but without `@EnableWebMvc`!
28+
29+
https://stackoverflow.com/questions/42393211/how-can-i-serve-static-html-from-spring-boot
30+
31+
spring-boot-autoconfigure-2.7.18.jar!\META-INF\spring-configuration-metadata.json
32+
<pre><code>
33+
"name": "spring.web.resources.static-locations",
34+
"type": "java.lang.String[]",
35+
"description": "Locations of static resources. Defaults to classpath:[\/META-INF\/resources\/, \/resources\/, \/static\/, \/public\/].",
36+
"sourceType": "org.springframework.boot.autoconfigure.web.WebProperties$Resources",
37+
"defaultValue": [
38+
"classpath:\/META-INF\/resources\/",
39+
"classpath:\/resources\/",
40+
"classpath:\/static\/",
41+
"classpath:\/public\/"
42+
]
43+
44+
"name": "spring.web.resources.cache.period",
45+
"type": "java.time.Duration",
46+
"description": "Cache period for the resources served by the resource handler. If a duration suffix is not specified, seconds will be used. Can be overridden by the 'spring.web.resources.cache.cachecontrol' properties.",
47+
"sourceType": "org.springframework.boot.autoconfigure.web.WebProperties$Resources$Cache"
48+
49+
</code></pre>
50+
*/
2651
@SpringBootApplication // (scanBasePackageClasses = { LunaApplication.class, RemoteMvelConsoleController.class })
27-
@EnableWebMvc
52+
//@EnableWebMvc → WebMvcConfigurer
2853
@ComponentScan(// == <context:component-scan base-package="org.apache.click.examples" scope-resolver="org.apache.click.extras.spring.PageScopeResolver"/>
2954
basePackages = "org.apache.click.examples",
30-
scopeResolver = PageScopeResolver.class
55+
scopeResolver = PageScopeResolver.class,
56+
basePackageClasses = ExamplesApplication.class
3157
)
3258
@Slf4j
33-
public class ExamplesApplication implements WebMvcConfigurer, Lifecycle {
59+
public class ExamplesApplication implements Lifecycle {
3460
public static void main (String[] args) {
3561
System.out.println(" *** main thread started *** "+Thread.currentThread());
3662
SpringApplication.run(ExamplesApplication.class, args);
3763
System.err.println(" *** main thread finishes *** "+Thread.currentThread());
3864
}
39-
private static final String[] CLASSPATH_RESOURCE_LOCATIONS = {
40-
"classpath:/META-INF/resources/",// todo classpath*:
41-
"classpath:/resources/",
42-
"classpath:/static/",
43-
"classpath:/public/"
44-
};
45-
46-
@Override
47-
public void addResourceHandlers(ResourceHandlerRegistry registry) {
48-
registry.addResourceHandler("/**").addResourceLocations(CLASSPATH_RESOURCE_LOCATIONS);
49-
registry.addResourceHandler("/static/**").addResourceLocations("classpath*:/static/").setCachePeriod(3600);// .resourceChain(true)
50-
registry.addResourceHandler("/resources/**").addResourceLocations("classpath:/resources/", "classpath:/static/", "classpath:/public/").setCachePeriod(3600).resourceChain(true);
51-
}
65+
// private static final String[] CLASSPATH_RESOURCE_LOCATIONS = {
66+
// "classpath:/META-INF/resources/",// todo classpath*:
67+
// "classpath:/resources/",
68+
// "classpath:/static/",
69+
// "classpath:/public/"
70+
// };
71+
// @Override
72+
// public void addResourceHandlers(ResourceHandlerRegistry registry) {
73+
// registry.addResourceHandler("/**").addResourceLocations(CLASSPATH_RESOURCE_LOCATIONS);
74+
// registry.addResourceHandler("/static/**").addResourceLocations("classpath*:/static/").setCachePeriod(3600);// .resourceChain(true)
75+
// registry.addResourceHandler("/resources/**").addResourceLocations("classpath:/resources/", "classpath:/static/", "classpath:/public/").setCachePeriod(3600).resourceChain(true);
76+
// }
5277

5378
/**
54-
The Spring Click Servlet which handles *.htm requests.
55-
Add mapping informing ClickServlet to serve static resources contained under /click/* directly from Click's JAR files.
79+
The Spring Click Servlet which handles *.htm requests ~ "*.htm"
80+
Add mapping informing ClickServlet to serve static resources contained under /click/* directly from Click's JAR files ~ "/click/*"
5681
Please note, you only need this mapping in restricted environments where Click cannot deploy resources to the file system.
5782
*/
5883
@Bean
5984
public ServletRegistrationBean<SpringClickServlet> clickServlet() {
60-
val reg = new ServletRegistrationBean<>(new SpringClickServlet(), "*.htm", "/click/*");
85+
//val reg = new ServletRegistrationBean<>(new SpringClickServlet(), "*.htm", "/click/*");
86+
val reg = new ServletRegistrationBean<>(new SpringClickServlet(), "*.htm");
6187
reg.setName("ClickServlet");
6288
reg.setLoadOnStartup(0);
6389

@@ -73,10 +99,10 @@ public ServletRegistrationBean<SpringClickServlet> clickServlet() {
7399
return reg;
74100
}
75101

76-
@Override
77-
public void addViewControllers(ViewControllerRegistry registry) {
78-
registry.addViewController("/").setViewName("forward:/home.htm");
79-
}
102+
// @Override
103+
// public void addViewControllers(ViewControllerRegistry registry) {
104+
// registry.addViewController("/").setViewName("forward:/home.htm");
105+
// }
80106

81107
/**
82108
Provides an in memory database initialization listener.

0 commit comments

Comments
 (0)