DEV Community

Neelakandan R
Neelakandan R

Posted on

Random Quotes Generator in spring Boot

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

Top comments (5)

Collapse
 
vigneshwaralingam profile image
Vigneshwaralingam

epd implement panni pakrathu bro

Collapse
 
neelakandan_ravi profile image
Neelakandan R
Collapse
 
vigneshwaralingam profile image
Vigneshwaralingam

Thank you 👍

Collapse
 
vigneshwaralingam profile image
Vigneshwaralingam

java,html,springboot ellathayum epd connect panninga

Collapse
 
neelakandan_ravi profile image
Neelakandan R

Using spring boot we can connect java and html
Platform -springtoolsuite4