Gobble up pudding

プログラミングの記事がメインのブログです。

MENU

SAStrutsの環境構築とチュートリアルのセットアップ


今更ですが、SAStrutsのtutorialの環境構築をします。
SAStrutsとはJavaのWebフレームワークStrutsの1系を改造したものです。

公式のチュートリアルのセットアップのページの手順を踏むと
情報が古いのか不親切なのかたぶんうまくいかないでしょう。
下記ではスムーズにいっていますが、実はいろいろ苦戦しました。
たぶんほかの人も同じようなことではまるんじゃないでしょうか?

  • Eclipseの解凍がうまくいかない(実はこれが一番ハマった...)
  • Tomcatのバージョンの指定がうまくできない(7以上を使いたいのに6までしか表示がないなど)→チュートリアル通りにやった場合そうなる。
  • インポートしたプロジェクトが動的プロジェクトになってない
  • http://localhost:8080/sa-struts-tutorialにアクセスすると404 Not Foundになる

今回の環境構成

OS Windows 10(Windowsなら何でもよい)
Eclipse Pleiades all in One 4.5 Mars (Full Edition)
JRE Java7 (Pleiades付属)
Tomcat 8.0.32 (Pleiades付属)

注意事項としてSAStrutsのセットアップに説明のある
Tomcat LauncherとTomcat本体は別途インストールしません(Eclipseに付属のため)。
あと、プロパティエディタも確かもともとついているはずなので無視します。
※Java8じゃなくてJava7にしているのはJava8だと追加の設定が必要なのと一部機能が使えなくなってしまうからです(Scaffoldなど)

1.EclipseのPleiades all in oneをダウンロード

https://mergedoc.osdn.jp/
にアクセスして
Eclipse 4.5 Mars Java 64bit Full Editionをダウンロード。
(32bit環境の方は以降32bitに読み替えてください。)
Add Block等を入れている方はOFFにしましょう。
また、Google Chromeの場合、ダウンロード時にURLの横に盾マークがでて
このページは承認されていないソースからスクリプトを読み込もうとしています。
とでるので「安全でないスクリプトを読み込む」を押しましょう。
1GBほどあります。
Chromeを使っているとありがちですが、わりとダウンロードが失敗します。
ダウンロードした後の容量が1GB以上あるか確認しましょう。
解凍したらeclipse.exeがないなんてことがありました。

2.EclipseのPleiadesをインストール

ダウンロードしたファイル(私と同じものを選んだ場合pleiades-e4.5-java-jre_20160312.zip)
を解凍します。
なぜかWindows標準で備わっている解凍ツール以外で解凍するとファイルが壊れます。
解凍した後なぜかeclipse.exeが0バイトになっているなんてことがあります。

公式サイトに下記の記述があります。

解凍ツールは Windows 標準の展開ウィザードまたは WinRAR を推奨します。
この2つのツールは解凍時に MAX_PATH (最大パスを示す Windows 定数 = 260 文字) を超えていた場合、
適切なエラーを返します。その場合は c:\pleiades のような短いパスに解凍してください。
Windows XP 展開ウィザード、Lhaz、Explzh は MAX_PATH を超えている場合、
書庫が壊れているかのようなエラーが出ますが、
これも短いパスに解凍することで対応可能です。
7-Zip、Lhaplus、Lhaca は使用しないでください。MAX_PATH を超えていても警告やエラーが表示されず、
正常に解凍できたか判断できません。 

なので次の手順を行います。
pleiades-e4.5-java-jre_20160312.zipをC:\直下に置きます。
右クリック→プログラムから開く→エクスプローラー
で開き、Exploer上に出ているすべて展開ボタンを押します。
展開先の選択とファイルの展開ダイアログで、
C:\pleiades-4.5
としておきましょう。
※私の環境にはpleiades-4.4もあるため。
そうするとC:\pleiades-4.5\pleiadesとなっているので
1階層あげて
C:\pleiades-4.5としておきましょう。
これでこうなるはずです。
C:\pleiades-4.5の下には

.metadata.default
eclipse
java
tomcat

の4フォルダがあるはずです。

C:\pleiades-4.5\eclipse
を開いてeclipse.exeを右クリックしてタスクバーにピン止めかショートカットを作成しておきましょう。

3.チュートリアル本体のダウンロード

http://sastruts.seasar.org/download.html
にアクセスして、
Super Agile Strutsチュートリアル
sa-struts-tutorial-1.0.4-sp9.zip
をダウンロードしましょう

4.SAStrutsでの開発を便利にするPluginをEclipseにインストール

Eclipseを起動します。
ヘルプ > 新規ソフトウェアのインストールで追加ボタンを押します。
名前: Seasar Plugin3.3
ロケーション: http://eclipse.seasar.org/updates/3.3/
でOKを押します。
しばらくすると一覧が表示されるので
Dolteng
EMecha
Kijimuna
matatabi
ResourceSynchronizer
S2Junit4 Plugin
SAStrutsPlugin
を選び次へを押します。
Ymir以外を選びます。
あとはウィザードに従い完了を押します。

5.チュートリアルをインポートします。

Eclipseからファイル > インポートで
一般 > 既存プロジェクトをワークスペースへ
次へを押します。
アーカイブファイルの選択でsa-struts-tutorial-1.0.4-sp9.zipを選択します。
完了を押します。
たぶん、このあとエラーが出ますが気にしないでください。
エラー例:ビルドパスのエラーが解決されるまで、プロジェクトをビルドできません。

6.チュートリアルを動的プロジェクトに変換します。

ここの作業が重要です。ここのセクションのことを実施しないと
たぶん404 Not Foundを見続けることになります。
プロジェクトを右クリック > プロパティー
Tomcatを選択します。
全般タブでTomcatプロジェクトにチェック
コンテキスト名をsa-struts-tutorial
にしてOKを押します。

また、プロジェクトを右クリック > 構成で
プロジェクト・ファセットの変換を選びます。
一度選ぶとプロパティーのほうに出てきます。
動的Webモジュールにチェックを入れます。
ランタイムタブを選んで、
Tomcat8を選択

また、プロジェクトを右クリック > プロパティーで
デプロイメント・アセンブリーを選択します。
/WebContent /というのがあるので除去します。

追加ボタンよりフォルダーを選択します。
/src/main/webapp でデプロイ先を / にします。

この辺はここを参考にしました。
http://knightsheep.blogspot.jp/2014/01/web-eclipseweb.html

7.Eclipse付属のTomcat8の設定をします。

ウィンドウ > 設定でTomcatを選びます。
Tomcatホーム C:\pleiades-4.5\tomcat\8
コンテキスト・ファイルを選んで
コンテキスト・ディレクトリー C:\pleiades-4.5\tomcat\8\conf\Catalina\localhost
とします。
するとエラーが消えるかと思います。

8.コンテキスト定義を更新します。

プロジェクトを右クリック > Tomcatプロジェクト
コンテキスト定義を更新を実行します。

9.実行します。

プロジェクトを右クリック > 実行 > サーバーで実行を押します。
Tomcat v8.0サーバを選択します。
サーバのホスト名はlocalhostのままでよいです。
毎回コレで実行のチェックをしておいたほうが便利です。
完了を押します。
しばらくするとTomcatが起動し、
Eclipse上のブラウザが起動し、画面が表示されるはずです。

パースペクティブを開くからJavaEEを追加してそれに切り替えたほうが見やすいと思います…。

C++でカレンダー

f:id:fa11enprince:20200628230502j:plain
カレンダーなんてJavaScriptを使ったらすでにたくさん便利なのがありますが、 自分でカレンダーを書いたらどうなるか…とおもい書いてみました。C++で。 そういえばツェラーの公式というのがあり、使ってみました。 C++はここ数か月書いてませんでしたが以外に覚えているものですね。 std::unordered_mapをconstにしたらmap[i]みたいのができないんだメンドクセ…っていうのがありました(findで代用しました)

ソース

#include <cstdio>
#include <unordered_map>

namespace Pudding {
    using namespace std;
    static const unordered_map<int, int> DaysOfMonth = {
        {1,31}, {2,28}, {3,31}, {4,30}, {5,31}, {6,30},
        {7,31}, {8,31}, {9,30}, {10,31}, {11,30}, {12,31}
    };

    class Calendar {
    public:
        static void print(int year, int month) {
            printf("\n%4d%2d月", year, month);
            printf("\n 日 月 火 水 木 金 土\n");
            int index = get_day_of_week_by_zeller(year, month, 0);
            int position = index % 7;
            fill_with_blank_to(position);
            for (int i = 0; i < get_days_of_month(year, month); i++) {
                line_break_if_sunday(position);
                printf("%3d", i + 1);
                position++;
            }
            printf("\n");
        }

    private:
        // 指定位置まで空白を埋める
        static void fill_with_blank_to(int position) {
            for (int i = 0; i < position; i++) {
                printf("   ");
            }
        }
        // 位置により必要なら改行をする 7の倍数(この場合日曜)なら改行
        static void line_break_if_sunday(int position) {
            if ((position % 7) == 0) {
                printf("\n");
            }
        }

        // うるう年判定
        static bool is_leap_year(int year) {
            return (year % 400 == 0) ? true
                : (year % 100 == 0) ? false
                : (year % 4 == 0) ? true
                : false;
        }

        // 月の日数
        static int get_days_of_month(int year, int month) {
            int days = DaysOfMonth.find(month)->second;
            if (is_leap_year(year) && month == 2) {
                days = 29;
            }
            return days;
        }

        // ツェラーの公式 曜日のインデックスを求める (1:月~7:日)
        static int get_day_of_week_by_zeller(int year, int month, int day) {
            if (month < 3) {
                year--;
                month += 12;
            }
            return (year + year / 4 - year / 100 + year / 400 + (13 * month + 8 ) / 5 + day) % 7 + 1;
        }
    };

}

auto main() -> int {
    for (int i = 1; i <= 12; i++) {
        Pudding::Calendar::print(2016, i);
    }
}

実行結果

2016年 1月
 日 月 火 水 木 金 土
                 1  2
  3  4  5  6  7  8  9
 10 11 12 13 14 15 16
 17 18 19 20 21 22 23
 24 25 26 27 28 29 30
 31

2016年 2月
 日 月 火 水 木 金 土
     1  2  3  4  5  6
  7  8  9 10 11 12 13
 14 15 16 17 18 19 20
 21 22 23 24 25 26 27
 28 29

2016年 3月
 日 月 火 水 木 金 土
        1  2  3  4  5
  6  7  8  9 10 11 12
 13 14 15 16 17 18 19
 20 21 22 23 24 25 26
 27 28 29 30 31

...省略

単方向リスト(Singly Linked List)の実装 (C++)

f:id:fa11enprince:20160518180613j:plain
単方向リストを勉強がてら久々にビールを飲みながらC++で書いてみました。
これをやると、ポインタって何かってよく理解できますね。
しかしビールを飲むと、わけのわからないミスをしでかします…。

ソースコード

#include <iostream>
#include <exception>

namespace My {

    template <typename T>
    class SinglyLinkedList {
    private:
        struct Node {
            T value;
            Node *next;
            Node() : value(0), next(nullptr) {
            }
            ~Node() {
            }
        };
        Node *head_;
        int number_;

        Node *create(T value) {
            Node *node = new Node();
            node->value = value;
            node->next = nullptr;
            return node;
        }
        void destroy(Node *node) {
            delete node;
            node = nullptr;
        }
    public:
        SinglyLinkedList() :
            head_(nullptr)
            ,number_(0) {
        }
        ~SinglyLinkedList() {
            Node *cur = nullptr;
            for (Node *node = head_;
                 node != nullptr;
                 cur = node, node = node->next) {
                destroy(cur);
            }
        }
        void push_front(T value) {
            Node *new_node = create(value);
            if (head_ == nullptr) {
                head_ = new_node;
            }
            else {
                Node *node = head_;
                head_ = new_node;
                head_->next = node;
            }
            number_++;
        }
        void push_back(T value) {
            Node *new_node = create(value);
            if (head_ == nullptr) {
                head_ = new_node;
            }
            else {
                Node *node = head_;
                Node *tail = nullptr;
                for (; node != nullptr; tail = node, node = node->next);
                tail->next = new_node;
            }
            number_++;
        }
        void insert(int pos, T value) {
            Node *new_node = create(value);
            if (pos == 0) {
                push_front(value);
            }
            else if (pos < number_ - 1) {
                Node *prev = nullptr;
                Node *node = head_;
                for (int i = 0; i < pos; ++i) {
                    prev = node;
                    node = node->next;
                }
                prev->next = new_node;
                new_node->next = node;
                number_++;
            }
            else if (pos == number_ - 1) {
                push_back(value);
            }
            else {
                throw std::bad_exception();
            }
        }
        void pop_front() {
            Node *node = head_;
            head_ = node->next;
            destroy(node);
            number_--;
        }
        void pop_back() {
            Node *node = head_;
            Node *prev = nullptr;
            Node *tail = nullptr;
            for (; node != nullptr;
                 prev = tail, tail = node, node = node->next);
            prev->next = nullptr;
            destroy(tail);
            number_--;
        }
        void erase(int pos) {
            if (head_ == nullptr) {
                throw std::bad_exception();
            }
            Node *node = head_;
            if (pos == 0) {
                pop_front();
            }
            else if (pos < number_ - 1) {
                Node *prev = node;
                for (int i = 0; i < pos; ++i) {
                    prev = node;
                    node = node->next;
                }
                prev->next = node->next;
                destroy(node);
                number_--;
            }
            else if (pos == number_ - 1) {
                pop_back();
            }
            else {
                throw std::bad_exception();
            }
        }
        void display() {
            for (Node *node = head_; node != nullptr; node = node->next) {
                std::cout << node->value << ' ';
            }
            std::cout << std::endl;
        }
    };
}

auto main()->int {
    try {
        My::SinglyLinkedList<int> list;
        list.push_back(2);
        list.display();
        list.push_back(3);
        list.display();
        list.push_back(4);
        list.display();
        list.push_front(1);
        list.display();
        list.push_front(0);
        list.display();
        list.insert(1, 3);
        list.display();
        std::cout << "---------" << std::endl;
        list.pop_back();
        list.display();
        list.pop_front();
        list.display();
        list.erase(2);
        list.display();
        list.pop_back();
        list.display();
        std::cout << "---------" << std::endl;
        list.push_back(4);
        list.display();
    }
    catch (std::exception& e) {
        std::cout << e.what() << std::endl;
    }
}

イテレーターがねえ!とか
追加するたびnewしてる…とかいろいろ足りないところありますが、
勉強で書くにはこれで十分でしょう。
双方向リストじゃないと微妙に実用性ないですね。
ちょこっと変えると双方向リストになります。

実行結果

$ ./list.exe
2
2 3
2 3 4
1 2 3 4
0 1 2 3 4
0 3 1 2 3 4
---------
0 3 1 2 3
3 1 2 3
3 1 3
3 1
---------
3 1 4

参考リンクなど

以前にも書いてたな…

MacにEclipse 4.5 Mars に Pleiades導入

Mac版 Eclipseのダウンロード

Eclipse Downloads | The Eclipse Foundation
ここからEclipse IDE for Java EE Developersをダウンロードします

Pleiadesをダウンロード

統合開発環境 Eclipse 日本語化プロジェクト - Pleiades
ここからPleiades All in One 4.5.2のJava 64bit Standard Editionをダウンロードします。

Eclipseのインストール

ダウンロードしたeclipse-jee-mars-2-macosx-cocoa-x86_64.tar.gzを解凍します
その後、Eclipse.appを/Applicationsに移動します
なお、任意のフォルダに移動する場合はCommand + Shift + Gです
さらに、pleiades-e4.5-java_20160312.zipを解凍します
/Applications/Eclipse.appを右クリックしてパッケージの内容を表示を選択します。
次のものをごそっと全部Pleiadesからコピーします
Windowsと違ってフォルダごと移動すると中身が上書きされちゃうので注意です。
なのでFinderでやる場合はフォルダに潜ってファイル全選択をしてコピーがいいと思います。

コピー元 コピー先
pleiades/eclipse/dropins/ /Applications/Eclipse.app/Contents/Eclipse/dropins/
pleiades/eclipse/features/ /Applications/Eclipse.app/Contents/Eclipse/features/
pleiades/eclipse/plugins/ /Applications/Eclipse.app/Contents/Eclipse/plugins/

また、/Applications/Eclipse.app/Contents/Eclipse/eclipse.iniを書き換えます
ファイルの末尾に次を追加してください
javaagentの場所は/Applications/Eclipse.app/Contents/MacOSから見たpleiades.jarのPATHです

-Dfile.encoding=UTF-8
-Xverify:none
-javaagent:../Eclipse/dropins/MergeDoc/eclipse/plugins/jp.sourceforge.mergedoc.pleiades/pleiades.jar

Eclipseを起動します

問題が起こりやすいのでコマンドラインから起動します。
そうすると原因を教えてくれます。
私はjavaagentのPATH指定で一度失敗しました

$ /Applications/Eclipse.app/Contents/MacOS/eclipse

なお、起動時に
「eclipse.appは壊れているために開けません。”ゴミ箱”に入れる必要があります」
というイケてないメッセージが出ましたら
システム環境設定 > セキュリティとプライバシー > ダウンロードしたアプリケーションの実行許可
で全てのアプリケーションを許可に変更してみてください。