2016-12-01 35 views
0

我想從thymeleaf輸入到我的java類中獲得一個值。如何從沒有Ambigous Handler錯誤的百里香輸入?

從thymeleaf

<h1>Form</h1> 
<form action="#" th:action="@{/index}" th:object="${emails}" method="post"> 
    <p>Enter Emails: <input type="text" th:field="*{email}" /></p> 
    <p><input type="submit" value="Submit" /> <input type="reset" value="Reset" /></p> 
</form> 

簡單的腳本我怎麼能收取郵件到我的java類?

控制器

@Controller 
@RequestMapping(method = RequestMethod.GET) 
public class IndexController { 
@RequestMapping(value = "/index", method = RequestMethod.GET) 
public ModelAndView getdata() throws IOException { 
ModelAndView model = new ModelAndView("index"); 
    model.addObject("emails", new MailModel()); 
     return model; 
} 

@PostMapping("/index") 
    public String emailSubmit(@ModelAttribute MailModel emails) { 
     System.out.println(emails.getEmail()); 

     return "index"; 
    } 

錯誤消息

Ambiguous handler methods mapped for HTTP path 'http://localhost:8080/': {public org.springframework.web.servlet.ModelAndView com.spring.web.controller.IndexController.getdata() throws java.io.IOException, public java.lang.String com.spring.web.controller.IndexController.emailSumbit(com.spring.web.model.MailModel)} 

我的應用程序與Springboot,Java和Thymeleaf創建。我究竟做錯了什麼?是否有可能ModelandView不適用於PostMapping?我也跟着https://spring.io/guides/gs/handling-form-submission/,我得到了這個樣本的工作,但當我試圖按照邏輯和實施到我的項目。它不起作用。

回答

1

在聲明您的控制器之前,您要設置RequestMethod以在任何地方進行GET。在你再次設置它們的方法上,這是非常模糊的。

刪除第2行中的@RequestMapping(method = RequestMethod.GET)。這應該解決上述問題。

+0

哇,傻我。那是一個非常愚蠢的錯誤,讓我感到沮喪。謝謝,問題解決了。 – Jesse

+0

沒問題!但請將問題標記爲「已解決」,以便其他SO用戶知道這個問題是正確答案:) – manniL

+0

是否有任何理由當我單擊提交按鈕時出現錯誤,指出「既無BindingResult也無法使用bean的普通目標對象名稱'電子郵件'可用作請求屬性「?在我的POJO中,我的變量名是「email」。 – Jesse