我们将我们的mvc代码迁移到Spring4,以前我们有一个方法formBackingObject,我们把它转换成initForm方法。但问题是,在以前扩展SimpleFormController的控制器中,formBackingObject甚至在提交方法之前就被调用了。我们现在已经删除了SimpleFormController。但是initForm在页面加载时只被调用一次。它在提交之前不会被调用。还有一些创建用户对象和添加到UserProfileForm对象的自定义逻辑。
你有没有遇到过类似的问题。
旧码
protected Object formBackingObject(HttpServletRequest request) throws Exception {
final UserProfileForm userProfileForm = new UserProfileForm();
final String id = request.getParameter("id");
if (id != null && !id.trim().equals("")) {
final User user = authenticationServices.findUser(ServletRequestUtils.getLongParameter(request, "id"));
userProfileForm.setUser(user);
} else {
final User user = new User();
userProfileForm.setUser(user);
}
return userProfileForm;
}新码
@RequestMapping(method = RequestMethod.GET)
public String initForm(HttpServletRequest request, ModelMap model) throws Exception{
final UserProfileForm userProfileForm = new UserProfileForm();
final String id = request.getParameter("id");
if (id != null && !id.trim().equals("")) {
final User user = authenticationServices.findUser(ServletRequestUtils.getLongParameter(request, "id"));
userProfileForm.setUser(user);
} else {
final User user = new User();
userProfileForm.setUser(user);
}
addToModel(request, model);
model.addAttribute("userProfileForm", userProfileForm);
return "user-management/user-profile";
}发布于 2014-06-03 14:26:24
创建一个带有@ModelAttribute注释的方法来填充模型。
@ModelAttribute("userProfileForm");
public UserProfileForm formBackingObject(@RequestParam(value="id", required=false) Long id) throws Exception{
final UserProfileForm userProfileForm = new UserProfileForm();
if (id != null) {
final User user = authenticationServices.findUser(id);
userProfileForm.setUser(user);
} else {
final User user = new User();
userProfileForm.setUser(user);
}
return userProfileForm;
}
@RequestMapping(method = RequestMethod.GET)
public String initForm() {
return "user-management/user-profile";
}这样,您也可以使用@RequestParam注释,而不是自己提取参数。
有关该主题的更多信息,请参见参考指南。
发布于 2014-09-19 10:00:38
某些模块间依赖关系现在在曾经需要它们的Maven POM级别上是可选的。例如,spring及其对spring上下文的依赖。这可能会给依赖传递依赖管理的用户带来ClassNotFoundErrors或其他类似的问题,从而导致受影响的下游spring-*。要解决这个问题,只需在构建配置中添加适当的缺少的jars即可。
https://stackoverflow.com/questions/24017512
复制相似问题