JSP、Spring MVC 页面重定向
JSP, Spring MVC page redirect
需要您帮助解决以下错误。
我正在尝试使用 Spring MVC 从 index.jsp 重定向到第二个页面。但是得到这个错误。
HTTP Status 404 - /redirect.do
description The requested resource is not available.
我的控制器 class 是,
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>
return "bravo";
不会重定向(我希望当你说重定向时你认为有 HTTP 302)。它将尝试查找相对于您当前路径的视图(如果成功则重现 HTTP 200)。
对于重定向,您应该使用
return "redirect:bravo"
但是首先你的 jsp 文件中有一个错误。
<form method="GET" action="/redirect.do">
应该改为
<form method="GET" action="/redirect">
除了上面的答案之外,您还需要删除 @ResponseBody
注释,以便 Spring 将 return 值识别为 View
的名称,不是应该 return 直接发送给客户的 String
。
需要您帮助解决以下错误。
我正在尝试使用 Spring MVC 从 index.jsp 重定向到第二个页面。但是得到这个错误。
HTTP Status 404 - /redirect.do
description The requested resource is not available.
我的控制器 class 是,
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>
return "bravo";
不会重定向(我希望当你说重定向时你认为有 HTTP 302)。它将尝试查找相对于您当前路径的视图(如果成功则重现 HTTP 200)。
对于重定向,您应该使用
return "redirect:bravo"
但是首先你的 jsp 文件中有一个错误。
<form method="GET" action="/redirect.do">
应该改为
<form method="GET" action="/redirect">
除了上面的答案之外,您还需要删除 @ResponseBody
注释,以便 Spring 将 return 值识别为 View
的名称,不是应该 return 直接发送给客户的 String
。