1/ Some note:
- web.xml: listener, filter, dispatch Sevlet.
- [name]-servlet: bean.
- ModelAndView: Enable return view and model.
- Model: an interface, enable add model with attribute.
- ModelMap: and class , enable add model with attribute.
2/ How to using model and view (*.jsp):
- DTO:
- Controller
- web.xml: listener, filter, dispatch Sevlet.
- [name]-servlet: bean.
- ModelAndView: Enable return view and model.
- Model: an interface, enable add model with attribute.
- ModelMap: and class , enable add model with attribute.
2/ How to using model and view (*.jsp):
- DTO:
private String name;
private String password;
private String salt;
private String username;
private Date createTime;
private Date updateTime;
- Controller
UserDto user = new UserDto();
user.setName("vuphan");
model.addAttribute("user", user);
- View:
<div><h2> show user name: ${user.name}</h2></div>
3/ About Navigation:
- Show view: ex with page 'test.jsp'
@RequestMapping(method = RequestMethod.GET)
public String reloadPage(ModelMap model) {
UserDto user = new UserDto();
user.setName("vuphan");
model.addAttribute("user", user);
model.addAttribute("user1", new UserDto());
return "test";
}
- Redirect to another page:
return "redirect:/homeprofile/courses/teaching";
4/ CommandName is same ModelAttribute (from SpringMVC 4.2):
<form:form class="login-form" method="post" commandName="user1" action="${pageContext}/login/doLogin">
<div class="input">
<form:input class="input-username" type="text" required="true" placeholder="Username" path="username"/>
<span><i class="fa fa-envelope-o"></i></span>
</div>
<div class="input">
<form:input class="input-password" type="password" placeholder="Password" required="true" path="password" />
<span><i class="fa fa-lock"></i></span>
</div>
<button class="btn btn-primary btn-lg btn-login" type="submit">GO</button>
</form:form>
5/ Pass Model to other Controller
Controller 1:
Controller 1:
@RequestMapping(value = "/doLogin")
public String demoPassModelToOtherViewController(Model model, @ModelAttribute("user1") UserDto a, final RedirectAttributes redirectAttributes) {
redirectAttributes.addFlashAttribute("user1", a);
return "redirect:/demoNext/show";
}
}
Controller 2:
@Controller
@RequestMapping("/demoNext")
public class TestNextController {
@RequestMapping(value = "/show", method = RequestMethod.GET)
public String reloadPage(ModelMap model, @ModelAttribute("user1") final Object userDto) {
model.addAttribute("user1", userDto);
return "test_next";
}
}
Nhận xét