没有模板引擎的spring-boot [英] spring-boot without template engine
问题描述
我有一个显然不合常规的 Spring-Boot 应用程序.它是 Spring-MVC,但我不想使用速度、百里香叶或任何东西.理想情况下,我只会使用 HTML.然后我使用 jQuery 对我的 REST 服务进行 AJAX 调用,然后用返回的 JSON 填充页面.我可以让它工作的唯一方法是将我的 html 放在 /src/resources/templates
下,然后为每个页面设置一个 @Controller.所以我有:
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
@SpringBootApplication
public class Application {
public static void main(String[] args) throws Throwable {
SpringApplication.run( Application.class, args );
}
}
和我的控制器
@Controller
public class HomeController {
@RequestMapping("/")
public String getHome() {
return "index"
}
和
@Controller
public class AboutController {
@RequestMapping("/about")
public String getAbout() {
return "about"
}
我已经浏览了 Spring 指南和示例项目,但我不知道如何配置它.我正在使用启动项目来获取 spring-mvc 和安全性,但除此之外我不明白我需要做什么,所以导航到:
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 或 localhost/about 或 localhost/customer
或者,是否需要每个页面都有一个@Controller?
Or, is it necessary to have a @Controller for each page?
推荐答案
如果你想使用像 HTML 和 JavaScript 这样的静态资源,你可以将它们放在名为 /src/main/resources
的子文件夹中/public
、/static
或 /resources
.例如,位于 /src/main/resources/public/dummy/index.html
的文件可以通过 http://localhost/dummy/index.html
访问.为此,您不需要控制器.
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.
另见 参考指南.
这篇关于没有模板引擎的spring-boot的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!