2011-08-11 11 views
10

私のJSFページなぜselectOneMenu ItemLabelをコンバーターに送りますか?このコンバータの


<h:form> 
    <h:selectOneMenu id="studlist" value="#{studBean.selectedStudent}">     
    <p:ajax event="change" process="studlist" update="studdep" ></p:ajax> 
    <f:selectItems value="#{studBean.student}" var="s" 
        itemValue="#{s.studid}" itemLabel="#{s.name}"/> 
    <f:converter converterId="studentconverter"/> 
    </h:selectOneMenu> 
</h:form> 

コンバータクラス(StudentConverter)


public Object getAsObject(FacesContext context, UIComponent component, String value) { 

Student studConvert= new Student(); 
List<Student> students=new ArrayList<Student>(); 
students=(ArrayList<Student>)((UISelectItems  
      component.getChildren().get(0)).getValue(); 
} 

それがなぜ起こるか引数 '文字列値は' ITEMLABEL iを与えます? 私はあなたのページは次のようになりますh:selectOneMenu

f:selecitems属性でのSelectItemリストを使用する必要があり、この文字列

答えて

17

getAsObject()の項目値の代わりに項目ラベルを取得した理由がわかりません。おそらくあなたのgetAsString()はそれを間違っているし、学生IDに基づいて学生名を返すでしょう。

いずれにせよ、私はあなたのitemValueが間違いなく正しいと言うことができます。

<h:selectOneMenu id="studlist" value="#{studBean.selectedStudent}"> 
    <f:selectItems value="#{studBean.student}" var="s" 
     itemValue="#{s.studid}" itemLabel="#{s.name}" /> 
    <f:converter converterId="studentconverter" /> 
</h:selectOneMenu> 

変換器は、それがHTTPリクエストパラメータとしての周りに渡すことができるように、複雑なJavaオブジェクトと文字列表現の間で変換するために使用さintentedされます。ただし、学生ID全体を学生の代わりに項目値として指定しています。あなたは学生オブジェクト全体を代わりに指定する必要があります。また、#{studBean.selectedStudent}Studentプロパティを参照するようにしてください。Longプロパティが学生IDを表すものではありません。

次のようにitemValueを修正:

<h:selectOneMenu id="studlist" value="#{studBean.selectedStudent}"> 
    <f:selectItems value="#{studBean.student}" var="s" 
     itemValue="#{s}" itemLabel="#{s.name}" /> 
    <f:converter converterId="studentconverter" /> 
</h:selectOneMenu> 

とあなたのコンバータを次のように(ささいなnullchecksは省略):

public String getAsString(FacesContext context, UIComponent component, Object value) { 
    // This method is called when item value is to be converted to HTTP request parameter. 
    // Normal practice is to return an unique identifier here, such as student ID. 
    Student student = (Student) value; 
    Long id = student.getStudid(); 
    return String.valueOf(id); 
} 

public Object getAsObject(FacesContext context, UIComponent component, String value) { 
    // This method is called when HTTP request parameter is to be converted to item value. 
    // You need to convert the student ID back to Student. 
    Long id = Long.valueOf(value); 
    Student student = someStudentService.find(id); 
    return student; 
} 

働くべきです。あなたが最初に持っていたとして

また、あなたはあなたのitemValueを保つことができると完全<f:converter>を削除したが、その後、あなたは学生のIDを表すLongプロパティを指すように#{studBean.selectedStudent}を変更する必要があります。

+0

いつものように素晴らしいBalusC!ありがとう – Mariah

1

にitemValueワンド。

<h:form> 
    <h:selectOneMenu id="studlist" value="#{studBean.selectedStudent}"> 
    <p:ajax event="change" process="studlist" update="studdep" ></p:ajax>  
    <f:selectItems value="#{studBean.studentSelectItemList}" />  
    <f:converter converterId="studentconverter"/>  
    </h:selectOneMenu> 
</h:form> 

backing bean側では、studentSelectItemList selectitemを入力する必要があります。

private List<SelectItem> studentSelectItemList; 

//fill studentSelectItemList at the appropriate place 
    studentSelectItemList.add(new SelectItem(studentId,studentName)); 

これらの設定の後で、学生IDを選択値として設定する必要があります。

+0

これは何の違いもありません。 – BalusC

0

今日私は同じ問題を抱えていました。

それは、この間違ったレンダリングによって引き起こされる:代わりに空の文字列ラベルを提出するには、「なし」オプションの結果のために

value=""を省略
<select ...> 
    <option>None</option> 
    <option value="1">First</option> 
    <option value="2">Second</option> 
</select> 

。しかしちょうどgetAsString()を確認してこの問題を解決し、最初のオプションのためのレンダラーの書き込みvalue=""を作るために

代わり""(空の文字列)を返し、nullをを返すことはありません。救助へ


@BalusC

<h:form id="form"> 
    ... 

    <p:selectOneMenu id="targetStep" value="#{action.targetStep}" required="true"> 
     <o:converter converterId="omnifaces.ListIndexConverter" list="#{entity.stepList}" /> 
     <f:selectItems var="step" value="#{entity.stepList}" itemLabel="#{step.name}" 
      itemValue="#{step}" /> 
    </p:selectOneMenu> 

    <p:commandButton process="@this" update="@widgetVar(stepDialog)" 
     oncomplete="PF('stepDialog').show()" icon="#{icons.addStep}" 
     value="#{bundle.addStep}"> 
     <f:setPropertyActionListener target="#{viewScope.step}" 
      value="#{s:newInstance('it.shape.edea2.jpa.WorkflowStep')}" /> 
    </p:commandButton> 

    <p:message for="targetStep" /> 

    ... 
</h:form> 

<p:dialog widgetVar="stepDialog" header="#{bundle.addStep}" modal="true" dynamic="true" 
    resizable="false"> 
    <h:form> 
     <p:panelGrid columns="2" styleClass="app-full-width"> 
      <h:outputLabel value="#{bundle.name}" /> 
      <h:panelGroup> 
       <p:inputText id="name" value="#{step.name}" required="true" /> 
       <p:message for="name" /> 
      </h:panelGroup> 

      ... 

      <f:facet name="footer"> 
       <p:commandButton process="@form" update="@form :form" 
        oncomplete="hideDialog('stepDialog', args)" icon="#{icons.confirm}" 
        value="#{bundle.confirm}"> 
        <p:collector value="#{step}" addTo="#{entity.stepList}" /> 
        <f:setPropertyActionListener target="#{action.targetStep}" 
         value="#{step}" /> 
       </p:commandButton> 
      </f:facet> 
     </p:panelGrid> 
    </h:form> 
</p:dialog> 

あなたomnifaces.ListIndexConverter :)

+0

同意しない。 '#{s.studid}'がnull(サービスによってロードされていない新しいStudentインスタンス)であり、 'getAsString()'がnull submittedValue(おそらく)に対してnullを返す場合、この状況が発生する可能性があります。そして、このスニペットは、*なぜselectOneMenu ItemLabelをコンバータに送るのですか?*。しかし、数時間前にその質問が見つかった場合、私はその時間を惜しまなかったでしょう:) –

+0

私はそれが一般的ではないことを知っていますが、ユーザーがエンティティを選択し、彼自身。更新された回答の実際の使用例 –

0

BalusC(再び)は私のためにこれを釘付け。 同じ問題がありました。BalusCが以前に指摘したように、コンバータのgetAsString()メソッドはオブジェクトの「firstname」プロパティを返しました。

@OverrideパブリックストリングgetAsString(のFacesContextコンテキストのUIComponentコンポーネント、オブジェクト値){

if (value == null || value.equals("")) { 
     return ""; 
    } 
    else { 
     return String.valueOf(((Employee) value).getfirstname()); 
    } 

} 

Iは、IDを返すために、これを変更し、期待どおりに作業を開始しました。

@OverrideパブリックストリングgetAsString(のFacesContextコンテキストのUIComponentコンポーネント、オブジェクト値){理論を説明する

if (value == null || value.equals("")) { 
     return ""; 
    } 
    else { 
     return String.valueOf(((Employee) value).getId()); 
    } 

} 

BalusCあなたの努力が非常理解されます。あなたは天国です!

関連する問題