2017-05-29 102 views
1

我是新来的春天,因为我面临一些问题,当登录凭证成功时如何重定向到注册页面否则返回用户不存在的消息。我使用的弹簧安置和客户端是ajax.here是我的示例代码春天mvc宁静登录

@RequestMapping(value="/login",method=RequestMethod.POST, 
           consumes=MediaType.APPLICATION_JSON_VALUE, 
           produces=MediaType.APPLICATION_JSON_VALUE) 
public ResponseEntity<Login> login(@RequestBody Login login,UriComponentsBuilder ucBuilder) 
{ 
      System.out.println("Checking Login Credentials..."); 

      boolean isValid=loginServiceBo.checkUserLogin(login); 

      if(isValid) 
      { 
       System.out.println("Found..."); 
       return new ResponseEntity<Login>(login, HttpStatus.ACCEPTED); 

      } 
      System.out.println("Not Found..."); 
      return new ResponseEntity<>(login, HttpStatus.NOT_FOUND); 
} 

回答

1

当你正在使用Spring REST服务,所以你需要发送状态代码到客户端,并根据这些状态代码,你可以在重定向客户端。

if(isValid) 
      { 
       System.out.println("Found..."); 
       return new ResponseEntity<Login>(login, HttpStatus.OK); 

      } 
      System.out.println("Not Found..."); 
      return new ResponseEntity<>(login, HttpStatus.NOT_FOUND); 

阿贾克斯

$("button").click(function(){ 
    $.post("/login",{username: "xxxxx",password: "xxxxxx"}, 
    function(response){ 
     //redirect to registration page 
    }, function(error) { 
     //show user does not exist message 
    }); 
}); 
+0

谢谢...... –

+0

欢迎你,我upvoting你 –