简体   繁体   English

使用Spring Boot映射html资源

[英]Mapping html resources using Spring Boot

I'm using Spring Boot to create a REST API for a site I'm creating. 我正在使用Spring Boot为我正在创建的网站创建REST API。 Currently, to access static content, I simply go to the resources name. 目前,要访问静态内容,我只需转到资源名称。 For example, /index.html or /dashboard.html . 例如, /index.html/dashboard.html Since I'm using REST controllers, there's no way to map pages to certain URL endings and there's no web.xml file since I'm using Spring Boot. 由于我使用的是REST控制器,因此无法将页面映射到某些URL结尾,因为我使用的是Spring Boot,所以没有web.xml文件。 (for example, I want /dashboard to display the dashboard.html file.) (例如,我希望/dashboard显示dashboard.html文件。)

Should I use standard MVC @Controller controllers to control my page mapping or is there a better way to do this? 我应该使用标准MVC @Controller控制器来控制我的页面映射还是有更好的方法来做到这一点?

EDIT 编辑

I can currently access static content by simply typing in the file name in the URL. 我现在只需在URL中输入文件名即可访问静态内容。 (ex. /index.html will open the index.) (例如/index.html将打开索引。)

What I want to know, is how to remap static content. 我想知道的是,如何重新映射静态内容。 For instance, if I want /dashboard to display dashboard.html or if I want /error to display /error.html etc... It'll make it nicer/easier for a user to simply go to www.mydomain.com/dashboard instead of having to add the .html endings. 例如,如果我想/dashboard显示dashboard.html或者我想要/error显示/error.html等...它会让用户更好/更容易地访问www.mydomain.com/dashboard而不是必须添加.html结尾。

Is there a way to remap pages like this with Spring Boot? 有没有办法用Spring Boot重新映射这样的页面?

EDIT 2 编辑2

I wish to have the burden of generating the views of each page placed upon the client's system. 我希望负担生成放在客户系统上的每个页面的视图。 In other words, I do NOT want the server to generate each page. 换句话说,我不希望服务器生成每个页面。 I wish to have "templates" essentially which are then turned into useful pages with information VIA a REST API utilized by AngularJS controllers. 我希望拥有“模板”,然后将其转换为有用的页面,其中包含AngiaJS控制器使用的REST API信息。

You can use the Thymeleaf framework to serve HTML web content: 您可以使用Thymeleaf框架来提供HTML Web内容:

First, add the dependency: 首先,添加依赖项:

build.gradle 的build.gradle

    dependencies {
    compile("org.springframework.boot:spring-boot-starter-thymeleaf")
}

Then, create a Controller: 然后,创建一个Controller:

GreetingController.java GreetingController.java

@Controller
public class GreetingController {

    @RequestMapping("/greeting")
    public String greeting(@RequestParam(value="name", required=false, defaultValue="World") String name, Model model) {
        model.addAttribute("name", name);
        return "greeting";
    }

}

Then create a Template: 然后创建一个模板:

The name of the name.html file must exactly match the String returned in the Controller in this case "greeting" name.html文件的名称必须与Controller中返回的String完全匹配,在本例中为"greeting"

src/main/resources/templates/greeting.html SRC /主/资源/模板/ greeting.html

<!DOCTYPE HTML>
<html xmlns:th="http://www.thymeleaf.org">
<head>
    <title>Serving HTML Web Content</title>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
</head>
<body>
    <p>HEY THERE!</p>
</body>
</html>

You can then access the page locally by navigating to: 然后,您可以通过导航到本地访问本地页面:

http://localhost:8080/greeting HTTP://本地主机:8080 /问候

If you want to use Angular, do not implement Thymeleaf (Thymeleaf is not RESTful). 如果你想使用Angular,不要实现Thymeleaf(Thymeleaf不是RESTful)。 Then what you can do is use $routeProvider to specify what static resource to serve to which URI. 然后你可以做的是使用$ routeProvider来指定为哪个URI提供的静态资源。 If you have an index.html in your static dir, Spring Boot will automatically use that as the default page, and then your other static URI's used within Angular will be relative to the index page. 如果在静态目录中有index.html ,Spring Boot会自动将其用作默认页面,然后在Angular中使用的其他静态URI将相对于索引页面。

https://docs.angularjs.org/api/ngRoute/provider/ $routeProvider https://docs.angularjs.org/api/ngRoute/provider/ $ routeProvider

  1. I would actually move all the static files - index.html, angular.js or bootstrap.js and all your angular template.html to a node front end proxy and proxy all your spring boot service calls through that (This will help me avoid retrofitting spring boot service for CORS). 我实际上将所有静态文件 - index.html,angular.js或bootstrap.js以及所有的angular template.html移动到节点前端代理并通过它代理所有弹簧启动服务调用(这将帮助我避免改装CORS的春季启动服务)。
  2. Or at least I would use nginx/httpd to serve all the static files and proxy all the service calls to spring boot app. 或者至少我会使用nginx / httpd来提供所有静态文件,并将所有服务调用代理到spring boot app。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM