spring-boot without template engine

2020-07-04 12:44发布

问题:

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?

回答1:

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.