URL Parameter Binding
@RequestParam Annotation
For URL parameters in format ?username=value&password=value, use @RequestParam annotation:
@RequestMapping("/auth.do")
public ModelAndView authenticate(
@RequestParam("username") String userName,
@RequestParam(value = "password", required = false) String userPassword) {
ModelAndView mv = new ModelAndView("Dashboard");
System.out.println("User: " + userName + " Password: " + userPassword);
return mv;
}
@PathVariable Annotation
For REST-style URLs like /user/{id}/{role}, use @PathVariable:
@RequestMapping(value = "/profile/{userId}/{role}", method = RequestMethod.GET)
public ModelAndView loadProfile(
@PathVariable("userId") String userId,
@PathVariable("role") String userRole) {
ModelAndView mv = new ModelAndView("Profile");
System.out.println("ID: " + userId + " Role: " + userRole);
return mv;
}
Form Data Binding
Direct Parameter Binding
HTML form with input fields:
<form method="post" action="/submit.do">
Username: <input type="text" name="userName"/>
Password: <input type="password" name="userPass"/>
<input type="submit"/>
</form>
Controller method with matching parameter names:
@RequestMapping(value = "/submit.do", method = RequestMethod.POST)
public ModelAndView processForm(String userName, String userPass) {
ModelAndView mv = new ModelAndView("Result");
System.out.println("Username: " + userName + " Password: " + userPass);
return mv;
}
POJO Object Binding
User entity class:
public class UserAccount {
private String userName;
private String userPass;
public String getUserName() { return userName; }
public void setUserName(String userName) { this.userName = userName; }
public String getUserPass() { return userPass; }
public void setUserPass(String userPass) { this.userPass = userPass; }
}
Controller using object binding:
@RequestMapping("/register.do")
public ModelAndView registerUser(UserAccount account) {
ModelAndView mv = new ModelAndView("Welcome");
System.out.println("User: " + account.getUserName() +
" Pass: " + account.getUserPass());
return mv;
}
Servlet API Parameter Access
@RequestMapping("/process.do")
public ModelAndView processRequest(HttpServletRequest req) {
String username = req.getParameter("userName");
System.out.println("Username: " + username);
return new ModelAndView("Output");
}
Array Parameter Binding
Form with multiple checkboxes:
<form method="post" action="/preferences.do">
<input type="checkbox" name="interests" value="sports" />Sports
<input type="checkbox" name="interests" value="music" />Music
<input type="checkbox" name="interests" value="reading" />Reading
<input type="submit"/>
</form>
Controller handling array parameters:
@RequestMapping(value = "/preferences.do", method = RequestMethod.POST)
public ModelAndView savePreferences(String[] interests) {
ModelAndView mv = new ModelAndView("Preferences");
for (String interest : interests) {
System.out.println("Interest: " + interest);
}
return mv;
}
Date Parameter Binding
Custom date binding configuration:
@InitBinder
protected void initBinder(WebDataBinder binder) {
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
dateFormat.setLenient(false);
binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, true));
}
Form with date input:
<form method="post" action="/birthday.do">
Birth Date: <input type="date" name="birthDate"/>
<input type="submit"/>
</form>
Controller with date parameter:
@RequestMapping(value = "/birthday.do", method = RequestMethod.POST)
public ModelAndView recordBirthday(Date birthDate) {
ModelAndView mv = new ModelAndView("Birthday");
System.out.println("Birth Date: " + birthDate);
return mv;
}