Work with Eclipse





DESCRIPTION :

This is a basic Spring Boot application that provides a web-based calculator for performing addition of two numbers. The program is designed to accept two numbers as input, perform the addition operation, and display the result.

Step 1: Install Eclipse and Set Up Java

  1. Download Eclipse IDE:

    • If you don't have Eclipse installed, download it from the Eclipse Downloads Page.
    • Select Eclipse IDE for Java Developers and install it.
  2. Install Java Development Kit (JDK):

    • You’ll need to have Java installed on your machine.
    • Download JDK from Oracle's Website or AdoptOpenJDK.
    • Set the JAVA_HOME environment variable and update the PATH.

Step 2: Set Up Spring Boot Project in Eclipse

  1. Install Spring Tool Suite (STS) Plugin for Eclipse:
    • To make it easier to develop Spring Boot applications, you can install Spring Tool Suite (STS), a set of plugins for Eclipse that simplifies Spring development.
    • To install the STS plugin:
      • Open Eclipse.
      • Go to Help > Eclipse Marketplace.
      • In the marketplace, search for Spring Tools and install the plugin.
  2. Create a Spring Boot Project:
    • Open Eclipse and click on File > New > Spring Starter Project.
    • Fill in the details:
      • Name: addition-app
      • Type: Maven Project
      • Group: com.example
      • Artifact: additionapp
      • Dependencies: Select Spring Web.
    • Click Finish to create the project.

Step 3: Set Up Project Structure

Once the project is created, you should see a directory structure like this:

  • src/main/java/com/example/additionapp
  • src/main/resources

Step 4: Create a Controller for Addition Logic

  1. Create a Controller Class:

    • Right-click on the com.example.additionapp package and create a new class called AdditionController.java.
package com.example.additionapp;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class AdditionController {

    @GetMapping("/add")
    public String addNumbers(@RequestParam(name = "num1", defaultValue = "0") int num1,
                              @RequestParam(name = "num2", defaultValue = "0") int num2) {
        int sum = num1 + num2;
        return "The sum of " + num1 + " and " + num2 + " is: " + sum;
    }
}

This class does the following:
  • It’s a @RestController, which means it will handle HTTP requests and send back a response.
  • The @GetMapping("/add") annotation maps HTTP GET requests to the /add URL.
  • It accepts two query parameters (num1 and num2) using the @RequestParam annotation.
  • It performs the addition of the two numbers and returns the result as a string.
  1. Run the Application:

    • Right-click the project in Eclipse and select Run As > Spring Boot App. This will start the application, and the server will run on http://localhost:8080 by default.

Step 5: Add a Simple HTML Interface (Optional)

You can add an HTML interface to make it user-friendly, allowing users to input numbers and submit them for addition.

  1. Create an HTML File:

    • Under the src/main/resources/static directory, create an index.html file.
  2. Add HTML Code for User Input:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Addition Calculator</title>
</head>
<body>
    <h2>Simple Addition</h2>
    <form action="/add" method="get">
        <label for="num1">Number 1:</label>
        <input type="text" id="num1" name="num1"><br><br>
        <label for="num2">Number 2:</label>
        <input type="text" id="num2" name="num2"><br><br>
        <button type="submit">Add</button>
    </form>
</body>
</html>

3.Test the Application:

  • When you run the Spring Boot application, navigate to http://localhost:8080/ in your browser. You’ll see a form where you can enter two numbers and click "Add".
  • When you submit the form, the sum will be displayed on the same page (you might want to tweak it to display the result on the page or redirect to a result page).

Step 6: Review and Final Touches

Your simple addition program with Spring Boot is now up and running! Here’s a summary of the project:

  1. Controller (AdditionController.java): Handles the addition logic and responds with the result.
  2. HTML Interface: Allows users to input two numbers for addition.
  3. Running the Application: The Spring Boot app runs on localhost:8080, and users can interact with the app via browser or API requests.

Comments

Popular posts from this blog

LEETCODE PRACTICE

II-MSC ( file Operation)

Java - SpringBoot