Spring boot

How to add Two numbers using Spring boot

This tutorial will teach you how to add two numbers using Spring Boot.

First Step

Select the Template folder.Inside the template folder create index.html page.

index.html

<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Insert title here</title>

<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.0.0-beta1/dist/css/bootstrap.min.css" rel="stylesheet" >

</head>
<body>
<div class="container">
 <div class="row">
      <div>
  <h1>Add Two Numbers</h1>
      </div>  
         <form th:action="@{addtion}" th:object="${addtion}" method="post">
       <div>
  <label>Num1</label>
  <input type="text" id="num1" class="form-control" name="num1" > 
       </div>
       <div>
  <label>Num2</label>
  <input type="text" id="num2" class="form-control" name="num2" >
       </div>
  </br>
        <div>
  <input type="submit" value="submit" />
        </div>
    </form>
 </div>
</div>
</body>
</html>

Create the  Package Domain

After that select the  src/main/java right click the new package create the package name as com.example.FormExampleSpring.domain

After created the package select the package com.example.FormExampleSpring.domain  right click and  create the class file name Addtion.class

public class Addtion 
{
 private int num1;
 private int num2;
 public int getNum1() 
 {
  return num1;
 }
 public void setNum1(int num1) 
 {
  this.num1 = num1;
 }
 public int getNum2() 
 {
  return num2;
 }
 public void setNum2(int num2) 
 {
  this.num2 = num2;
 }
 public int Total()
 {
  return num1 + num2;
 }
}

After that  Create the Controller

AdditionController

After  that create the Controller  select src/main/java right click new package create the package name as com.example.FormExampleSpring.StudentController

inside the package create the controller as AdditionController.java

@Controller
public class AdditionController 
{
 @GetMapping("/index")
 public String ViewForm(Model model)
 {
  model.addAttribute("Addition", new Addtion());
  return "index";
 }
 
 @PostMapping("/addtion")
 public String addForm(@ModelAttribute Addtion addtion,BindingResult result,Model model)
 {
  model.addAttribute("addtion",addtion);
  return "calculate";
 }
}

After fill the Form click Submit goes to calculate.html and display the total

After that create the calculate.html Page

calculate.html

<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
  <h1>Result</h1>
  <p th:text="${addtion.Total()}"></p>
</body>
</html>

i have attached the video link below. which will do this tutorials step by step.

 

 

admin

Recent Posts

Chat with Claude AI Free – Your Super-Smart AI Buddy

It's like having a super-smart buddy that is always there to help you write stories,…

2 days ago

Best Festivals UK 2025 [Free Guide Included]

The UK is known for its rich history, diverse culture, and most of all  its…

5 days ago

Bank Holidays 2025 UK – Plan Your Perfect Long Weekends

Do you have a plan for your next holiday? Being aware of the Bank Holidays within the…

5 days ago

Master Cursor AI Full Guide for Students & Creators

The world is rapidly changing of software development AI-assisted tools for coding have become the main focus. As…

6 days ago

Google Gemini AI Free AI Tool for Students & Creators

Google Gemini AI is among the top talked about developments. What exactly is it? What…

6 days ago

Top 5 Reasons You Should Use Grubby AI Humanizer in 2025

As the need for AI content undetectable humanization software skyrockets with more and more AI…

2 weeks ago