在我的Spring MVC项目中,我创建了一个注册页面。在此页面中,有一个用户可以在其中插入其信息(姓名,姓氏等)的表单。我已经使用Spring标记表单将对象“ cliente”绑定到了形式。在我的控制器中,我有:
@RequestMapping(value="/registration",method=RequestMethod.GET)
public String viewRegistration(ModelMap model){
model.addAttribute("cliente",clienteFactory.createCliente());
return "registrazione";
}//registrazione
在registration.jsp中
<form:form method="post" action="add" modelAttribute="cliente">
....
</form:form>
在这个项目中,我没有使用过Spring Security,因为我是一名学生,而且还没有学到这一部分。如果用户没有注册就离开页面,我想从模型中删除对象“ Cliente”。如何解决?谢谢
最初,您不需要从模型中删除对象,就好像客户端通过映射/注册离开页面时,模型引用会被其他映射方法的模型覆盖。
其次,在更多情况下,在GET请求方法中调用方法clienteFactory.createCliente()并不是一个好主意。最好在用户填写所有表单字段并发布请求后在POST中调用它,而不是知道需要调用clientFactory。也可以使用@ModelAttribute批注作为方法参数。
如表格所示form:form method="post"
,没有这种方法将无法正常工作
@RequestMapping(value="/registration",method=RequestMethod.POST)
public String makeRegistration(ModelMap model){
.....
另请参阅简单的Sring教程以处理表单。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句