2017-04-20 28 views
0

我使用Spring Boot。 我沒有web.xml和jsp文件如何將數據從HTML傳輸到控制器Spring

我有一個處理數據並將其寫入數據庫的控制器。

@RequestMapping(value = "/registration", method = RequestMethod.POST, consumes = MediaType.APPLICATION_JSON_VALUE) 
@ApiOperation(value = "Registration user", response = Account.class) 
public AccountDto registration(@Valid @RequestBody RegistrationDto registration, HttpServletResponse response) { 
    Account account = new Account(registration.getEmail(), registration.getPassword()); 
    return new AccountDto(account); 
} 

我檢查了控制器與它的工作Swagger的幫助。

而且我有一個用戶輸入數據的HTML頁面。

<body> 
    <h1 th:inline="text">Please register</h1> 

    <form th:action="@{/logout}" method="post"> 
     <div> 
      <label> 
       E-mail:<input type="email" name="email"/> 
       Password:<input type="password" name="password"/> 
      </label> 
     </div> 
     <input type="submit" name="registration" value="Registration"/> 
    </form> 
</body> 

如何從頁數據傳輸到控制器?

感謝您的幫助

+0

您指定'消耗= MediaType.APPLICATION_JSON_VALUE',你必須發送Json數據到你的控制器。 – Zorglube

回答

1

這取決於你使用的是什麼,以便使客戶端(瀏覽器)和服務器 我會使用jQuery和做這樣的事情

var dataObj = new Object(); 
dataObj.email = $("#email").val(); 
dataObj.password = $("#passord").val(); 

$.ajax({ 
url : '/registration', 
dataType : 'json', 
contentType : 'application/json; charset=UTF-8', 
type : 'POST', 
data: JSON.stringify(dataObj),    
beforeSend : function(){ 
    //do something before send (e.g. show a message like: please wait) 
}); 

}, 
complete : function(){ 
    //do something after sent (e.g. remove the message please wait) 
}, 
success : function(data) { 
    //handle the success response 
}, 
error : function(data) { 
    //handle the error response 
} 
}); 

我希望這是有用的使用AJAX調用之間調用

安傑洛

編輯

爲了提交數據,你可以(通過使用JQuery總是)添加一個偵聽以這種方式提交

//This is called after the DOM is fully loaded 
$(function() { 
    $("#mySubmitId").click(function(evt){ 
     //Prevent the default submit 
     evt.preventDefault(); 
     //call the submit funtion 
     submitData(); 
    }); 
}); 

function submitData() 
{ 
    var dataObj = new Object(); 
    dataObj.email = $("#email").val(); 
    dataObj.password = $("#passord").val(); 

    $.ajax({ 
    url : '/registration', 
    dataType : 'json', 
    contentType : 'application/json; charset=UTF-8', 
    type : 'POST', 
    data: JSON.stringify(dataObj),    
    beforeSend : function(){ 
     //do something before send (e.g. show a message like: please wait) 
    }); 

    }, 
    complete : function(){ 
     //do something after sent (e.g. remove the message please wait) 
    }, 
    success : function(data) { 
     //handle the success response 
    }, 
    error : function(data) { 
     //handle the error response 
    } 
    }); 
} 

安傑洛

+0

我理解正確。 調用此函數時,數據將以json格式發送到控制器。 在這裏,您可以向用戶顯示他已註冊的消息? 成功:函數(數據){處理成功響應 }, – Garazd

+0

@Garazd;是的,您可以將偵聽器添加到提交中;請參閱編輯我的答案 –

+0

謝謝你的幫助 – Garazd

0

當您提交表單時,表單數據被收集。它獲得所有輸入控件 - 輸入,textareas,選擇,複選框等,併發送所有輸入的內容。

在你的情況下,當你按下提交按鈕的數據必須發送。您的表單的行爲是/logout,但控制器期望/registration

更改操作。

同時檢查RegistrationDto是否有電子郵件和密碼字段來獲取發送的數據。

而作爲@Zorglube在註釋嘗試提到的只是刪除consumes 而且還應該驗證形式有個屬性:對象=「註冊」

+0

我刪除了消費者並更改了

崩潰時出現錯誤:用於對象選擇的表達式是註冊,這是無效的:只有變量表達式($ {...} )在Spring-enabled環境中的'th:object'屬性中允許使用 – Garazd

相關問題