• Spring MVC页面重定向实例


    以下内容引用自http://wiki.jikexueyuan.com/project/spring/mvc-framework/spring-page-redirection-example.html

    例子:

    pom.xml:

    <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
        <modelVersion>4.0.0</modelVersion>
        <groupId>com.jsoft.testspring</groupId>
        <artifactId>testmvcredirect</artifactId>
        <packaging>war</packaging>
        <version>0.0.1-SNAPSHOT</version>
        <name>testmvcredirect Maven Webapp</name>
        <url>http://maven.apache.org</url>
        <dependencies>
            <dependency>
                <groupId>junit</groupId>
                <artifactId>junit</artifactId>
                <version>3.8.1</version>
                <scope>test</scope>
            </dependency>
            
            <!-- Servlet Library -->
            <!-- http://mvnrepository.com/artifact/javax.servlet/javax.servlet-api -->
            <dependency>
                <groupId>javax.servlet</groupId>
                <artifactId>javax.servlet-api</artifactId>
                <version>3.1.0</version>
                <scope>provided</scope>
            </dependency>
    
            <!-- Spring Core -->
            <!-- http://mvnrepository.com/artifact/org.springframework/spring-core -->
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-core</artifactId>
                <version>4.1.4.RELEASE</version>
            </dependency>
    
            <!-- Spring Web -->
            <!-- http://mvnrepository.com/artifact/org.springframework/spring-web -->
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-web</artifactId>
                <version>4.1.4.RELEASE</version>
            </dependency>
    
            <!-- Spring Web MVC -->
            <!-- http://mvnrepository.com/artifact/org.springframework/spring-webmvc -->
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-webmvc</artifactId>
                <version>4.1.4.RELEASE</version>
            </dependency>
            
        </dependencies>
        <build>
            <finalName>testmvcredirect</finalName>
            <plugins>
                <!-- Config: Maven Tomcat Plugin -->
                <!-- http://mvnrepository.com/artifact/org.apache.tomcat.maven/tomcat7-maven-plugin -->
                <!-- http://tomcat.apache.org/maven-plugin-2.0/tomcat7-maven-plugin/plugin-info.html -->
                <plugin>
                    <groupId>org.apache.tomcat.maven</groupId>
                    <artifactId>tomcat7-maven-plugin</artifactId>
                    <version>2.2</version>
                    <!-- Config: contextPath and Port (Default:8080) -->
                    <!-- 
                    <configuration> 
                        <path>/</path> 
                        <port>8899</port> 
                    </configuration>
                     -->
                </plugin>
                <!-- Config: Maven Jetty Plugin -->
                <!-- http://mvnrepository.com/artifact/org.mortbay.jetty/jetty-maven-plugin -->
                <!-- http://www.eclipse.org/jetty/documentation/current/jetty-maven-plugin.html -->
                <plugin>
                    <groupId>org.eclipse.jetty</groupId>
                    <artifactId>jetty-maven-plugin</artifactId>
                    <version>9.4.3.v20170317</version>
                    <!-- Config: contextPath and Port (Default:8080) -->
                    <!--             
                    <configuration>
                        <httpConnector>
                            <port>8899</port>
                        </httpConnector>
                        <webAppConfig>
                            <contextPath>/</contextPath>
                        </webAppConfig>
                    </configuration>
                     -->
                </plugin>
            </plugins>
        </build>
    </project>

    web.xml:

    <web-app id="WebApp_ID" version="3.0"
             xmlns="http://java.sun.com/xml/ns/javaee"
             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
                xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
                                     http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
        
       <display-name>Spring MVC Application</display-name>
        
       <servlet>
           <servlet-name>spring-mvc</servlet-name>
           <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
           <!-- 默认:[servlet-name]-servlet.xml -->
           <!-- 通过初始化参数,指定xml文件的位置 -->
           <init-param>
               <param-name>contextConfigLocation</param-name>
               <param-value>/WEB-INF/springmvc-context.xml</param-value>
           </init-param>
           <load-on-startup>1</load-on-startup>
       </servlet>   
        
       <servlet-mapping>
           <servlet-name>spring-mvc</servlet-name>
           <url-pattern>/</url-pattern>
       </servlet-mapping>
        
    </web-app>

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

    WebController.java:

    package com.jsoft.testspring.testmvcredirect;
    
    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 "finalPage";
        }
    }

    说明:主要是使用了redirect关键字。

    index.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">
    <%@taglib uri="http://www.springframework.org/tags/form" prefix="form"%>
    <html>
    <head>
        <title>Spring Page Redirection</title>
    </head>
    <body>
    <h2>Spring Page Redirection</h2>
    <p>Click below button to redirect the result to new page</p>
    <form:form method="GET" action="redirect">
    <table>
        <tr>
        <td>
        <input type="submit" value="Redirect Page"/>
        </td>
        </tr>
    </table>  
    </form:form>
    </body>
    </html>

    finalPage.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>
        <title>Spring Page Redirection</title>
    </head>
    <body>
    
    <h2>Redirected Page</h2>
    
    </body>
    </html>

    测试工程:https://github.com/easonjim/5_java_example/tree/master/springtest/test19/testmvcredirect

  • 相关阅读:
    c#读取文件内容存放到int数组 array.txt
    YxShop_V4.2.0开源免费商城2009新春版发布,整合dz最新版,开源!
    c# 读取xml文件 "分析 EntityName 时出错"的解决方案
    正则表达式口诀
    c#操作xml文件
    在WebClient类中保持Session
    实测SCSI RAID速度
    朋友送我的一套书
    给LFF占了个板凳
    ECC内存也能坏
  • 原文地址:https://www.cnblogs.com/EasonJim/p/6919511.html
Copyright © 2020-2023  润新知