@ModelAttribute একটি গুরুত্বপূর্ণ অ্যানোটেশন যা Spring MVC তে মডেল অবজেক্ট (Model Objects) থেকে ডেটা পাবার জন্য এবং ফর্ম ডেটা মডেল অবজেক্টে বেঁধে (bind) দেওয়ার জন্য ব্যবহৃত হয়। এটি সাধারণত ফর্ম ডেটা গ্রহণ করার জন্য ব্যবহার করা হয় এবং সেই ডেটা কন্ট্রোলার মেথডে পাঠানো হয়।
@ModelAttribute অ্যানোটেশনটি Controller ক্লাসে মডেল অবজেক্ট (POJO) এর জন্য বা মেথড প্যারামিটার হিসেবে ব্যবহৃত হয় এবং এটিকে বিভিন্নভাবে ব্যবহার করা যেতে পারে।
@ModelAttribute এর বিভিন্ন ব্যবহার
১. ফর্ম ডেটা মডেল অবজেক্টে বেঁধে দেওয়া (Binding form data to model object)
এটি সবচেয়ে সাধারণ ব্যবহার, যেখানে ফর্ম ডেটা সরাসরি মডেল অবজেক্টে bind করা হয়।
উদাহরণ:
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PostMapping;
@Controller
public class UserController {
// This method initializes the model with a new User object.
@ModelAttribute("user")
public User setUpForm() {
return new User(); // Empty User object, can be filled with form data
}
@GetMapping("/userForm")
public String showForm() {
return "userForm"; // Returns the view name (userForm.jsp or Thymeleaf template)
}
@PostMapping("/submitForm")
public String submitForm(@ModelAttribute User user) {
// Do something with the User object (e.g., save it)
System.out.println("User: " + user.getUsername() + ", " + user.getAge());
return "formSuccess";
}
}
userForm.jsp (view):
<form action="/submitForm" method="post">
<label for="username">Username:</label>
<input type="text" id="username" name="username" value="${user.username}">
<label for="age">Age:</label>
<input type="number" id="age" name="age" value="${user.age}">
<button type="submit">Submit</button>
</form>
ব্যাখ্যা:
@ModelAttribute("user"): এটি একটি User অবজেক্ট তৈরি করে এবং এটিuserনামে মডেলে যোগ করা হয়। যখন ফর্ম সাবমিট করা হয়, ফর্ম ডেটা এই অবজেক্টে bind হয়ে যাবে।@ModelAttribute User user: এখানেUserঅবজেক্ট ফর্মের ডেটা থেকে স্বয়ংক্রিয়ভাবে পূর্ণ হবে।
২. @ModelAttribute ব্যবহার করে মডেল ডেটা যোগ করা
@ModelAttribute মেথড লেভেলে ব্যবহার করলে, এটি একটি মডেল অবজেক্ট ইনিশিয়ালাইজ করতে সহায়তা করে, যা আপনার ভিউতে উপলব্ধ থাকবে।
উদাহরণ:
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
@Controller
public class UserController {
@ModelAttribute("user")
public User setUpForm() {
return new User("John", 30); // Initializes a User object with predefined values
}
@GetMapping("/userPage")
public String showPage() {
return "userPage"; // Returns the view name (userPage.jsp or Thymeleaf template)
}
}
userPage.jsp (view):
<h1>User Information</h1>
<p>Username: ${user.username}</p>
<p>Age: ${user.age}</p>
ব্যাখ্যা:
@ModelAttribute("user"): মেথডটি কন্ট্রোলার লেভেলেuserমডেল অবজেক্ট প্রদান করে, যা ভিউতে ব্যবহৃত হতে পারে।@ModelAttributeদ্বারা ফর্ম বা ডেটা না থাকলেও আপনি একটি প্রাথমিক মান দিয়ে মডেল অবজেক্ট তৈরি করতে পারেন।
৩. @ModelAttribute ব্যবহার করে কাস্টম মডেল ডেটা ইনিশিয়ালাইজেশন
@ModelAttribute এর সাহায্যে, আপনি ভিউ-সুনির্দিষ্ট ডেটা বা গ্লোবাল ডেটা ইনিশিয়ালাইজ করতে পারেন যা প্রতিটি রিকোয়েস্টে সঠিকভাবে ম্যাপ হয়।
উদাহরণ:
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
@Controller
public class AdminController {
// This method adds a global attribute to every view
@ModelAttribute("admin")
public String setUpAdmin() {
return "Admin User";
}
@GetMapping("/dashboard")
public String showDashboard() {
return "dashboard";
}
}
dashboard.jsp (view):
<h1>Welcome ${admin}</h1>
ব্যাখ্যা:
@ModelAttribute("admin"): এখানে কন্ট্রোলার মেথডে একটি গ্লোবাল প্যারামিটারadminযুক্ত করা হয়েছে যা প্রতিটি ভিউতে অ্যাক্সেস করা যাবে।
৪. @ModelAttribute এবং ফর্ম ভ্যালিডেশন
Spring MVC তে, ফর্ম ডেটার ভ্যালিডেশন করতে JSR-303 (Bean Validation) ব্যবহার করা যায় এবং @ModelAttribute এর মাধ্যমে এটি ইনপুট ফর্মে bind করা হয়।
উদাহরণ:
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.Valid;
@Controller
public class UserController {
@PostMapping("/submitForm")
public String submitForm(@ModelAttribute @Valid User user, BindingResult result) {
if (result.hasErrors()) {
return "userForm"; // Return to the form if validation fails
}
return "formSuccess"; // Proceed if validation is successful
}
}
ব্যাখ্যা:
@Valid: এটি ফর্ম ডেটা ভ্যালিডেশন চালানোর জন্য ব্যবহৃত হয়।BindingResult: এটি ব্যাকএন্ডে ফর্ম ডেটার ভ্যালিডেশন ফলাফল ধারণ করে এবং আপনি এর মাধ্যমে ত্রুটি চেক করতে পারবেন।
User ক্লাস:
import javax.validation.constraints.NotEmpty;
public class User {
@NotEmpty(message = "Username cannot be empty")
private String username;
private int age;
// Getters and Setters
}
ব্যাখ্যা:
- এখানে
@NotEmptyঅ্যানোটেশন ব্যবহার করা হয়েছে, যা নিশ্চিত করবে যে username ফিল্ড খালি না থাকবে।
উপসংহার
@ModelAttributeঅ্যানোটেশন Spring MVC তে ডেটা পাস করার একটি শক্তিশালী পদ্ধতি।- ফর্ম ডেটা bind করতে, কাস্টম মডেল ডেটা ইনিশিয়ালাইজেশন, এবং ভ্যালিডেশন এর জন্য এটি ব্যবহৃত হয়।
- আপনি
@ModelAttributeদিয়ে ফর্ম ভ্যালিডেশন, ডাটা ইনিশিয়ালাইজেশন, এবং গ্লোবাল ডেটা প্রতিটি রিকোয়েস্টে সহজেই ইনজেক্ট করতে পারবেন।
Read more