0

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>
Harry
  • 65
  • 1
  • 2
  • 9
  • You've misunderstood if you were expecting `name` to get a "default" value from somewhere. The duplicate explains things pretty well (the second answer). – Kayaman Jul 04 '20 at 15:53
  • I am not getting that – Harry Jul 06 '20 at 14:54
  • Well, it doesn't really matter since it doesn't do what you thought it does. The answer to your question "How can I..." would be "you can't". – Kayaman Jul 07 '20 at 09:33

0 Answers0