2014-12-03 45 views
0

在我的Spring MVC項目中,我創建了一個註冊頁面。在這個頁面中有一個用戶插入他的信息(名字,姓氏等)的表單。我已經使用了Spring標籤形式將對象「客戶」綁定到表單。在我的控制,我有:春季:註冊表從模型中刪除對象

@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」從model.How我能解決這個問題?謝謝

回答

1

起初,你不需要從模型中刪除的對象,就好像客戶端葉帶有映射/註冊模型ref的頁面將被其他映射方法的模型覆蓋。

其次,在更多情況下,在GET請求方法中調用方法clienteFactory.createCliente())並不是個好主意。最好在用戶填寫所有表單字段併發布他的請求之後在POST中調用它,而不是您知道需要調用clientFactory.Also則使用@ModelAttribute註釋作爲方法參數。

當你在你的表格form:form method="post"也不會沒有這種方法工作

@RequestMapping(value="/registration",method=RequestMethod.POST) 
public String makeRegistration(ModelMap model){ 
..... 

另請參閱簡單Sring tutorial for handling forms

+0

如果我更改爲「發佈」,我有:HTTP狀態405 - 請求方法'GET'不支持 – Alex 2014-12-03 12:29:36

+0

當然!最佳做法有2種方法將一個映射爲get和post映射一個。 http://docs.spring.io/spring/docs/current/spring-framework-reference/html/mvc.html#mvc-ann-requestmapping – njjnex 2014-12-03 12:46:00

+0

因爲在jsp中我打電話給'Registration' – Alex 2014-12-03 12:52:46