使用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. Currently, to access static content, I simply go to the resources name. For example, /index.html or /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. (for example, I want /dashboard to display the dashboard.html file.)

我正在使用Spring Boot為我正在創建的網站創建REST API。目前,要訪問靜態內容,我只需轉到資源名稱。例如,/ index.html或/dashboard.html。由於我使用的是REST控制器,因此無法將頁面映射到某些URL結尾,因為我使用的是Spring Boot,所以沒有web.xml文件。 (例如,我希望/ 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. (ex. /index.html will open the index.)

我現在只需在URL中輸入文件名即可訪問靜態內容。 (例如/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.html或者我想要/錯誤顯示/error.html等...它會讓用戶更好/更容易地訪問www.mydomain.com/儀表板,而不是必須添加.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信息。

3 个解决方案

#1


0  

If you want to use Angular, do not implement Thymeleaf (Thymeleaf is not RESTful). Then what you can do is use $routeProvider to specify what static resource to serve to which 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.

如果你想使用Angular,不要實現Thymeleaf(Thymeleaf不是RESTful)。然后你可以做的是使用$ routeProvider來指定為哪個URI提供服務的靜態資源。如果你的靜態目錄中有一個index.html,Spring Boot會自動將其用作默認頁面,然后在Angular中使用的其他靜態URI將相對於索引頁面。

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

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

#2


1  

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 /問候

#3


0  

  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).
  2. 我實際上將所有靜態文件 - index.html,angular.js或bootstrap.js和所有角度template.html移動到節點前端代理並代理所有彈簧引導服務調用(這將幫助我避免改裝CORS的spring boot服務)。
  3. Or at least I would use nginx/httpd to serve all the static files and proxy all the service calls to spring boot app.
  4. 或者至少我會使用nginx / httpd來提供所有靜態文件,並將所有服務調用代理到spring boot app。

注意!

本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:https://www.itdaan.com/blog/2016/10/27/7253451046714fb7a7933c7e445d7d89.html



 
粤ICP备14056181号  © 2014-2021 ITdaan.com