Step:1
File -> String Starter Project -> Project name Random_Quote_Generator ->Type Maven -> Packaging Jar-> Next
Step:2
Add dependencies:-
- Spring Web
- Spring Dev Tools
- Thymeleaf then finish. If unfortunately you not add missed some dependencies that time your project pom.xml (Right click)-spring -> ADD startes -> next -> you can add missed dependencies -> Next -> select pom.xml -> finish,
- spring web and Thymeleaf before we saw what and why, now we see spring Dev Tool.
What is Spring Boot DevTools?
Spring Boot DevTools is a development-ime tool that helps developer save time and increase productivity by automatically reloading the application when code changes are made.
work faster less effort,you do not need to stop and restart the app and again and again ,DevTool handle it , you can focus on writing code ,not managing the server.
### Why to use Spring DevTools?
- Automatic Restart -> Automatically restart the app when you change a class or file like home.html file.
- Live Reload -> Automatically refresh the browser when templates(html/Thymeleaf)change.
- Catching Disabled -> Disable template or static file catching during development.
- No need to Re-run -> you do not need to stop/start/ the app manually every time.
Step:3
create QuoteService class then add top of class @Service ,ctr+shift+o(import service)
step:4
create method:
public String show_quote()
{
//List<String> quoteList = new ArrayList<String>
List<String> quoteList = List.of("Try Hard","Hard work never fails","Spend Time wisely","Practice brings Perfection");
Random random = new Random();
int random_no =random.nextInt(quoteList.size());
return quoteList.get(random_no);
}
Why not used now this line
List<String> quoteList = new ArrayList<String>
- Creates a modifiable(not-fixed) list
- you can add, remove,update values.
- Used when list changes at runtime
why used this line
List<String> quoteList = List.of(....);
- creates an unmodified(fixed) list
- you can not modify(add/remove) values.
- used when list constant/randomly changed
Step:5 Random is a class in Java (java.util.Random)
- it is used to generate the random numbers.Can be used to get: Random int,double,Boolean...etc. nextInt is generate random number from 0 to n-1 , when use quoteList.size() ,it give random valid index list like now we have 3 list quoteList, random number from 0 to 2 , because random starts 0
Random random = new Random();
int num =random.nextInt(quoteList.size());
Step:6
create controller class QuotesController and we use dependence injection , one of type for field(auto-wiring with @Autowired) not to create object,if you create object it lead to tightly couple.
@GetMapping is used in a controller to handle HTTP GET requests.Model is an interface used in controller classes to store and pass objects (like List, String, .. etc)(data) to (Thymeleaf template)HTML view pages."
Thymeleaf is a template engine — it takes that Java data and dynamically renders(not fixed type value will change based on back end data or user input ) HTML content.
public class QuotesController {
@Autowired
private QuoteService quoteService;
@GetMapping("/quotesPage")
public String display_Quotes(Model model)
{
String quote = quoteService.show_quote();
model.addAttribute("Proverb", quote);
return "quotes_page";
}
}
Step:7
Create quotes_page.html in /src/main/resource/template/
quotes_page.html
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<title> Random Quote Generator
</title>
<style>
div{
text-align: center;
}
</style>
<head>
<body>
<div>
<h2> Random Quote Generator</h2>
<h3 th:text="${proverb}"> </h3>
</div>
</body>
</html>
class QuoteService
import org.springframework.stereotype.Service;
@Service
public class QuoteService {
public String show_quote()
{
//List<String> quoteList = new ArrayList<String>
List<String> quoteList = List.of("Try Hard","Hard work never fails","Spend Time wisely","Practice brings Perfection");
Random random = new Random();
int random_no =random.nextInt(quoteList.size());
return quoteList.get(random_no);
}
}
// List is interface ,Hold multiple values like array ,follow insertion order(first added item stays first, second stays second),allowed duplicate values,you can access element by index.
Top comments (0)