Struts-2-struts-redirect-action

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

Struts 2-リダイレクトアクション

*redirect* 結果タイプは、標準の_response.sendRedirect()_メソッドを呼び出して、ブラウザーに特定の場所への新しいリクエストを作成させます。

場所は、<result …​>要素の本体で、または<param name = "location">要素として提供できます。 リダイレクトは、 parse パラメーターもサポートしています。 これは、XMLを使用して構成された例です-

<action name = "hello"
   class = "com.finddevguides.struts2.HelloWorldAction"
   method = "execute">
   <result name = "success" type = "redirect">
       <param name = "location">
        /NewWorld.jsp
      </param >
   </result>
</action>

したがって、 WebContent/WEB-INF/classes/struts.xml ファイルを変更して、上記のリダイレクトタイプを定義します-

<?xml version = "1.0" Encoding = "UTF-8"?>
<!DOCTYPE struts PUBLIC
    "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
    "http://struts.apache.org/dtds/struts-2.0.dtd">

<struts>
   <constant name = "struts.devMode" value = "true"/>
   <package name = "helloworld" extends = "struts-default">

      <action name = "hello"
         class = "com.finddevguides.struts2.HelloWorldAction"
         method = "execute">
         <result name = "success" type = "redirect">
            <param name = "location">
            /NewWorld.jsp
            </param >
        </result>
      </action>

      <action name = "index">
         <result >/index.jsp</result>
      </action>

   </package>
</struts>
*NewWorld.jsp* は、アクションが「成功」を再実行するたびにリダイレクトされる新しいページです。 変更せずに *WebContent/WEB-INF/lib/web.xml* を維持してみましょう。その内容は次のようになります-
<?xml version = "1.0" Encoding = "UTF-8"?>
<web-app xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance"
   xmlns = "http://java.sun.com/xml/ns/javaee"
   xmlns:web = "http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
   xsi:schemaLocation = "http://java.sun.com/xml/ns/javaee
   http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
   id = "WebApp_ID" version = "3.0">

   <display-name>Struts 2</display-name>

   <welcome-file-list>
      <welcome-file>index.jsp</welcome-file>
   </welcome-file-list>

   <filter>
      <filter-name>struts2</filter-name>
      <filter-class>
         org.apache.struts2.dispatcher.FilterDispatcher
      </filter-class>
   </filter>

   <filter-mapping>
      <filter-name>struts2</filter-name>
      <url-pattern>/*</url-pattern>
   </filter-mapping>

</web-app>

次のようにアクションクラスファイル Java Resources/src/HelloWorldAction.java を作成します-

package com.finddevguides.struts2;

import com.opensymphony.xwork2.ActionSupport;

public class HelloWorldAction extends ActionSupport{
   private String name;

   public String execute() throws Exception {
      return "success";
   }

   public String getName() {
      return name;
   }

   public void setName(String name) {
      this.name = name;
   }
}

次のコンテンツでメインページ WebContent/WEB-INF/index.jsp を作成しましょう-

<%@ page language = "java" contentType = "text/html; charset = ISO-8859-1"
   pageEncoding = "ISO-8859-1"%>
<%@ taglib prefix = "s" uri = "/struts-tags"%>
   <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
   "http://www.w3.org/TR/html4/loose.dtd">

<html>
   <head>
      <title>Hello World</title>
   </head>
   <body>
      <h1>Hello World From Struts2</h1>
      <form action = "hello">
         <label for = "name">Please enter your name</label><br/>
         <input type = "text" name = "name"/>
         <input type = "submit" value = "Say Hello"/>
      </form>
   </body>
</html>

アクションが「成功」を返す場合にリクエストがリダイレクトされる WebContent/WEB-INF/NewWorld.jsp を作成しましょう-

<%@ page language = "java" contentType = "text/html; charset = ISO-8859-1"
   pageEncoding = "ISO-8859-1"%>
<%@ taglib prefix = "s" uri = "/struts-tags"%>
   <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
   "http://www.w3.org/TR/html4/loose.dtd">

<html>
   <head>
      <title>Redirected Page</title>
   </head>
   <body>
      <h1>New Page after redirection</h1>
   </body>
</html>

次に、プロジェクト名を右クリックし、[エクスポート]> [WARファイル] *をクリックして、WARファイルを作成します。 次に、このWARをTomcatのwebappsディレクトリにデプロイします。 最後に、Tomcatサーバーを起動し、URL http://localhost:8080/HelloWorldStruts2/index.actionにアクセスしてみます。 これにより、次の画面が表示されます-

ページリダイレクトの例

テキストボックスに値を入力し、ページを送信します。 リダイレクト後に次のページが表示されます-

リダイレクトされたページ