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
-
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.
-
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_HOMEenvironment variable and update thePATH.
Step 2: Set Up Spring Boot Project in Eclipse
- 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.
- 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/additionappsrc/main/resources
Step 4: Create a Controller for Addition Logic
-
Create a Controller Class:
- Right-click on the
com.example.additionapppackage and create a new class calledAdditionController.java.
- 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/addURL. - It accepts two query parameters (
num1andnum2) using the@RequestParamannotation. - It performs the addition of the two numbers and returns the result as a string.
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:8080by default.
- Right-click the project in Eclipse and select Run As > Spring Boot App. This will start the application, and the server will run on
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.
-
Create an HTML File:
- Under the
src/main/resources/staticdirectory, create anindex.htmlfile.
- Under the
-
Add HTML Code for User Input:
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:
- Controller (
AdditionController.java): Handles the addition logic and responds with the result. - HTML Interface: Allows users to input two numbers for addition.
- Running the Application: The Spring Boot app runs on
localhost:8080, and users can interact with the app via browser or API requests.

Comments
Post a Comment