重定向页面未显示带有弹簧MVC AJAX请求(redirected page is not bein

2019-09-30 19:08发布

在我的Spring MVC应用程序,我想从我的重定向页面index.jsp页面final.jsp使用AJAX页面。 但是,重定向的页面(final.jsp)当我点击未显示Redirect Page在我的应用程序按钮。 我已经使用System.out.println("inside /redirect");System.out.println("inside /finalPage"); 我的Java代码内部,以便查看是否AJAX GET请求被接收在服务器或没有,只有inside /redirect正在打印其中这意味着/finalPage不被调用。 此外,我的浏览器的控制台不显示任何错误。 所以,请告诉,有什么可以是问题,哪里是问题吗? 我已张贴下面我完整的代码:

WebController.java

package com.tutorialspoint;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

@Controller
public class WebController {

   @RequestMapping(value = "/index", method = RequestMethod.GET)
   public String index() {
       return "index";
   }

   @RequestMapping(value = "/redirect", method = RequestMethod.GET)
   public String redirect() {
      System.out.println("inside /redirect");
      return "redirect:/finalPage";
   }

   @RequestMapping(value = "/finalPage", method = RequestMethod.GET)
   public String finalPage() {
       System.out.println("inside /finalPage");
      return "final";
   }
}

的index.jsp

<%@taglib uri = "http://www.springframework.org/tags/form" prefix = "form"%>
<html>
   <head>
      <title>Spring Page Redirection</title>
      <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
   </head>
   <body>
      <h2>Spring Page Redirection</h2>
      <p>Click below button to redirect the result to new page</p>
      <button onClick="redFun()">Redirect Page</button>
      <script>
            function redFun() {
                $.ajax({
                    type : "GET",
                    url : "/HelloWeb/redirect"
                });
            }
      </script>
   </body>
</html>

final.jsp

<%@taglib uri = "http://www.springframework.org/tags/form" prefix = "form"%>
<html>

   <head>
      <title>Spring Page Redirection</title>
   </head>

   <body>
      <h2>Redirected Page</h2>
   </body>

</html>

web.xml中:

<web-app id = "WebApp_ID" version = "2.4"
   xmlns = "http://java.sun.com/xml/ns/j2ee" 
   xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance"
   xsi:schemaLocation = "http://java.sun.com/xml/ns/j2ee 
   http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">

   <display-name>Spring MVC Application</display-name>

   <servlet>
      <servlet-name>HelloWeb</servlet-name>
      <servlet-class>
         org.springframework.web.servlet.DispatcherServlet
      </servlet-class>
      <load-on-startup>1</load-on-startup>
   </servlet>

   <servlet-mapping>
      <servlet-name>HelloWeb</servlet-name>
      <url-pattern>/</url-pattern>
   </servlet-mapping>

</web-app>

的HelloWeb-servlet.xml中

<beans xmlns = "http://www.springframework.org/schema/beans"
   xmlns:context = "http://www.springframework.org/schema/context"
   xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance"
   xsi:schemaLocation = "
   http://www.springframework.org/schema/beans     
   http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
   http://www.springframework.org/schema/context 
   http://www.springframework.org/schema/context/spring-context-3.0.xsd">

   <context:component-scan base-package = "com.tutorialspoint" />

   <bean class = "org.springframework.web.servlet.view.InternalResourceViewResolver">
      <property name = "prefix" value = "/WEB-INF/jsp/" />
      <property name = "suffix" value = ".jsp" />
   </bean>

</beans>

Answer 1:

因为你正在使用AJAX来打你的控制器的方法重定向你的浏览器不跟随重定向。 你有两个选择:

  1. 在所有不使用AJAX,自然让你的浏览器响应了重定向的状态。

     function redFun() { window.location = "/HelloWeb/redirect"; } 
  2. 寻找一个HTTP 302响应于所述的XmlHttpRequest,并改变了window.location在响应Location头的值:

     function redFun() { $.ajax({ type: "GET", url: "/HelloWeb/redirect" }).fail(function (jqXHR, status, err) { if (jqXHR.status === 302) { window.location = jqXHR.getResponseHeader('Location'); } }); } 


文章来源: redirected page is not being displayed with spring-mvc ajax request