Future Optimization

Security (before deploying to AWS!!!)

Caching and cache-busting

Serving static content and gzip encoding it.

https://css-tricks.com/the-difference-between-minification-and-gzipping/

Gzipping reduces the file size about five times as much as minifying does. But, you get a little boost from minifying as well, and since it likely requires little additional effort in a build step, you might as well.

There is also some evidence that browsers can read and parse a minified file faster

 

First Thymeleaf template

Added the thymeleaf starter to my Spring Boot application.

Added a @RequestMapping to my Application java file:

package com.sgbsc;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

@SpringBootApplication
@Controller
public class Bsc1Application {

@RequestMapping("/hello-template")
public String helloTemplate() {
return "hello-template";
}

public static void main(String[] args) {
SpringApplication.run(Bsc1Application.class, args);
}
}

Needed the @Controller to tell Spring what to return for the URL.

In Spring, a Controller is a class which is responsible for preparing  a model Map with data to be displayed by the view as well as choosing the right view itself. It can also directly write into response stream by using @ResponseBody annotation and complete the request.

If we were writing a RESTful application, we’d use @RestController which is a convenience controller meaning @Controller + @ResponseBody.

This application just returns two pages:

index.html

  • sent back automatically by the DispatcherServlet
  • auto-configured by Spring Boot upon seeing @SpringBootApplication and Web starter in pom.xml.
  • It looks for an index.html, and css under the static resources folder.

hello-template.html

  • Spring Boot notices thymeleaf starter in pom.xml
  • Spring’s RequestMappingHandlerMapping is used to maintain the mapping of the request URI to the handler (which are the controller methods annotated with the @RequestMapping annotation). Once the handler is obtained, the DispatcherServlet
    dispatches the request to the appropriate handler adapter.

src/main/resources/static/index.html

<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>bsc1</title>
<link rel="stylesheet" href="/css/bsc1.css"/>
<link rel="shortcut icon" href="/favicon.ico" type="image/x-icon">
<link rel="icon" href="/favicon.ico" type="image/x-icon">
</head>
<body>
<h1>Hello, and welcome to a static "hello world" page!</h1>
</body>
</html>

src/main/resources/static/css/bsc1.css

@charset "ISO-8859-1";
body {
background-color: lightblue;
}

h1 {
color: white;

src/main/resources/static/template/hello-template.html

<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>bsc1 - hello template</title>
<link rel="stylesheet" href="/css/bsc1.css"/>
<link rel="shortcut icon" href="/favicon.ico" type="image/x-icon">
<link rel="icon" href="/favicon.ico" type="image/x-icon">
</head>
<body>
<h1>This is a Thymeleaf template!</h1>
</body>
</html>

I also used a free icon generator to make a favicon.ico and put it under src/main/resources/static too.

First application: bsc1

I created a Spring Boot application, choosing: h2, JDBC and Web.

I’m going to use Profiles as a program argument, leaving the dev as application.properties, then changing out for application-test.properties and using
–spring.profiles.active=test in the Run Configuration.

I’ve hooked it up to the remote repository on GitHub, and am working on the ‘initial’ branch.

I’ve opened it up in the Git window of STS, but I’m using GitHub Desktop to manage commits with help from the command window (as I got into a pickle and had /project/project and couldn’t see anything!)

Anyway… up and running now.

I’m following on 2x speed the O’Reilly Introduction to Spring Boot course with Kevin Bowersox to make a CRUD application.

The Spring in Action 5 book is out next week building a Taco application, so I’d like to make mine first, then make that and apply it to my project as I’m going along. I want a super project in the end full of notes 🙂

I’ve started looking at the tutorial again and he’s using Thymeleaf, then Mustache. Now, in my past I used JSP and this is still used, however, there’s all this new JavaScript stuff with Angular and React too. Spring say that there are known limitations of using JSP in embedded servlet containers. Fair enough then. I found a Spring tutorial:

https://spring.io/guides/tutorials/spring-security-and-angular-js/

And I found a few templating engine examples:

https://www.baeldung.com/spring-template-engines

Hmmm… what to do….

I think I’ll follow the O’Reilly tutorial first, then create a new branch to test out the other front-ends. I’m doing an Angular Live Online Training next week, so maybe I can wrap up the front-end exploration with that and have some great examples 🙂