Spring 立柱弹簧传递模型
我对春天还很了解。我在努力学习。请帮我一点忙 我有一个使用Spring 立柱弹簧传递模型,spring,spring-mvc,post-redirect-get,Spring,Spring Mvc,Post Redirect Get,我对春天还很了解。我在努力学习。请帮我一点忙 我有一个使用modeldattribute=“projectBean”的表单,它工作得非常好,我能够在下面的控制器上操作数据 @RequestMapping( value = "projects/newProject", method = RequestMethod.POST ) public String newProject( @ModelAttribute( "projectBean" ) ProjectBean projectBean, Ht
modeldattribute=“projectBean”
的表单,它工作得非常好,我能够在下面的控制器上操作数据
@RequestMapping( value = "projects/newProject", method = RequestMethod.POST )
public String newProject( @ModelAttribute( "projectBean" )
ProjectBean projectBean, HttpServletRequest request, ModelMap model )
{
model.addAttribute( "projectBean", projectBean );
return "redirect:../projects/projectItems.do";
}
我已经把它保存到数据库中了,所以现在我想把projectBean
传递给另一个控制器
@RequestMapping( value = "/projects/projectItems", method = RequestMethod.GET )
public String projectItems( @RequestParam( defaultValue = "" )
String message, @RequestParam( defaultValue = "" )
String messageType, @RequestParam( defaultValue = "" )
String projectID, HttpServletRequest request, @RequestParam( "projectBean" )
ProjectBean projectBean, ModelMap model )
{
return "project/items";
}
但我有一个例外:Required ProjectBean参数'ProjectBean'不存在
我做错了什么?通常不会将模型从一个控制器传递到另一个控制器。我假设您在将模型传递到另一个JSP页面(本例中为
project/items
)之前尝试执行一些逻辑
您可以在newProject()
controller中实现相同的功能,而无需尝试将模型传递给另一个控制器
@RequestMapping( value = "projects/newProject", method = RequestMethod.POST )
public String newProject( @ModelAttribute( "projectBean" )
ProjectBean projectBean, HttpServletRequest request, RedirectAttributes redirectAttributes)
{
//Call DAO class to save the model to database
//Call BusinessDelegate class to perform the additional logic
//Add beans to RedirectAttributes using addFlashAttribute() methods to make it available in next JSP page
redirectAttributes.addFlashAttribute("projectBean", projectBean);
return "redirect:/project/items";
}
注意:为了应用POST-REDIRECT-GET模式,应该使用
RedirectAttributes
而不是ModelMap
使模型属性在重定向的JSP页面中可用 为什么要将其传递给另一个控制器?您正在尝试将其传递到另一个JSP页面吗?请在projectItems中使用@ModelAttribute(“projectBean”)projectBean projectBean。。为什么@RequestParam(?@pramod.nikam.dev为什么@modeldattribute
有效?如果你刷新页面,如果我这样做,它会再次将数据插入数据库。我错过了这一点。谢谢。我用相关代码更新了答案,应该对你有用。请验证。对不起,你能再给我一点指导吗?我真的对Spring不感兴趣。我尝试了flashAt贡
但我得到了这个错误。参数[重定向属性]属于Model或Map类型,但不可从实际模型分配。您可能需要切换较新的MVC基础结构类才能使用此参数。
在使用此方法之前,是否需要配置某些内容?我缺少的是此MVC:annotation driven
我刚刚将其添加到我的webapp servlet.xml
中,并且它可以正常工作