zoukankan      html  css  js  c++  java
  • Spring MVC页面重定向

    以下示例显示如何编写一个简单的基于Web的重定向应用程序,这个应用程序使用重定向将http请求传输到另一个页面。

    • 基于Spring MVC - Hello World实例章节中代码,创建创建一个名称为 PageRedirection 项目。
    • 在 com.ktao.controller 包下创建一个Java类WebController
    • jsp子文件夹下创建一个视图文件index.jspfinal.jsp
    • 最后一步是创建所有源和配置文件的内容并导出应用程序,如下所述。

    完整的项目代码,如下所示 -

      

    配置文件

    web.xml

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

    dispatcher-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: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/context http://www.springframework.org/schema/context/spring-context.xsd">
    
        <context:component-scan base-package="com.ktao.controller"/>
    
        <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
            <property name="prefix" value="/WEB-INF/jsp/"/>
            <property name="suffix" value=".jsp"/>
        </bean>
    </beans>

    控制器

    WebController.java

    webpackage com.ktao.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

    <%--
      Created by IntelliJ IDEA.
      User: ktao
      Date: 17-12-2
      Time: 下午10:46
      To change this template use File | Settings | File Templates.
    --%>
    <%@ page contentType="text/html;charset=UTF-8" language="java" %>
    <%@taglib uri="http://www.springframework.org/tags/form" prefix="form"%>
    <html>
    <head>
        <title>Spring MVC页面重定向</title>
    </head>
    <body>
    <h2>Spring MVC页面重定向</h2>
    <p>点击下面的按钮将结果重定向到新页面</p>
    <form:form method="GET" action="redirect">
        <table>
            <tr>
                <td><input type="submit" value="页面重定向" /></td>
            </tr>
        </table>
    </form:form>
    </body>
    </html>

    final.jsp

    <%--
      Created by IntelliJ IDEA.
      User: ktao
      Date: 17-12-2
      Time: 下午10:46
      To change this template use File | Settings | File Templates.
    --%>
    <%@ page contentType="text/html;charset=UTF-8" language="java" %>
    <%@taglib uri="http://www.springframework.org/tags/form" prefix="form"%>
    <html>
    <head>
        <title>Spring重定向页面</title>
    </head>
    <body>
    <h2>重定向页面...</h2>
    </body>
    </html>

    运行结果

  • 相关阅读:
    吊打面试官系列:Redis 性能优化的 13 条军规大全
    Laravel 7.6 发布
    Laravel 8 新功能:使用 schema:dump 来加速 Migration 和测试
    php中常用的4种运行方式
    基于 Redis 的订阅与发布
    [ida]使用pycharm编写IDApython
    visual studio 配置使生成的pdb文件中包含所有符号
    D/B位、一致与非一致代码段、向下拓展的实验与总结
    [debug] CE指针扫描扫出来为空
    error LNK2019: 无法解析的外部符号 _main,该符号在函数___tmainCRTStartup 中被引用
  • 原文地址:https://www.cnblogs.com/ktao/p/7956218.html
Copyright © 2011-2022 走看看