Random Quotes Generator in spring Boot
Neelakandan R

Neelakandan R @neelakandan_ravi

About: I have worked in bank for 8 month and after some gap in my career,now currently started learning full stack java to restart my career...😜

Location:
Chennai
Joined:
Dec 19, 2024

Random Quotes Generator in spring Boot

Publish Date: Apr 14
17 5

Quotes Generator

1.controller

package com.example.demo.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;


import com.example.demo.service.quoteservice;

@Controller
public class QuotesController
{

@Autowired
private quoteservice quoteService; 

@GetMapping("/")
public String show_Quotes_Page(Model model)
    {
         String quote =  quoteService.send_quote(); 
        model.addAttribute("quo", quote); 
        return "quotes"; 
    }

}
Enter fullscreen mode Exit fullscreen mode

2.Service

package com.example.demo.service;

import java.util.List;
import java.util.Random;

import org.springframework.stereotype.Service;

@Service
public class quoteservice
{
public String send_quote()
{
List<String> quotesList = List.of
        ("Failure is Stepping Stone of Success", 
        "Hardwork never fails", 
        "Love is Life", 
        "I ride my soul", 
        "Talent never fails"); 
Random random = new Random(); 
int random_no = random.nextInt(quotesList.size()); 

return quotesList.get(random_no); 
}

}

Enter fullscreen mode Exit fullscreen mode

3.html.file

<!DOCTYPE>
<html xmlns:th="http://www.thymeleaf.org">
<head>
    <title> Random Quote Generator </title>
    <style>


    </style>
</head>
<body>
    <div>
        <form action="/" method="get">
        <h2> Random Quote Generator </h2>
        <h3 th:text = "${quo}"> </h3>
        <button type="submit"> Next Quote </button>
         </form>
    </div>


</body>

</html>
Enter fullscreen mode Exit fullscreen mode

Comments 5 total

Add comment