Struts-2-struts-actions
Struts 2-アクション
- アクション*は、MVC(Model View Controller)フレームワーク用であるため、Struts2フレームワークの中核です。 各URLは特定のアクションにマッピングされ、ユーザーからのリクエストを処理するために必要な処理ロジックを提供します。
しかし、このアクションは他の2つの重要な機能にも役立ちます。 まず、アクションは、JSPであれ他のタイプの結果であれ、要求からビューへのデータの転送において重要な役割を果たします。 第二に、アクションは、リクエストへの応答で返されるビューをレンダリングする結果を決定するフレームワークを支援する必要があります。
アクションを作成
*Struts2* のアクションの唯一の要件は、StringまたはResultオブジェクトのいずれかを返し、POJOでなければならないnoargumentメソッドが1つ存在することです。 引数なしのメソッドが指定されていない場合、デフォルトの動作はexecute()メソッドを使用することです。
オプションで、 Action インターフェイスを含む6つのインターフェイスを実装する ActionSupport クラスを拡張できます。 アクションインターフェイスは次のとおりです-
public interface Action {
public static final String SUCCESS = "success";
public static final String NONE = "none";
public static final String ERROR = "error";
public static final String INPUT = "input";
public static final String LOGIN = "login";
public String execute() throws Exception;
}
Hello Worldの例のアクションメソッドを見てみましょう-
package com.finddevguides.struts2;
public class HelloWorldAction {
private String name;
public String execute() throws Exception {
return "success";
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
アクションメソッドがビューを制御する点を説明するために、 execute メソッドに次の変更を加え、次のようにクラスActionSupportを拡張しましょう-
package com.finddevguides.struts2;
import com.opensymphony.xwork2.ActionSupport;
public class HelloWorldAction extends ActionSupport {
private String name;
public String execute() throws Exception {
if ("SECRET".equals(name)) {
return SUCCESS;
} else {
return ERROR;
}
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
この例では、name属性を調べるためのexecuteメソッドにいくつかのロジックがあります。 属性が文字列 "SECRET" と等しい場合、結果として SUCCESS を返します。それ以外の場合、結果として ERROR を返します。 ActionSupportを拡張しているため、文字列定数 SUCCESS およびERRORを使用できます。 さて、次のように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">/HelloWorld.jsp</result>
<result name = "error">/AccessDenied.jsp</result>
</action>
</package>
</struts>
ビューを作成する
eclipseプロジェクトのWebContentフォルダーに以下のjspファイル HelloWorld.jsp を作成しましょう。 これを行うには、プロジェクトエクスプローラーでWebContentフォルダーを右クリックし、[ New> JSP File ]を選択します。 このファイルは、返される結果がSUCCESSである場合に呼び出されます。SUCCESSは、Actionインターフェイスで定義されている文字列定数「成功」です-
<%@ page contentType = "text/html; charset = UTF-8" %>
<%@ taglib prefix = "s" uri = "/struts-tags" %>
<html>
<head>
<title>Hello World</title>
</head>
<body>
Hello World, <s:property value = "name"/>
</body>
</html>
以下は、アクションの結果が文字列定数「error」に等しいERRORの場合にフレームワークによって呼び出されるファイルです。 以下は AccessDenied.jsp のコンテンツです。
<%@ page contentType = "text/html; charset = UTF-8" %>
<%@ taglib prefix = "s" uri = "/struts-tags" %>
<html>
<head>
<title>Access Denied</title>
</head>
<body>
You are not authorized to view this page.
</body>
</html>
また、WebContentフォルダーに index.jsp を作成する必要があります。 このファイルは、ユーザーがStruts 2フレームワークにHelloWorldActionクラスの* execute *メソッドを呼び出してHelloWorld.jspビューをレンダリングするように指示するためにクリックできる初期アクションURLとして機能します。
<%@ 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>
それだけです。web.xmlファイルに必要な変更はないので、例*の章で作成したweb.xmlと同じものを使用しましょう。 これで、Struts 2フレームワークを使用して *Hello World アプリケーションを実行する準備が整いました。
アプリケーションを実行する
プロジェクト名を右クリックし、[エクスポート]> [WAR ファイル]をクリックして、WARファイルを作成します。 次に、このWARをTomcatのwebappsディレクトリにデプロイします。 最後に、Tomcatサーバーを起動して、URL *http://localhost:8080/HelloWorldStruts2/index.jsp にアクセスしてみます。 これにより、次の画面が表示されます-
「SECRET」と単語を入力してみましょう。次のページが表示されます-
「SECRET」以外の単語を入力すると、次のページが表示されます-
複数のアクションを作成する
あなたは頻繁に異なる要求を処理し、ユーザーに異なるURLを提供するために複数のアクションを定義します。したがって、以下に定義するように異なるクラスを定義します-
package com.finddevguides.struts2;
import com.opensymphony.xwork2.ActionSupport;
class MyAction extends ActionSupport {
public static String GOOD = SUCCESS;
public static String BAD = ERROR;
}
public class HelloWorld extends ActionSupport {
...
public String execute() {
if ("SECRET".equals(name)) return MyAction.GOOD;
return MyAction.BAD;
}
...
}
public class SomeOtherClass extends ActionSupport {
...
public String execute() {
return MyAction.GOOD;
}
...
}
次のように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.HelloWorld"
method = "execute">
<result name = "success">/HelloWorld.jsp</result>
<result name = "error">/AccessDenied.jsp</result>
</action>
<action name = "something"
class = "com.finddevguides.struts2.SomeOtherClass"
method = "execute">
<result name = "success">/Something.jsp</result>
<result name = "error">/AccessDenied.jsp</result>
</action>
</package>
</struts>
上記の仮想的な例でわかるように、アクション結果 SUCCESS と ERROR’s は重複しています。
この問題を回避するには、結果の結果を含むクラスを作成することをお勧めします。