Springmvc-page-redirection

提供:Dev Guides
移動先:案内検索

Spring MVC-ページリダイレクトの例

次の例は、リダイレクトを使用してhttp要求を別のページに転送する単純なWebベースのアプリケーションを作成する方法を示しています。 まず、作業中のEclipse IDEを用意し、Spring Web Frameworkを使用して動的フォームベースのWebアプリケーションを開発するための次の手順を検討します。

Step Description
1 Create a project with a name HelloWeb under a package com.finddevguides as explained in the Spring MVC - Hello World chapter.
2 Create a Java class WebController under the com.finddevguides package.
3 Create view files index.jsp, final.jsp under jsp sub-folder.
4 The final step is to create the content of the source and configuration files and export the application as explained below.

WebController.java

package com.finddevguides;

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

次に、Springビューファイル index.jsp のコンテンツを示します。 これはランディングページになり、このページはアクセスリダイレクトサービスメソッドにリクエストを送信します。アクセスリダイレクトサービスメソッドはこのリクエストを別のサービスメソッドにリダイレクトし、最終的に* final.jsp *ページが表示されます。

index.jsp

<%@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 = "/HelloWeb/redirect">
         <table>
            <tr>
               <td>
                  <input type = "submit" value = "Redirect Page"/>
               </td>
            </tr>
         </table>
      </form:form>
   </body>
</html>

final.jsp

<%@taglib uri = "http://www.springframework.org/tags/form" prefix = "form"%>
<html>

   <head>
      <title>Spring Page Redirection</title>
   </head>

   <body>
      <h2>Redirected Page</h2>
   </body>

</html>

ソースファイルと構成ファイルの作成が完了したら、アプリケーションをエクスポートします。 アプリケーションを右クリックし、[エクスポート]→[WARファイル]オプションを使用して、HelloWeb.warファイルをTomcatのwebappsフォルダーに保存します。

次に、Tomcatサーバーを起動し、標準ブラウザーを使用してwebappsフォルダーから他のWebページにアクセスできることを確認します。 URL –http://localhost:8080/HelloWeb/indexを試してください。SpringWebアプリケーションで問題がなければ、次の画面が表示されます。

スプリングリダイレクトフォーム

「ページのリダイレクト」ボタンをクリックして、フォームを送信し、リダイレクトされた最終ページに移動します。 Spring Webアプリケーションで問題がなければ、次の画面が表示されるはずです-

スプリングリダイレクトフォームの結果