2015-03-25 224 views
0

這是我的形式:請求方法「POST」春不支持MVC

<form action="${pageContext.request.contextPath}/admin/editc" method="POST" id="editForm"> 
    <input type="text" name="username" class="form-control" /> 
    <input type="text" name="password" class="form-control" /> 
    <input type="submit" value="submit" > 
</form> 

這是我的控制器方法:

@RequestMapping(value = "/admin/edit", method = RequestMethod.GET) 
public ModelAndView editPage() { 

    ModelAndView model = new ModelAndView(); 
    model.addObject("title", "User edit Form - Database Interaction"); 
    model.addObject("message", "This page is for ROLE_ADMIN only!"); 
    model.setViewName("editpage"); 
    System.out.println("getting edit page"); 

    return model; 

} 


@RequestMapping(value = "/admin/editc", method = RequestMethod.POST) 
public ModelAndView updateCredentials() { 
//  System.out.println("Username= "+username+" password= "+password); 
    ModelAndView model = new ModelAndView(); 
    model.addObject("title", "Credential Edit Operation"); 
    model.addObject("message", "You are successfully updated your credentials"); 
    model.addObject("edited", "TRUE"); 
    model.setViewName("editpage"); 
    System.out.println("executed updateCredentials POST method"); 

    return model; 

} 

現在的問題是我收到405錯誤控制檯類似如下:

org.springframework.web.servlet.PageNotFound handleHttpRequestMethodNotSupported 
WARNING: Request method 'POST' not supported 

任何人都可以幫我解決這個錯誤嗎?

+0

我在發佈的代碼中看不到任何錯誤。其他事情出錯了。它很難說出它可能出錯的地方。要進一步調試,您的電話工作正常嗎?如果是這樣,當服務器啓動時,它會吐出請求映射的細節。檢查映射是否綁定到控制器。 – minion 2015-03-25 18:49:54

回答

0

您的java方法的簽名是錯誤的,您還需要添加您期望從POST中獲得的內容。看看這個guide來更正你的代碼。

+0

您的意思是@ModelAttribute(...)註釋@daniele。我之前擁有它,當時它不工作 – 2015-03-25 11:57:52

+0

我的意思是@damien答案的相同簽名,並且您應該爲GET添加另一個處理程序。請注意,對於POST處理程序,您需要接受具有 – daniele 2015-03-25 12:11:05

+0

格式字段的類的參數。將model屬性作爲控制器方法的參數不一定是正確的。 – minion 2015-03-25 18:50:42

0

您首先需要一個GET方法來顯示錶單和一個POST來提交它。

@RequestMapping(value = "/admin/editc", method = RequestMethod.GET) 
public ModelAndView updateCredentials() { 
    return new ModelAndView("editPage"); 
} 

@RequestMapping(value = "/admin/editc", method = RequestMethod.POST) 
public ModelAndView updateCredentials(@ModelAttribute UserCredentials credentials) { 
    someService.save(credentials); 
    ... 
} 
+0

嗨@Damien recheck我有Get方法的問題。 – 2015-03-25 11:53:35

+0

但是你的post方法不包含任何屬性值。您的表單將嘗試找到一個處理程序,該處理程序使用模式中的內容,就像我的示例中的UserCredentials對象包含用戶名和密碼字符串字段。 – 2015-03-26 06:22:26

+0

即使我添加了用戶名和密碼的方式,但它不起作用,然後通過刪除這些參數進行測試。 – 2015-03-26 07:23:07

相關問題