I have a Spring-Boot app that is apparently way out of the norm. It's Spring-MVC, but I don't want to use velocity,thymeleaf or anything. Ideally I would just use HTML. Then I use jQuery to make my AJAX calls to my REST services, and then fill in the pages with the returned JSON. The only way I can mostly get this to work is to put my html under /src/resources/templates
, and then have a @Controller for each page. So I have:
@SpringBootApplication
public class Application {
public static void main(String[] args) throws Throwable {
SpringApplication.run( Application.class, args );
}
}
and my controllers
@Controller
public class HomeController {
@RequestMapping("/")
public String getHome() {
return "index"
}
and
@Controller
public class AboutController {
@RequestMapping("/about")
public String getAbout() {
return "about"
}
I have looked thru the Spring guides and sample projects, but I don't see how to configure this. I am using the starter projects to get spring-mvc and security, but otherwise I don't see what I need to do this so navigating to :
localhost/home or localhost/about or localhost/customer
Or, is it necessary to have a @Controller for each page?
The default template directory is src/main/resources/templates . This is the Maven build file. The spring-boot-devtools enables hot swapping, disables template cache and enables live reloading. The spring-boot-starter-thymeleaf is a starter for building Spring MVC applications with Thymeleaf.
1. JMustache is a template engine which can be easily integrated into a Spring Boot application by using the spring-boot-starter-mustache dependency.
In the pursuit of comparison between FreeMarker , Thymeleaf, Groovy and Mustache, FreeMarker has the upper hand in performance. However, Thymeleaf wins the battle overall.
Thymeleaf is way better in my opinion because it have good underlying priciples and exploits natural behaviour of browsers. Jsp makes html hard to read, it becomes weird mixture of html and java code which makes a lot of problems in comunication between designer - developer.
If you want to use static resources like HTML and JavaScript you can place them into a subfolder of /src/main/resources
named /public
, /static
or /resources
. For example a file located at /src/main/resources/public/dummy/index.html
will a accessible via http://localhost/dummy/index.html
. You won't need a Controller for this.
See also the Reference Guide.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With