2012-11-28 4 views
3

문자열 목록을 보유하고있는 빈을 가진 JSF 2.0 애플리케이션이 있습니다.JSF : 목록에 문자열 추가

문자열을 <h:inputText>/>에서 내 목록에 추가하고 내 목록을 표시하고 싶습니다.

다음 코드는 내 목록에 참조를 넣었습니다. 그래서 내 목록의 모든 요소는 마지막 입력으로 설정됩니다. 여기

@ManagedBean 
@ApplicationScoped 
public class Bean { 

private String name; 
private ArrayList<String> test = new ArrayList<String>(); 

public Bean() { 
} 

public Bean(String name) { 
    this.name = name; 
} 


public String addtoList(String _name){ 
    test.add(_name); 
    return "./index.xhtml"; 
} 


/***************GETTER/SETTER/HASHCODE/EQUALS**************************/ 
    ... 

} 

내 index.xhtml의 일부 :

 <h:inputText id="name" 
         value="#{bean.name}" 
         required="true"> 
     </h:inputText> 
     <h:commandButton value="Post" 
         action="#{bean.addtoList(name)}"/> 
     <br/> 
     <h:dataTable var="bean" 
        value="#{bean.test}"> 
      <h:column> 
       <h:outputText value="#{bean.name}"/> 
      </h:column> 

     </h:dataTable> 

apllicationExample

+2

'공공 무효 addtoList (문자열 _name) { 테스트의 또 다른 이름이어야이라고했다 .add (_name); return "./index.xhtml"; }'컴파일해서는 안됩니다 ... 예외는 무엇입니까? –

답변

2

이 시도 :

<h:commandButton 
    value="Post" 
    action="#{bean.addtoList}"/> <!-- no parameter passed --> 
012 :

public String addtoList() { // no parameter 
    test.add(this.name); // add value of bean's property 
    return "./index.xhtml"; 
} 

을하고 facelet에서

요점은 매개 변수없이 addToList 메소드를 갖는 것이며 목록에 추가하는 문자열은 백업 빈의 name 속성 값이어야합니다.

또한 데이터 테이블에서 var의 이름을 백업 빈과 동일하게 지정하지 마십시오. 혼란스럽고 잠재적으로 버그를 유발합니다. 이 같은 것을 사용

<h:dataTable 
    var="it" 
    value="#{bean.test}"> 
    <h:column> 
     <h:outputText value="#{it}" /> 
    </h:column> 
</h:dataTable> 
+0

"요점은 이름 메서드 매개 변수를 목록에 추가하는 것이 아니라 bean의 name 속성을 추가하는 것입니다." 어떻게해야합니까? – JavaNullPointer

+0

@JavaNullPointer 답변을 약간 편집했습니다. 너에게 아직도 불분명 한가? – Natix

+0

감사합니다. 그거였다! – JavaNullPointer

0
<h:dataTable var="beany" 
         value="#{bean.test}"> 
       <h:column> 
        <h:outputText value="#{beany}"/> 
       </h:column> 

</h:dataTable> 

이 확률값은 var="bean" 내 클래스의 같은 이름이 더 beanvar