I am practicing ModelAttribute in Spring MVC I had read that if no value is supplied then it will load default value so is there any way we can achieve that. I have tried below program and when I comment the setName() method line then no value is displayed. Could anyone help me out how we can achieve that if it is possible?
User.java
public class User{
private String name; //generate getter and setter
}
UserController.java
@Controller
public class UserController {
@RequestMapping(value="init", method = RequestMethod.GET)
public String saveCountry(@ModelAttribute("user") User user, ModelMap model) {
user.setName("ABC");
model.addAttribute("userName",user.getName());
return "output";
}
}
output.jsp
<h3>Name: ${userName}</h3>