目次

チュートリアル2(ソース)

以下のソースをテストします。

商品マスタ

商品マスタは、クラスにより準備しています。プログラムが変更する事がない不変のデータです。

test/Master.java
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
package test;
 
 
public class Master {
    public String name;
    public int price;
 
    public static Master[]    db = new Master[]{
        new Master( "A4ノート", 189 ),
        new Master( "ボールペン", 99 ),
        new Master( "セロハンテープ", 45 ),
        new Master( "A4コピー用紙 500枚", 298 )
    };
 
    private Master( String name, int price ) {
        this.name = name;
        this.price = price;
    }
}

ショッピングカート

ショッピングカートは、HTTPセッションにて管理します。
商品ページでカートへ追加し、カート画面でカートの中身を表示します。

test/Cart.java
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:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
68:
69:
70:
71:
72:
73:
74:
75:
76:
77:
78:
79:
80:
81:
82:
83:
84:
85:
86:
87:
88:
89:
90:
package test;
 
import java.util.*;
import javax.servlet.http.*;
import paraselene.supervisor.*;
 
public class Cart {
    private static final String SESSION = "cart";
 
    private class Item {
        int no, count;
        Item( int no, int count ) {
            this.no = no;
            this.count = count;
        }
    }
 
    private ArrayList<Item> item = new ArrayList<Item>();
 
    private Cart() {}
 
    /**
     * 新規購入。
     * @param no    Masterの配列番号。
     * @param count 購入数。
     */
    public void add( int no, int count ) {
        item.add( new Item( no, count ) );
    }
 
    /**
     * キャンセル。
     * @param line カートの何番目のデータか?
     */
    public void remove( int line ) {
        item.remove( line );
    }
 
    /**
     * カートの登録件数。
     */
    public int getItemCount() {
        return item.size();
    }
 
    /**
     * 購入品の名称。
     */
    public String getName( int line ) {
        return Master.db[item.get( line ).no].name;
    }
 
    /**
     * 単価。
     */
    public int getPrice( int line ) {
        return Master.db[item.get( line ).no].price;
    }
 
    /**
     * 購入数。
     */
    public int getCount( int line ) {
        return item.get( line ).count;
    }
 
    /**
     * 合計金額。
     */
    public int getAllPrice() {
        int sum = 0;
        int all = getItemCount();
        for ( int i = 0; i < all; i++ ) {
            sum += getPrice( i ) * getCount( i );
        }
        return sum;
    }
 
    /**
     * カートの取得。
     */
    public static Cart getCart( RequestParameter req ) {
        HttpSession session = req.getSession();
        Cart    cart = (Cart)session.getAttribute( SESSION );
        if ( cart != null ) return cart;
        cart = new Cart();
        session.setAttribute( SESSION, cart );
        return cart;
    }
}

shop.html

商品ページです。 ※スケルトン生成のコメントは一部削除しています。

test/logic/ShopHtml.java
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:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
68:
69:
70:
71:
72:
73:
74:
75:
76:
77:
78:
79:
80:
81:
82:
83:
84:
85:
86:
87:
88:
89:
90:
91:
92:
93:
94:
95:
96:
97:
98:
99:
100:
101:
102:
103:
package test.logic;
 
import test.*;
import test.base.*;
import paraselene.*;
import paraselene.supervisor.*;
import paraselene.tag.*;
import paraselene.tag.attr.*;
import paraselene.tag.form.*;
import paraselene.tag.list.*;
import paraselene.tag.table.*;
import java.math.*;
 
/**
* shop.html
*/
public class ShopHtml extends test.view.ShopHtml {
    private class Item {
        int             no;
        SingleTextBox   count;
        Item( int no, SingleTextBox count ) {
            this.no = no;
            this.count = count;
        }
    }
    /**
     * コンストラクタ。
     */
    public ShopHtml() {
        super();
        init();
    }
    /**
     * 初期化。
     */
    public void init(){
        super.init();
        getErrorP().setVisible( false );
        Class<?>[]  cls = new Class<?>[]{
            getCountInput().getClass(),
            getBuyInput().getClass()
        };
        Table   list = getListTable();
        Line    org_line = list.getLineReplica( 0 );
        list.removeLine( 0 );
        for ( int i = 0; i < Master.db.length; i++ ) {
            Line    line = (Line)org_line.getReplica();
            Tag[]   tag = line.getAllTagByClass( cls[0] );
            Item    item = new Item( i, (SingleTextBox)tag[0] );
            tag = line.getAllTagByClass( cls[1] );
            ((Button)tag[0]).setUserData( item );
            line.setValueString( 0, Master.db[i].name );
            line.setValueString( 2, Integer.toString( Master.db[i].price ) + "円" );
            list.addLine( line );
        }
    }
    /**
     * 別名URI設定。nullを返すと別名は設定しません。
     * @return URI。必ず".na"で終えて下さい。
     */
    public String getAliasURI() {
        return "shop.na";
    }
    /**
     * 入力値の検証を行う。
     * このメソッドが呼ばれる際には必ずセッションが発生しています。
     * 入力値のエラーチェックや入力値に即した動作を記述します。
     * @param req リクエスト内容。
     * @param fw デフォルト遷移先。
     * @exception PageException 処理の継続が不可能(ブラウザには500を返す)。
     */
    public Forward inputMain( RequestParameter req, Forward fw ) throws PageException {
        getErrorP().setVisible( false );
        int     count = 0;
        Item    item = (Item)getClickedButton().getUserData();
        try {
            BigInteger  bi = item.count.toInteger( null, BigInteger.ONE, null );
            count = bi.intValue();
        }
        catch( Exception e ) {
            getErrorP().setVisible( true );
            return new Forward( PageType.SHOP_HTML, false, false );
        }
        Cart    cart = Cart.getCart( req );
        cart.add( item.no, count );
        return fw;
    }
    /**
     * 出力情報の設定を行う。
     * @param from 遷移元ページ。直接呼ばれている場合はnullです。
     * @param req リクエスト内容。
     * @return 出力ページ。
     * nullを返すとthisをリターンしたのと同じ扱いにされます。
     * @exception PageException 処理の継続が不可能(ブラウザには500を返す)。
     */
    public Page outputMain( Page from, RequestParameter req ) throws PageException {
        Tag[]   tag = getCountTags();
        for ( int i = 0; i < tag.length; i++ ) {
            tag[i].setValueString( null );
        }
        return this;
    }
}

cart.html

カート表示ページです。

test/logic/CartHtml.java
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:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
68:
69:
70:
71:
72:
73:
74:
75:
76:
77:
78:
79:
80:
81:
82:
83:
84:
85:
86:
87:
88:
89:
90:
91:
92:
93:
94:
95:
96:
97:
98:
99:
100:
101:
102:
103:
104:
105:
106:
107:
108:
109:
110:
111:
112:
package test.logic;
 
import test.*;
import test.base.*;
import paraselene.*;
import paraselene.supervisor.*;
import paraselene.tag.*;
import paraselene.tag.attr.*;
import paraselene.tag.form.*;
import paraselene.tag.list.*;
import paraselene.tag.table.*;
import java.net.*;
 
/**
* cart.html
*/
public class CartHtml extends test.view.CartHtml {
    private Line    org_line;
    /**
     * コンストラクタ。
     */
    public CartHtml() {
        super();
        init();
    }
 
    /**
     * 初期化。
     */
    public void init(){
        super.init();
        org_line = getListTable().getLineReplica( 1 );
    }
 
    /**
     * 別名URI設定。nullを返すと別名は設定しません。
     * @return URI。必ず".na"で終えて下さい。
     */
    public String getAliasURI() {
        return null;
    }
 
    /**
     * 入力値の検証を行う。
     * このメソッドが呼ばれる際には必ずセッションが発生しています。
     * 入力値のエラーチェックや入力値に即した動作を記述します。
     * @param req リクエスト内容。
     * @param fw デフォルト遷移先。
     * @exception PageException 処理の継続が不可能(ブラウザには500を返す)。
     */
    public Forward inputMain( RequestParameter req, Forward fw ) throws PageException {
        Tag[]   tag = getCancelTags();
        if ( tag != null ) {
            for ( int i = 0; i < tag.length; i++ ) {
                Button  button = (Button)tag[i];
                if ( button.isClicked() ) {
                    Integer line = (Integer)button.getUserData();
                    Cart.getCart( req ).remove( line.intValue() );
                    return fw;
                }
            }
        }
        if ( getBuyInput().isClicked() ) {
            try {
                // 外部決済サービスを呼び出すイメージ
                fw = new Forward( new URI( "http://paraselene.sourceforge.jp/" ), true );
            }
            catch( Exception e ) {
                throw new PageException( e );
            }
        }
        return fw;
    }
 
    /**
     * 出力情報の設定を行う。
     * @param from 遷移元ページ。直接呼ばれている場合はnullです。
     * @param req リクエスト内容。
     * @return 出力ページ。
     * nullを返すとthisをリターンしたのと同じ扱いにされます。
     * @exception PageException 処理の継続が不可能(ブラウザには500を返す)。
     */
    public Page outputMain( Page from, RequestParameter req ) throws PageException {
        Class<?>    cls = null;
        try {
            cls = Class.forName( "paraselene.tag.form.Button" );
        }
        catch( Exception e ){
            throw new PageException( e );
        }
        Table   list = getListTable();
        // 先頭行(見出し)と最終行(合計)は残して削除。
        list.removeLine( 1, list.getLineCount() - 2 );
        Cart    cart = Cart.getCart( req );
        int item_count = cart.getItemCount();
        for ( int i = 0; i < item_count; i++ ) {
            Line    line = (Line)org_line.getReplica();
            Tag[]   tag = line.getAllTagByClass( cls );
            ((Button)tag[0]).setUserData( new Integer( i ) );
            line.setValueString( 0,
                cart.getName( i ),
                Integer.toString( cart.getPrice( i ) ) + "円",
                Integer.toString( cart.getCount( i ) ),
                Integer.toString( cart.getPrice( i ) * cart.getCount( i ) ) + "円"
            );
            list.addLine( i + 1, line );
        }
        getSumTd().setValueString( Integer.toString( cart.getAllPrice() ) + "円" );
        getBuyInput().setDisabled( item_count > 0?  false: true );
        return this;
    }
}

チュートリアル3(内部動作)