没有模板引擎的spring-boot

son*_*rin 11 spring-mvc spring-boot

我有一个Spring-Boot应用程序显然已超出常态.它是Spring-MVC,但我不想使用速度,百里香或任何东西.理想情况下,我只会使用HTML.然后我使用jQuery对我的REST服务进行AJAX调用,然后使用返回的JSON填充页面.我最常能让它工作的唯一方法是将我的html放在下面/src/resources/templates,然后为每个页面都有一个@Controller.所以我有:

@SpringBootApplication

public class Application {
    public static void main(String[] args) throws Throwable {
        SpringApplication.run( Application.class, args );
    }
}
Run Code Online (Sandbox Code Playgroud)

和我的控制器

@Controller
public class HomeController {
    @RequestMapping("/")
    public String getHome() {
        return "index"
}
Run Code Online (Sandbox Code Playgroud)

@Controller
public class AboutController {
    @RequestMapping("/about")
    public String getAbout() {
        return "about"
}
Run Code Online (Sandbox Code Playgroud)

我看过Spring指南和示例项目,但我没有看到如何配置它.我正在使用启动项目获得spring-mvc和安全性,但是否则我看不到我需要做什么,所以导航到:

localhost/homelocalhost/aboutlocalhost/customer

或者,是否有必要为每个页面都有一个@Controller?

Rol*_*der 10

如果您想使用HTML和JavaScript等静态资源,可以将它们放入/src/main/resourcesnamed 的子文件夹中/public,/static或者/resources.例如,位于at的文件/src/main/resources/public/dummy/index.html将是可访问的via http://localhost/dummy/index.html.你不需要控制器.

另请参见" 参考指南".

  • 谢谢@schlauergerd - 无论如何都不需要在网址上使用html扩展名?理想情况下,键入**localhost/office"**会有效,但是现在我必须附加".html"我有**spring.view.prefix = public**和**spring.view.suffix = html**现在在我的application.properties中. (3认同)