LoginSignup
7
8

More than 3 years have passed since last update.

Struts2でJava8を使う場合

Last updated at Posted at 2015-05-26

Struts2+Java8を使うには

Struts2.5.3以前の中でJava8を使う場合は、Struts2-Java8-supportプラグインを導入します。これによりActionクラスの中でJava8の機能が使えます。
( Struts2.5.5では不要です )

特に有用なのは、Actionクラスにて画面へレスポンスする前のList型やMap型フィールドにStream APIを使ってフィルタリングするケースでしょう。

他に留意するポイント

もしStruts2-SpringプラグインやAspectJを導入していた場合、古いバージョンのasm|OW2Consortiumを指定している場合があります。(asm-3.3.2.jarなど)
また、Struts2の前進であるwebwork2(xwork2)でもasm3を利用しています。

Struts2-Java8では 依存ライブラリとして asm-5.0系を指定しています。
もしasmがビルドパスやmavenのpom.xmlに含まれていた場合は削除します。

以下にmavenでの削除対象の指定例を示します。

Struts2.5.xの場合

pom.xml
<dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>org.apache.struts</groupId>
            <artifactId>struts2-core</artifactId>
            <exclusions>
                <exclusion>
                    <groupId>asm</groupId>
                    <artifactId>asm</artifactId>
                </exclusion>
                <exclusion>
                    <groupId>asm</groupId>
                    <artifactId>asm-commons</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
    </dependencies>
</dependencyManagement>

Struts2.3.xまでの場合

pom.xml
<dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>org.apache.struts.xwork</groupId>
            <artifactId>xwork-core</artifactId>
            <exclusions>
                <exclusion>
                    <groupId>asm</groupId>
                    <artifactId>asm</artifactId>
                </exclusion>
                <exclusion>
                    <groupId>asm</groupId>
                    <artifactId>asm-commons</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
    </dependencies>
</dependencyManagement>

Actionクラスのフィールドにstream()って使うの?

Actionクラスにロジック乗せてしまい、Actionクラスを肥えさせることになるのでは…の懸念はじゅうぶんあります。

しかしフィルタリングはstream()以降で指定する関数で定義するものなので、Actionクラスの本体はそこまで太ることもないですし、関数を切り替えるだけで出力も自在に変えられるので、レスポンス(View層、ViewModel…?)の実装方法として、ありなのかも知れません。

Struts2-Java8-supportプラグインの入手先

Struts2は全般的にmavenで導入、ないしはgithubからクローンするように移行しておりますが、個別に入手したい場合もありますので、mavenリポジトリのリンクと、githubの該当URLを残しておきます。

mavenリポジトリ:
http://mvnrepository.com/artifact/org.apache.struts/struts2-java8-support-plugin

github:
https://github.com/apache/struts/tree/master/plugins/java8-support

7
8
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
7
8