Spring Boot এর মাধ্যমে Spring MVC প্রজেক্ট তৈরি করা অনেক সহজ এবং এটি Spring Framework-এর কনফিগারেশন এবং সেটআপ পদ্ধতিকে অনেকটাই সরল করে দেয়। Spring Boot প্রজেক্টে আপনি Spring MVC ফিচার সক্রিয় করতে পারবেন এবং বিভিন্ন ধরনের RESTful API বা web applications তৈরি করতে পারবেন।
এখানে আমি দেখাবো কিভাবে Spring Boot ব্যবহার করে একটি Spring MVC প্রজেক্ট তৈরি করা যায়।
Step 1: Spring Boot প্রজেক্ট তৈরি করা
Spring Initializr ব্যবহার করে Spring Boot প্রজেক্ট তৈরি করা সহজ। আপনি Spring Initializr ওয়েবসাইট (https://start.spring.io/) অথবা IDE (IntelliJ IDEA, Eclipse) ব্যবহার করতে পারেন।
Using Spring Initializr (Web Interface)
- Go to: Spring Initializr.
- Project: Gradle or Maven (Maven ব্যবহার করা সবচেয়ে সাধারণ এবং সহজ)।
- Language: Java।
- Spring Boot version: Choose the latest stable version (e.g., 2.5.x or later).
- Group: com.example
- Artifact: spring-mvc-example
- Dependencies:
- Spring Web
- Thymeleaf (Optional for View Templates, if you want to use JSP or another template engine, you can choose them too)
- Generate: Click on Generate to download the project.
Using IntelliJ IDEA or Eclipse:
- IntelliJ IDEA: Go to
File->New->Project->Spring Initializrand select the dependencies. - Eclipse: Go to
File->New->Spring Starter Projectand select the dependencies.
Once the project is created, you can open it in your IDE.
Step 2: Add Dependencies
Spring Boot প্রজেক্টের pom.xml ফাইলে, আপনি Spring Web ও Thymeleaf বা JSP এর জন্য প্রয়োজনীয় ডিপেনডেন্সি থাকতে হবে।
<dependencies>
<!-- Spring Web dependency for MVC functionality -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- Thymeleaf for rendering templates (optional) -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<!-- Spring Boot DevTools (optional for development) -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
</dependency>
<!-- Spring Boot Starter Test (for unit testing) -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
spring-boot-starter-web: এটি Spring MVC এবং RESTful API নির্মাণের জন্য প্রয়োজনীয় ফিচারগুলি সরবরাহ করে।spring-boot-starter-thymeleaf: Thymeleaf ব্যবহার করার জন্য।spring-boot-devtools: উন্নয়নকালীন ফিচার (যেমন অ্যাপ্লিকেশন স্বয়ংক্রিয়ভাবে রিফ্রেশ করা)।
Step 3: Main Application Class (Spring Boot Entry Point)
Spring Boot এ আপনার প্রজেক্টের Main Class থাকে যেখানে @SpringBootApplication অ্যানোটেশন থাকে এবং এটি Spring Boot অ্যাপ্লিকেশন চালু করে।
package com.example.springmvc;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class SpringMvcExampleApplication {
public static void main(String[] args) {
SpringApplication.run(SpringMvcExampleApplication.class, args);
}
}
Explanation:
@SpringBootApplication: এটি একটি কম্বিনেশন অ্যানোটেশন, যা@Configuration,@EnableAutoConfiguration, এবং@ComponentScanএর সমন্বয়ে তৈরি হয়, যা Spring Boot অ্যাপ্লিকেশন শুরু করতে সাহায্য করে।
Step 4: Spring MVC Controller
এখন একটি Controller তৈরি করুন যা HTTP রিকোয়েস্টগুলো হ্যান্ডেল করবে।
package com.example.springmvc.controller;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
@Controller
public class HomeController {
@GetMapping("/")
public String homePage(Model model) {
model.addAttribute("message", "Welcome to Spring MVC with Spring Boot!");
return "home"; // This will resolve to home.html or home.jsp based on configuration
}
}
Explanation:
@Controller: Spring MVC কন্ট্রোলারকে চিহ্নিত করে।@GetMapping("/"): এটি HTTP GET রিকোয়েস্ট হ্যান্ডেল করবে যেটি/পাথে আসবে।Model: মডেল ব্যবহার করে আপনি ডেটা ভিউতে পাঠাতে পারেন (এখানে "message" পাঠানো হচ্ছে)।
Step 5: Create Views (HTML or JSP)
home.html (Located in /src/main/resources/templates/ for Thymeleaf)
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<title>Spring MVC with Spring Boot</title>
</head>
<body>
<h1 th:text="${message}"></h1>
</body>
</html>
Explanation:
- Thymeleaf এর মাধ্যমে আপনি মডেল থেকে ডেটা দেখতে পাবেন। এখানে
"${message}"দ্বারাHomeControllerথেকে প্রাপ্ত মেসেজ রেন্ডার করা হচ্ছে।
home.jsp (Located in /src/main/webapp/WEB-INF/jsp/ for JSP)
<html>
<head>
<title>Spring MVC with Spring Boot</title>
</head>
<body>
<h1>${message}</h1>
</body>
</html>
Explanation:
- এখানে JSP ব্যবহার করা হয়েছে। যদি আপনি JSP ব্যবহার করতে চান, তাহলে আপনাকে Spring Boot-এ JSP কনফিগার করতে হবে। তবে Thymeleaf বেশ জনপ্রিয় এবং সাধারাণত Spring Boot প্রজেক্টে ব্যবহৃত হয়।
Step 6: Application Properties (Optional)
Spring Boot প্রজেক্টের জন্য কিছু কনফিগারেশন করতে application.properties বা application.yml ব্যবহার করা যেতে পারে।
src/main/resources/application.properties:
# Server configuration
server.port=8080
# Thymeleaf specific settings
spring.thymeleaf.prefix=classpath:/templates/
spring.thymeleaf.suffix=.html
spring.thymeleaf.mode=HTML
Step 7: Run the Application
- Run the Application:
- Using IntelliJ IDEA: Right-click on
SpringMvcExampleApplication.java→Run. Using Maven Command:
mvn spring-boot:run
- Using IntelliJ IDEA: Right-click on
- Access the Application:
- Open your browser and go to
http://localhost:8080/. You should see the home page with the message "Welcome to Spring MVC with Spring Boot!".
- Open your browser and go to
Conclusion
Spring Boot ব্যবহার করে Spring MVC প্রজেক্ট তৈরি করা অনেক সহজ। Spring Boot অ্যাপ্লিকেশন কনফিগারেশন এবং ডিপেনডেন্সি ম্যানেজমেন্ট অনেকটাই সরল করে দেয়, এবং Spring MVC এর মাধ্যমে আপনি RESTful APIs বা web applications তৈরি করতে পারেন। Spring Boot এর সাথে Thymeleaf ব্যবহার করলে HTML টেমপ্লেট রেন্ডারিং খুবই সহজ হয়, তবে আপনি চাইলে JSP বা অন্য টেমপ্লেট ইঞ্জিনও ব্যবহার করতে পারেন।
Read more