1

私の JSF ページコードには、次のようなものがあります。

<h:selectOneRadio value="#{pagecode.attending}" required="true" id="attendingRadio" binding="#{attendingRadio}">
    <f:selectItem itemValue="Y" itemLabel="Yes"/>
    <f:selectItem itemValue="N" itemLabel="No"/>
    <f:ajax event="click" render="attendingDetails" execute="attendingRadio"/>
</h:selectOneRadio>
<h:panelGroup id="attendingDetails">
    <h:panelGroup rendered="#{pagecode.showForm(attendingRadio.value)}">
        ...
    </h:panelGroup>
</h:panelGroup>

私のページコード バッキング Bean には、次のようなものがあります。

public class Pagecode {
    protected Character attending;

    public Character getAttending() {
        return attending;
    }

    public void setAttending(Character attending) {    
        this.attending=attending;
    }

    public boolean showForm(Character c) {
        if (c==null) {
            return false;
        }
        (other stuff)...
    }

}

私が期待する動作は次のとおりです。

  1. ページが読み込まれます。ラジオボタンは初期値に定義されていないため、Yes も No も選択されていません。ただし、required="true" であるため、空白のままにしておくと、ユーザーは検証エラーを受け取ります (実際に空白にしておくと)。
  2. ページが最初に読み込まれるときに #{pagecode.showForm(attendingRadio.value)} が呼び出されると、null オブジェクトが渡され、showForm が false を返すことが予想されます。

いくつかのトラブルシューティングを行った後、実際に起こっていることは、null オブジェクトが渡される代わりに、つまり;showForm(null)と同等のものを取得していることであると判断しました。showForm(new Character('\u0000'))代わりに null の Unicode 文字を渡します。

h:selectOneRadio で値が選択されていない場合、JSF に null 文字ではなく Java null オブジェクトを渡す方法はありますか? WebSphere Portal 8.0 で Apache MyFaces JSF 2.0 を使用しています。

補足として、web.xml に以下を追加しようとしましたが、残念ながら役に立ちませんでした。

<context-param>
   <param-name>javax.faces.INTERPRET_EMPTY_STRING_SUBMITTED_VALUES_AS_NULL</param-name>
   <param-value>true</param-value>
</context-param>
4

1 に答える 1

1

Character/charは、表現したい値のデータ型として不適切です。

enum代わりにを使用してください。

public enum Choice {
    Y, N;
}

private Choice attending;

または、おそらくもっと良いでしょうBoolean

private Boolean attending;

<f:selectItem itemValue="true" itemLabel="Yes" />
<f:selectItem itemValue="false" itemLabel="No" />
于 2015-12-29T21:30:05.130 に答える