springMVC学习总结(三) --springMVC重定向

根据springMVC学习总结(一) --springMVC搭建搭建项目

  1. 在com.myl.controller包下创建一个java类WebController。
  2. 在jsp子文件夹下创建一个视图文件index.jsp、final.jsp。
  3. 配置web.xml
  4. 配置springmvc-servler.xml

创建项目结构如下:

WebController.java代码如下

package com.myl.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

/**
 * 
 * @author myl
 * @date      2018年5月20日   上午11:13:39
 */

@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";
    }
}

下面是Spring视图文件index.jsp的内容。用登录页面演示,该页面发送访问重定向方法的请求,将重定向这个请求到另一个服务方法,最后将享受final.jsp页面的内容

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>form演示重定向</title>
</head>
<body>
    <p>spring mvc重定向页面</p>
    <h2>点击下面按钮提交表单,跳转重定向界面</h2>
    <form:form method="GET" action="/springmvc_03/redirect">
        <table>
            <tr>
                <td>
                    <input type="submit" value="提交重定向">
                </td>
            </tr>
        </table>
    </form:form>
</body>
</html>

final.jsp代码如下

<%@ page language="java" contentType="text/html; charset=utf-8"
    pageEncoding="utf-8"%>
<!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>Spring重定向页面</title>
</head>
<body>
    <h2>final重定向页面</h2>
</body>
</html>

web.xml 配置如下

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
     xmlns
="http://xmlns.jcp.org/xml/ns/javaee"
     xsi:schemaLocation
="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
     version
="3.1"> <display-name>springmvc_03</display-name> <servlet> <servlet-name>springmvc</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>springmvc</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> </web-app>

springmvc-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.myl.controller"></context:component-scan>
   
   <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
           <property name="prefix" value="/WEB-INF/jsp/"></property>
           <property name="suffix" value=".jsp"></property>
   </bean>
   
   </beans>

运行该项目

访问http://localhost:8080/springmvc_03/index结果如下

点击  提交重定向按钮结果如下:

   

 到此简单的重定向代码完成

  

原文地址:https://www.cnblogs.com/maoyali/p/9062874.html