【发布时间】:2018-07-09 05:34:04
【问题描述】:
我在单击按钮时将网页重定向到另一个网页。但是重定向的页面没有打开,我收到No mapping found for HTTP request with URI ['UserTest/redirect/npage'] 的错误。但是,此路径是有效的,并且是在我的项目中定义的。 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://java.sun.com/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"
});
}
【问题讨论】:
-
具有
finalPage方法的控制器的请求映射是什么? -
尝试
return "redirect:/npage";,从错误消息['UserTest/redirect/npage']看来是试图点击错误的URL。 -
你没有'/UserTest/redirect'的映射,你有'/redirect'的映射。更改您的映射或 ajax 调用。
标签: java ajax jsp spring-mvc