2014-04-18 33 views
1

我的Spring MVC應用程序中有一個非常奇怪的問題。我寫一個登錄表單,並通過AJAX的影像資料發佈在Spring MVC控制器,它看起來像這樣:Spring MVC和POST重定向發送URL上的目標文件

@Controller 
public class LoginResourceController { 
    private static final Logger log = Logger.getLogger (LoginResourceController.class.getName()); 

    @RequestMapping (value="/login", method = RequestMethod.POST) 
    public String checkAccount (HttpServletRequest httpRequest, HttpServletResponse httpResponse, 
      @RequestHeader (value = "User-Agent") String retrievedUserAgent, 
      @RequestParam("username") String username, 
      @RequestParam("password") String password, 
      @RequestParam("rememberMe") String rememberMe) 
    { 
     //Check username and password in DB, and then if OK, 
     return "redirect:/login/redirectToMain"; 
    } 

@RequestMapping (value = "/login/redirectToMainpage", method = RequestMethod.GET) 
public String redirectControllerToMainPage (HttpServletRequest httpRequest, HttpServletResponse httpResponse) 
{ 
return "mainPage"; 
} 

現在的問題是,我有客戶端(瀏覽器)在重定向請求包含一個URL 整個內容mainPage.jsp在URL中。所以它看起來像:

https://localhost:8443/<!DOCTYPE html><html><head><meta charset=utf-8 /><title>Page that the subscriber sees after login</title>....

我感到相當此錯誤混淆。這是我需要更改的WEB-INF/web.xml還是mvc-dispatcher-servlet.xml中的一些servlet設置?我正在使用Spring 3.0.5。

順便說一下,我的重定向工作完美地爲GET方法控制器在相同的Spring MVC應用程序。 (例如,當我重新加載我的應用程序的主頁面時,重定向到上面登錄的mainPage.jsp完美地工作)。此外,在其他的JSP等GET方法正常工作過(例如,通過的https://localhost:8443/一個GET通過login.jsp重定向到/login

我檢查了以下,他們沒有幫助:。12

+0

也使用Spring 4.0.3證實了同樣的無法解釋的行爲。 – Sonny

回答

0

這對我來說有點棘手,弄清楚,並且作爲一個網絡開發noob在這裏沒有幫助。無論如何,@ jhadesdev的回答上面提到了這個問題。

在我的客戶,我這樣做:

$("#loginForm").submit(function(evt) 
{ 
    evt.preventDefault(); 

    if (loginFormInputIsValid()) 
    { 
     $.ajax ({ 
       type: "POST", 
       url: "/login", 
       data: $(this).serialize(), 
       success: function (response) 
       { 
        window.location = response; 
       } 
     }); 
    } 
} 

這是問題 - 你看,設置window.location=response;造成客戶端(瀏覽器)請求上述時髦的URL服務器。我必須改變我的客戶電話(這是@ jhadesdev的回覆幫助),以確保我不會做錯那麼重要的事情。

感謝您的時間,@ jhadesdev。

+1

得到了這個工作。我沒有改變我的AJAX呼叫。相反,我改變了我的控制器實現,像'http:// blog'中的文章一樣返回'@ ResponseBody'。brunoraljic.​​com/ajax-with-spring-mvc /',現在返回一個String URL,客戶端應該執行一個'GET'。我確實需要在我的'mvc-dispatcher-servlet.xml'文件中爲瀏覽器請求的URL映射到web應用程序中的某個內部文件,比如說,到'WEB-INF/jsp'中的某個目錄中(見'HTTP:// www.mkyong.com /彈簧MVC /彈簧MVC-如何對包括JS-或-CSS-文件-IN-A-JSP頁/')。現在,一切都按預期工作。 – Sonny

1

嘗試不要把重定向放在控制器的返回中,這看起來要麼是整個頁面被渲染爲ajax響應,要麼是一個重定向頭部被一個帶有頁面全部內容的url填充爲字符串應對機構

作爲第一種方法,嘗試使請求一個正常的HTTP請求而不是ajax,它應該可以正常工作。

另外嘗試使返回主體爲空,並返回一個HTTP狀態碼給客戶端。無論200 OK如果該帳戶是確定或401 Unauthorized

@RequestMapping (value="/login", method = RequestMethod.POST) 
    public ResponseEntity checkAccount (HttpServletRequest httpRequest, HttpServletResponse httpResponse, 
      @RequestHeader (value = "User-Agent") String retrievedUserAgent, 
      @RequestParam("username") String username, 
      @RequestParam("password") String password, 
      @RequestParam("rememberMe") String rememberMe) 
    { 
     //Check username and password in DB 
     .... 
     HttpStatus returnCode = null; 

     if(usernameAndPasswordOK) { 
      returnCode = HttpStatus.OK; 
     } 
     else { 
      returnCode = HttpStatus.UNAUTHORIZED; 
     }  

     return new ResponseEntity(returnCode); 
    } 

然後在客戶端上使用JavaScript重定向相應。

+0

感謝您的評論,@ jhadesdev。事實證明,我的jquery電話是不正確的。我會加註你的答案,因爲你的回答指出我在答案中顯示的錯誤,但問題是我在Ajax響應中做了一個'window.location',這顯然是問題。 – Sonny