需要您的帮助以下错误。
我正在尝试使用Spring MVC从index.jsp重定向到第二个页面。但得到这个错误。
HTTP Status 404 - /redirect.do
description The requested resource is not available.
我的控制器类是
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
@Controller
public class MainInvController {
@RequestMapping(value="/redirect")// , method = RequestMethod.GET
@ResponseBody
public String redirect() {
System.out.println("redirect");
return "bravo";
}
}
控制器只是尝试将用户请求重定向到第二页。
我的web.xml是
<!DOCTYPE web-app PUBLIC
"-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd" >
<web-app>
<display-name>Sample Inventory App</display-name>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath*:META-INF/spring/spring-master.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<servlet>
<servlet-name>invsample</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfiguration</param-name>
<param-value>WEB-INF/invsample-servlet.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>invsample</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
invsample-servlet.xml如下所示。
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.0.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd">
<mvc:annotation-driven/>
<context:component-scan base-package="com.sample">
<context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
</context:component-scan>
<bean id="viewResolver"
class="org.springframework.web.servlet.view.UrlBasedViewResolver">
<property name="viewClass" value="org.springframework.web.servlet.view.JstlView" />
<property name="prefix" value="" />
<property name="suffix" value=".jsp" />
</bean>
</beans>
spring-master.xml作为
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.0.xsd">
<context:property-placeholder location="classpath*:META-INF/spring/*.properties"/>
<!-- Load everything except @Controllers -->
<context:component-scan base-package="com.sample">
<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
</context:component-scan>
</beans>
index.jsp如下所示。
<html>
<body>
<h2>Hello World!</h2>
<h2>Spring Page Redirection</h2>
<p>Click below button to redirect the result to new page</p>
<form method="GET" action="/redirect.do">
<table>
<tr>
<td>
<input type="submit" value="Redirect Page"/>
</td>
</tr>
</table>
</form>
</body>
</html>
3条答案
按热度按时间u4dcyp6a1#
return "bravo";
不会重定向(我希望当你说重定向时,你认为有HTTP 302)。它将尝试查找相对于当前路径的视图(如果成功,则重新生成HTTP 200)。对于重定向,您应该使用
但是首先你的jsp文件中有一个错误。
应改为
w80xi6nr2#
除了上面的答案,您需要删除
@ResponseBody
annotation,以便Spring将返回值识别为View
的名称,而不是应该直接返回给客户端的String
。oxf4rvwz3#
将下面的代码添加到index.jsp中,它将重定向到/redirect,这需要在控制器类中捕获。
JSP:
Spring控制器: