2011-06-14 43 views
0

我有非常有趣的問題。我正在爲我的Web應用程序登錄頁面,並通過AJAX發送登錄請求。如果成功,我想轉發用戶到另一個頁面。Spring MVC 3,使用Ajax時轉發不工作

看來他是發生了什麼事。我發送Ajax請求,控制器轉發我需要視圖(我看到登錄調試模式),但我留在同一頁面,因爲我認爲該頁面正在等待AJAX​​響應,並因此轉發不會發生。

我認爲這也是錯誤的方法來解決這個問題,但因爲我對這個不熟悉。我如何登錄並將用戶轉發到下一頁。

謝謝。

這裏是我的代碼:

JS代碼:

Page.authenticate = function() { 
     $.ajax({ 
       url: "/login/authenticate/" + $('#username').val() + "/" + $('#password').val(), 
       type: "GET", 
       success: function(poi){ 
       // alert("nesto"); 
       } 
      }); 

     return true; 
    } 

控制器類:

@Controller 
public class LoginPageController { 

private Logger logger = Logger.getLogger(this.getClass()); 

@Autowired 
private UserManagement userManagement; 

@RequestMapping(value="/login/authenticate/{username}/{password}", method=RequestMethod.GET) 
public String authenticate(@PathVariable("username") String userName, @PathVariable("password") String password, Model model) { 

if (userManagement.authenticateUser(userName, password)) { 
     String forward = "forward:/login/success"; 
     return forward; 
    } else { 
     model.addAttribute("errorMessage", "Invalid Username/Password, please try again!"); 
     return "/"; 
    } 

} 

} 
+1

你可能想看看Spring Security的,這是SpringSource的認證/授權開發了一個成熟的框架。 – abalogh 2011-06-14 10:54:00

+0

謝謝,它看起來像一個很好的框架來保護你的應用程序。但是我仍然需要這個轉發才能工作:) – 2011-06-14 11:06:12

+1

如果你使用Spring Security,你將不需要處理這個問題,因爲它會被自動處理。 – abalogh 2011-06-14 11:24:21

回答

1

如果您使用的是AJAX,則需要在@ResponseBody註釋中作出響應。

@RequestMapping(value="/login/authenticate/{username}/{password}", method=RequestMethod.GET) 
    public String authenticate(@PathVariable("username") String userName, @PathVariable("password") String password, Model model) { 

    if (userManagement.authenticateUser(userName, password)) { 
      String forward = "forward:/login/success"; 
      return forward; 
     } else { 
      String forward = "forward:/login/error?message=Invalid Username/Password, please try again!"; 
      return forward; 
     } 

    } 

    @RequestMapping(value="/login/success", method=RequestMethod.GET) 
     @Responsebody 
     public String handleMySuccessRedirect() { 
     return "Logged In successfully" 
       } 

@RequestMapping(value="/login/error", method=RequestMethod.GET) 
    @Responsebody 
    public String handleMyExceptionOnRedirect(@RequestParamter("message") String message) { 
    return message; 
      } 

更新:

@RequestMapping(value="/login/authenticate/{username}/{password}", method=RequestMethod.GET) 
@ResponseBody 
    public String authenticate(@PathVariable("username") String userName, @PathVariable("password") String password, Model model) { 

    if (userManagement.authenticateUser(userName, password)) { 
      String response = "Logged Successfully"; 
      return response; 
     } else { 
      String response = "Invalid Username/Password, please try again!"; 
      return response; 
     } 

    } 
+0

謝謝你的輸入,但這並不能解決我的問題。由於這轉發在同一個控制器內部。 我需要在其他控制器(其他頁面)上轉發。 我會記住爲AJAX調用使用@ResponseBody註釋。 – 2011-06-14 13:18:17

+0

使用完整的承諾響應,而不是轉發作品。一個小問題是當你做POST時,你將請求轉發給POST請求。 由於您無法更改方法(可能有反射,您可以)在頁面上轉發請求土地作爲POST。不幸的是,我不得不爲POST請求添加處理,並最終得到我想要的。不乾淨的解決方案,但它的開始。 – 2011-06-14 13:56:08

+0

@Dolphin,請參閱我的更新並使用此建議。你根本不需要重定向,BTW id我解決了你的問題,請將此問題標記爲已接受或提高其評分。 – 2011-06-14 14:34:38

0

有一對夫婦的事情,你可以在這裏做:

  1. 不要從您的c中返回視圖ontroller,而不是返回JSON的基礎上,JSON的響應,適當地設置的位置 - window.location = 'home.action' - 這裏是一個example using ext-js

  2. 讓登錄頁面進行完全成熟後,沒有一個AJAX職位。

+0

感謝您的幫助,我正在考慮使用完整的帖子。至於其他的解決方案,那就是使用Spring Security,這對我來說目前是開銷。所有失敗的Spring Security在這裏我來:) – 2011-06-14 13:21:13