You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Spring MVC中AJAX GET请求重定向页面无法显示的问题排查

问题描述

我点击按钮尝试重定向到另一页面时,页面无法打开,还报错:No mapping found for HTTP request with URI ['UserTest/redirect/npage']。明明项目里已经定义了对应路径,view文件夹下也存在npage.jsp,试了网上同类问题的解法都没用,求帮忙排查!


相关配置与代码

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:web="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.4">
<display-name>User Test</display-name>
<servlet>
<servlet-name>UserTest</servlet-name>
<servlet-class> org.springframework.web.servlet.DispatcherServlet </servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>UserTest</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>

UserTest-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.test" />
<bean class = "org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name = "prefix" value = "/WEB-INF/view/" />
<property name = "suffix" value = ".jsp" />
</bean>
</beans>

Java控制器代码

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

AJAX代码

function nopg() {
$.ajax({
type : "GET",
url : "/UserTest/redirect"
});
}

问题排查与解决思路

我帮你梳理几个关键问题点,你可以逐一验证:

1. AJAX请求的本质误区

你现在用AJAX发起重定向请求,但AJAX是异步HTTP请求,它只会接收后台响应,不会自动触发浏览器的页面跳转。这是当前问题最核心的根源——后台的重定向指令对AJAX来说只是个普通响应,不会帮你跳页。

解决办法:

  • 最简单的方式:放弃AJAX,直接用按钮触发页面跳转:
    <button onclick="window.location.href='/UserTest/redirect'">跳转至新页面</button>
    
  • 若一定要保留AJAX,需在回调中手动触发跳转:
    function nopg() {
      $.ajax({
        type : "GET",
        url : "/UserTest/redirect",
        success: function() {
          window.location.href = "/UserTest/npage"; // 直接跳转到目标页面路由
        }
      });
    }
    

2. 重定向路径的写法错误

你后台的return "redirect:npage";是相对当前请求的路径,当前请求是/UserTest/redirect,所以Spring MVC会把它解析为/UserTest/redirect/npage——这就是报错的原因,你根本没有定义这个路径的映射!

解决办法:
把重定向路径改成相对于项目根路径的写法(加前缀斜杠):

@RequestMapping(value = "/redirect", method = RequestMethod.GET)
public String redirect() {
  return "redirect:/npage"; // 加斜杠后,路径会被解析为项目根下的/npage
}

这样Spring MVC就会正确重定向到/UserTest/npage,和你定义的/npage路由匹配。

3. 额外检查点

  • 确认你的控制器类上是否加了@Controller注解,否则Spring无法识别该类的路由映射;
  • 核对npage.jsp是否确实在/WEB-INF/view/目录下,和InternalResourceViewResolver配置的前缀、后缀完全匹配。

内容的提问来源于stack exchange,提问作者Agha

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.15 06:33:59