4

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.)

Should I use standard MVC @Controller controllers to control my page mapping or is there a better way to do this?

EDIT

I can currently access static content by simply typing in the file name in the URL. (ex. /index.html will open the index.)

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.

Is there a way to remap pages like this with Spring Boot?

EDIT 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.

3 Answers 3

1

You can use the Thymeleaf framework to serve HTML web content:

First, add the dependency:

build.gradle

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

Then, create a Controller:

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"

src/main/resources/templates/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

Sign up to request clarification or add additional context in comments.

4 Comments

So essentially, I create an HTML template, the template is served to the user when the mapped URL is called, the template is then served, and AngularJS controllers then fill in the content by utilizing the REST API? So in other words, the server does not generate the "views" with thymeleaf? I want to remove that burden from the server and have JavaScript controllers utilize the REST API to fill in the templates.
Thymeleaf is more of a templating engine as opposed to dynamically generated web pages like JSPs. You do have access to Spring Expression language if you choose, but you can just serve barebones HTML5 if that fits your use case. There is some server-side processing however if you do choose to use thymeleaf attributes to generate or populate elements.
Are you trying to achieve a single-page architecture? B/c in that case thymeleaf won't be a absolutely perfect fit. If this is the case you can checkout jhipster.github.io. JHipster generates a Single-page application using angularJS/Twitter Bootstrap and a Spring Boot REST API backend.
Yes, the app is going to be a single page application. I'm trying to maintain a RESTful architecture if possible. I'll take a look at JHipster.
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.

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

4 Comments

So I should simply use Angular to remove the .html tags at the end then? There's no elegant way to do this RESTfully with Spring? If not, then I'll go this route. Thank you
Realistically, there are a lot of ways to do it. Spring just exposes a RESTful service, and how it's received is up to you. What do you mean by removing .html tags?
As in, if I visit /dashboard in my browser, it comes up with 404 not found. I have to type in /dashboard.html which is rather ugly. I'm wondering if there's a way to get rid of this on the server side while maintaining REST principles.
@JakeMiller .. you have to serve index.html and angular.js or bootstrap.js (well, you get the idea) anyway from somewhere, right? Since you have not mentioned nginx/http or a front end node proxy, I assume your spring boot has to serve that. Even if you don't consider serving static content from your spring boot is 'restful', you are not left with much options. Or just use nginx/httpd to serve all your static file. Then your spring boot will be left with only RESTful services.
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. Or at least I would use nginx/httpd to serve all the static files and proxy all the service calls to spring boot app.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.