いろいろ備忘録日記

主に .NET とか Go とか Flutter とか Python絡みのメモを公開しています。

Tapestry奮闘記-0012 (Forコンポーネント(2))

今度は、ForコンポーネントをForm内で使用してみます。
ドキュメントを見るとForコンポーネントはForm内に配置すると
自動的にhiddenタグでリストの値を残すようです。

今回は、新旧の値を表示し、新しい値を入力されたら元の
値を更新するサンプルを作成します。

[HTMLテンプレート]

                    <!--
                    
                        Form内でForコンポーネントを使用しリストを表示する。

                    -->
                    <br/>
                    <hr/>
                    <div id="for_001" align="center">
                        <b>
                            Forコンポーネントを使用(1)<br/>
                            <br/>
                            ForコンポーネントをFormコンポーネント内で使用。<br/>
                        </b>
                        <br/>
                        <form jwcid="form003@Form" success="ognl:listeners.changeValues" name="form003" method="post" action="">
                            出力: <br/>
                            <table border="1">
                                <tr>
                                    <th>インデックスの値</th>
                                    <th>Value 1</th>
                                    <th>Value 2</th>
                                    <th>Value 3</th>
                                    <th>Value 4</th>
                                </tr>
                                <tr jwcid="@For" source="ognl:for002Values" value="ognl:aFor002Value" index="ognl:for002Index" element="literal:tr">
                                    <td align="center">
                                        <span jwcid="@Insert" value="ognl:for002Index">値0</span><br/>
                                    </td>
                                    <td>
                                        旧:<span jwcid="@Insert" value="ognl:aFor002Value.oldValue.value1">値1</span><br/>
                                        新:<input jwcid="@TextField" type="text" name="textfield1" value="ognl:aFor002Value.newValue.value1"/>
                                    </td>
                                    <td>
                                        旧:<span jwcid="@Insert" value="ognl:aFor002Value.oldValue.value2">値2</span><br/>
                                        新:<input jwcid="@TextField" type="text" name="textfield2" value="ognl:aFor002Value.newValue.value2"/>
                                    </td>
                                    <td>
                                        旧:<span jwcid="@Insert" value="ognl:aFor002Value.oldValue.value3">値3</span><br/>
                                        新:<input jwcid="@TextField" type="text" name="textfield3" value="ognl:aFor002Value.newValue.value3"/>
                                    </td>
                                    <td>
                                        旧:
                                        <span jwcid="@If" condition="ognl:aFor002Value.oldValue.value4">○</span>
                                        <span jwcid="@Else">×</span>
                                        <br/>
                                        新:<input jwcid="@Checkbox" type="checkbox" name="checkbox1" value="ognl:aFor002Value.newValue.value4"/>
                                    </td>
                                </tr>
                            </table>
                            <input jwcid="chageButton@Submit" type="button" name="changeButton" value="         変更         "/>
                        </form>
                        <br/>
                    </div>
                    <hr/>
                    <br/>

[ページ仕様ファイル]

<page-specification class="gsf.samples.tapestry.ThirdSample">

    <property name="radio001" initial-value="ognl:@gsf.samples.tapestry.ThirdSample@RADIO001"/>
    
    <property name="aFor001Value"/>
    <property name="for001Index"/>

    <property name="aFor002Value"/>
    <property name="for002Index"/>
</page-specification>

[ページコンポーネントクラス]
今回は、新旧の値を保持するため、データオブジェクトを新たに定義しています。
また、そのままだとリクエスト毎に保持している値がきえてしまうため、該当する
値を@Persistアノテーションを使用して、セッションに保持しています。

package gsf.samples.tapestry;

import java.io.*;

public class For002ValueObject implements Serializable{

    private String  value1;
    private Integer value2;
    private Long    value3;
    private boolean value4;
    
    /**
     * Get value1.
     *
     * @return value1 as String.
     */
    public String getValue1(){
        return this.value1;
    }
    
    /**
     * Set value1.
     *
     * @param value1 the value to set.
     */
    public void setValue1(String value1){
        this.value1 = value1;
    }
    
    /**
     * Get value2.
     *
     * @return value2 as Integer.
     */
    public Integer getValue2(){
        return this.value2;
    }
    
    /**
     * Set value2.
     *
     * @param value2 the value to set.
     */
    public void setValue2(Integer value2){
        this.value2 = value2;
    }
    
    /**
     * Get value3.
     *
     * @return value3 as Long.
     */
    public Long getValue3(){
        return this.value3;
    }
    
    /**
     * Set value3.
     *
     * @param value3 the value to set.
     */
    public void setValue3(Long value3){
        this.value3 = value3;
    }
    
    /**
     * Get value4.
     *
     * @return value4 as boolean.
     */
    public boolean getValue4(){
        return this.value4;
    }
    
    /**
     * Set value4.
     *
     * @param value4 the value to set.
     */
    public void setValue4(boolean value4){
        this.value4 = value4;
    }
}
package gsf.samples.tapestry;

import java.io.*;

public class For002Value implements Serializable{

    private For002ValueObject oldValue;
    private For002ValueObject newValue;
    
    /**
     * Get oldValue.
     *
     * @return oldValue as For002ValueObject.
     */
    public For002ValueObject getOldValue(){
        return this.oldValue;
    }
    
    /**
     * Set oldValue.
     *
     * @param oldValue the value to set.
     */
    public void setOldValue(For002ValueObject oldValue){
        this.oldValue = oldValue;
    }
    
    /**
     * Get newValue.
     *
     * @return newValue as For002ValueObject.
     */
    public For002ValueObject getNewValue(){
        return this.newValue;
    }
    
    /**
     * Set newValue.
     *
     * @param newValue the value to set.
     */
    public void setNewValue(For002ValueObject newValue){
        this.newValue = newValue;
    }
}
package gsf.samples.tapestry;

import java.util.*;

import org.apache.tapestry.*;
import org.apache.tapestry.annotations.*;
import org.apache.tapestry.event.*;
import org.apache.tapestry.html.*;

public abstract class ThirdSample extends BasePage implements PageBeginRenderListener{

    public static final String RADIO001 = "radio button 1";
    public static final String RADIO002 = "radio button 2";
    public static final String RADIO003 = "radio button 3";

    /**
     * PageBeginRenderListenerの実装メソッド.<br/>
     * このメソッドは、ページがリクエストされる毎に最初に呼ばれる。<br/>
     * 
     * @param event イベントオブジェクト
     */
    public void pageBeginRender(PageEvent event){

        this.createFor001Values();

        if(this.getFor002Values() == null){
            this.createFor002Values();
        }

    }

    public void changeValues(IRequestCycle cycle){

        for(Iterator<For002Value> iter = this.getFor002Values().iterator(); iter.hasNext();){

            For002Value value = iter.next();

            For002ValueObject oldValue = value.getOldValue();
            For002ValueObject newValue = value.getNewValue();

            oldValue.setValue1(newValue.getValue1());
            oldValue.setValue2(newValue.getValue2());
            oldValue.setValue3(newValue.getValue3());
            oldValue.setValue4(newValue.getValue4());
        }

    }

    private void createFor001Values(){

        List<For001Value> values = new ArrayList<For001Value>();
        for(int i = 0; i < 20; i++){
            For001Value aFor001Value = new For001Value();
            aFor001Value.setValue1("value1-" + i);
            aFor001Value.setValue2("value2-" + i);
            aFor001Value.setValue3(i);

            if*1;
            newValue.setValue2(oldValue.getValue2());
            newValue.setValue3(oldValue.getValue3());
            newValue.setValue4(oldValue.getValue4());

            aFor002Value.setNewValue(newValue);

            values.add(aFor002Value);
        }

        this.setFor002Values(values);
    }

    public abstract boolean getCheckbox001();
    public abstract void setCheckbox001(boolean value);

    public abstract String getRadio001();
    public abstract void setRadio001(String value);

    public abstract List<For001Value> getFor001Values();
    public abstract void setFor001Values(List<For001Value> values);

    @Persist
    public abstract List<For002Value> getFor002Values();
    public abstract void setFor002Values(List<For002Value> values);
}

*1:i % 2) == 0){ aFor001Value.setValue4(true); }else{ aFor001Value.setValue4(false); } values.add(aFor001Value); } this.setFor001Values(values); } private void createFor002Values(){ List<For002Value> values = new ArrayList<For002Value>(); for(int i = 0; i < 5; i++){ For002Value aFor002Value = new For002Value(); For002ValueObject oldValue = new For002ValueObject(); oldValue.setValue1("value1-" + i); oldValue.setValue2(100); oldValue.setValue3(15000L); if((i % 2) == 0){ oldValue.setValue4(true); }else{ oldValue.setValue4(false); } aFor002Value.setOldValue(oldValue); For002ValueObject newValue = new For002ValueObject(); newValue.setValue1(oldValue.getValue1(