1. 程式人生 > >Spring MVC頁面重定向

Spring MVC頁面重定向

quest this head 新頁面 ase frame png 按鈕 resource

以下示例顯示如何編寫一個簡單的基於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>

運行結果

技術分享圖片

技術分享圖片

Spring MVC頁面重定向