Jquery 如何在SpringMVC中使用AJAX在视图中传递和获取模型属性
我正在使用SpringMVC,我需要对服务器进行异步调用并检查用户的凭据。如果匹配,则我将重定向到另一个页面 MyController.javaJquery 如何在SpringMVC中使用AJAX在视图中传递和获取模型属性,jquery,ajax,spring-mvc,Jquery,Ajax,Spring Mvc,我正在使用SpringMVC,我需要对服务器进行异步调用并检查用户的凭据。如果匹配,则我将重定向到另一个页面 MyController.java @RequestMapping("performingLogin.htm") public ModelAndView performingLogin(@RequestParam String username, @RequestParam String password) { //boolean value which decides
@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";
}
});
}
函数doAjaxPost(){
$.ajax({
网址:“dologin.htm”,
数据:{
用户名:$('#用户名').val(),
密码:$('#password').val()
},
成功:函数(responseTxt、statusTxt、xhr){
/*打开新页面*/
window.location.href=“newpage.htm”;
}
});
}
我需要知道如何在JSP端验证用户,以便发出凭证不正确的警报
检查@ResponseBody注释
public @ResponseBody String performingLogin(@RequestParam String username, @RequestParam String password)
{
}
选中@ResponseBody注释
public @ResponseBody String performingLogin(@RequestParam String username, @RequestParam String password)
{
}
在代码中,您完全忽略返回的视图,并在成功时执行js重定向。只要这样做,返回
ModelAndView
或@ResponseBody
注释值之间就没有真正的区别
您可能希望返回401 Unauthorized
http错误,然后在javascript中检查该错误
有多种方法可以做到这一点
一种是创建一个异常并用spring注释对其进行注释,然后抛出它
大致如下:
@ResponseStatus(value = HttpStatus.UNAUTHORIZED)
public class AuthenticationException extends RuntimeException {
private static final long serialVersionUID = 23949237498237948234l;
}
将请求映射更改为:
@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);
if (authenticationWentWrong) {
throw new AuthenticationException();
}
//Currently returning the new model to be opened irrespective of valid user
ModelAndView model = new ModelAndView("newpage");
return model;
}
然后你的js代码:
<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";
},
statusCode : {
401: function(jqXHR, textStatus, errorThrown) {
//handle the authentication error here
}
}
});
}
函数doAjaxPost(){
$.ajax({
网址:“dologin.htm”,
数据:{
用户名:$('#用户名').val(),
密码:$('#password').val()
},
成功:函数(responseTxt、statusTxt、xhr){
/*打开新页面*/
window.location.href=“newpage.htm”;
},
状态代码:{
401:函数(jqXHR、textStatus、errorshown){
//在这里处理身份验证错误
}
}
});
}
在代码中,您完全忽略返回的视图,并在成功时执行js重定向。只要这样做,返回ModelAndView
或@ResponseBody
注释值之间就没有真正的区别
您可能希望返回401 Unauthorized
http错误,然后在javascript中检查该错误
有多种方法可以做到这一点
一种是创建一个异常并用spring注释对其进行注释,然后抛出它
大致如下:
@ResponseStatus(value = HttpStatus.UNAUTHORIZED)
public class AuthenticationException extends RuntimeException {
private static final long serialVersionUID = 23949237498237948234l;
}
将请求映射更改为:
@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);
if (authenticationWentWrong) {
throw new AuthenticationException();
}
//Currently returning the new model to be opened irrespective of valid user
ModelAndView model = new ModelAndView("newpage");
return model;
}
然后你的js代码:
<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";
},
statusCode : {
401: function(jqXHR, textStatus, errorThrown) {
//handle the authentication error here
}
}
});
}
函数doAjaxPost(){
$.ajax({
网址:“dologin.htm”,
数据:{
用户名:$('#用户名').val(),
密码:$('#password').val()
},
成功:函数(responseTxt、statusTxt、xhr){
/*打开新页面*/
window.location.href=“newpage.htm”;
},
状态代码:{
401:函数(jqXHR、textStatus、errorshown){
//在这里处理身份验证错误
}
}
});
}
此外,您可能想尝试我认为它经常被忽略,但给了您更大的控制权-对于新的测试包可能很有用此外,您可能想尝试我认为它经常被忽略,但给了您更大的控制权-对于新的测试包可能很有用