spring mvc: 页面重定向调整
我的项目名称是hello,
在src/main/java目录下面建了一个chapter2目录
有三个配置文件: web.xml, chapter2-servlet.xml, applicationContext.xml
三个配置如下:
web.xml
<web-app> <display-name>Archetype Created Web Application</display-name> <!--配置文件路径--> <context-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/applicationContext.xml</param-value> </context-param> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <servlet> <servlet-name>chapter2</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>chapter2</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> </web-app>
applicationContext.xml
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <!-- ViewResolver --> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/> <property name="prefix" value="/WEB-INF/jsp/"/> <property name="suffix" value=".jsp"/> </bean> </beans>
chapter2-servlet.xml
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <!-- 默认的注解映射的支持 --> <mvc:annotation-driven /> <!-- 静态资源 --> <mvc:resources mapping="/pages/**" location="/pages/"/> <!-- 自动扫描的包名 --> <context:component-scan base-package="chapter2.*" /> </beans>
接下来看代码实现:
WeboneController.java
import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; @Controller public class WeboneController { @RequestMapping(value="/webone/index", method=RequestMethod.GET) public String index() { return "webone_index"; } //重定向跳转 @RequestMapping(value="/webone/redirect", method=RequestMethod.GET) public String redirect() { return "redirect:finalPage"; } @RequestMapping(value="/webone/finalPage", method=RequestMethod.GET) public String finalPage() { return "webone_final"; } }
jsp页面
webone_index.jsp
<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%> <%@ taglib uri="http://www.springframework.org/tags/form" prefix="form" %> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <title>webone index</title> </head> <body> <form:form method="GET" action="/hello/webone/redirect"> <table> <tr> <td><input type="submit" value="重定向跳转"/> </td> </tr> </table> </form:form> </body> </html>
webone_fina.jsp
<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%> <%@ taglib uri="http://www.springframework.org/tags/form" prefix="form" %> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <title>web-one finalPage spring mvc重定向页面<</title> </head> <body> <h2>spring mvc重定向页面</h2> </body> </html>
实际我的 访问地址如下: http://localhost:8080/hello/webone/index
点击里面的跳转按钮,会自动跳转到:http://localhost:8080/hello/webone/finalPage