新建WEB工程pageRedirect,并引入springMVC相关jar包:
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/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0"> <display-name>pageRedirect</display-name> <servlet> <servlet-name>pageRedirect</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>pageRedirect</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> </web-app>
springMVC配置文件,pageRedirect-servlet.xml:
<?xml version="1.0" encoding="UTF-8"?> <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" xmlns:mvc="http://www.springframework.org/schema/mvc" 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 http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd"> <context:component-scan base-package="com.young" /> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/WEB-INF/jsp/" /> <property name="suffix" value=".jsp" /> </bean> </beans>
springMVC控制类,webController.java
package com.young.saticpage.controller; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; @Controller public class WebController { @RequestMapping(value = "/index", method = RequestMethod.GET) public String index() { return "index"; } @RequestMapping(value = "/redirect", method = RequestMethod.GET) public String redirect() { return "redirect:finalPage"; } @RequestMapping(value = "/finalPage", method = RequestMethod.GET) public String finalPage() { return "final"; } }
index.jsp和final.jsp如下:
<%@ page contentType="text/html; charset=UTF-8"%> <%@taglib uri="http://www.springframework.org/tags/form" prefix="form"%> <html> <head> <title>Spring Landing Page</title> </head> <body> <h2>SpringMVC 页面重定向</h2> <p>点击下面的按钮重定向到另外一个新的jsp</p> <form:form method="GET" action="/pageRedirect/redirect"> <table> <tr> <td><input type="submit" value="页面重定向" /></td> </tr> </table> </form:form> </body> </html>
<%@ page contentType="text/html; charset=UTF-8"%> <%@taglib uri="http://www.springframework.org/tags/form" prefix="form"%> <html> <head> <title>Spring Landing Page</title> </head> <body> <h2>页面重定向。。。。。。</h2> </body> </html>
启动tomcat,并访问