我正在使用Spring MVC,並且需要對服務器進行異步調用並檢查用戶的憑據。如果匹配,那麼我將重定向到另一頁。如何在Spring MVC中使用AJAX在視圖中傳遞和獲取模型屬性
MyController.java
@RequestMapping("performingLogin.htm")
public ModelAndView performingLogin(@RequestParam String username, @RequestParam String password)
{
//boolean value which decides whether its a valid user
myService.performingLogin(username, password);
//Currently returning the new model to be opened irrespective of valid user
ModelAndView model = new ModelAndView("newpage");
return model;
}
MainView.jsp
<script src="http://code.jquery.com/jquery-1.7.1.min.js"></script>
<script type="text/javascript">
function doAjaxPost() {
$.ajax({
url : "dologin.htm",
data : {
username: $('#username').val(),
password: $('#password').val()
},
success : function(responseTxt,statusTxt,xhr) {
/* Opening new page */
window.location.href="newpage.htm";
}
});
}
我需要知道我將如何驗證用戶在JSP結束,這樣我可以給一個警報,提醒憑據不正確。