This Spring Boot web application displays a random Chuck Norris Joke on the index.
Following the instructions of an online course about Spring Framework 5 on Udemy, I build this toy web application. It contains a Spring Service Layer (JokeService & JokeServiceimpl), a Spring MVC Controller (JokeController) and a View Layer (insides resources/templates).
The building steps go as following:
- Create Spring Boot Project from Spring Initializr (add 'Web' and 'Thymeleaf' dependencies)
- Add new Dependency: group: 'guru.springframework', name: 'chuck-norris-for-actuator', version: '0.0.2'
- Create Spring Service Layer:
- The Service returns a random joke string from
ChuckNorrisQuotes.getRandomQuote()
- The Service returns a random joke string from
- Create the Spring MVC Controller:
- Map context root ("/", "") to Jokes view
- Add the joke generated by Service to 'joke' property of Model (
model.addAttribute("joke", jokeService.getJoke())
) - Return view name of 'chucknorris'
- Create the View Layer
Through building this application, I gained some basic understandings of MVC and SpringFramework:
- When the client makes a request (for a webpage here), the Controller gets the Model (through the wired up Service layer) and returns the Model to the View.