2017-11-30 29 views
0

c:setui:param을 시도했지만 결국 내가 잘못 생각한 것 같습니다.변수를 JSF에 저장하는 방법/c : set 및 ui : param과 관련된 문제

<h:form id="formTable"> 
    <p:dataTable editable="true" value="#{types.dataList}" var="datalist" style="width:90%; margin-bottom:20px" 
     rows="10" paginator="true" rowsPerPageTemplate="10,25" resizableColumns="true" liveResize="true" tableStyle="width:auto"> 
     <!-- save the old value for the edit action--> 
     <c:set var="oldValue" value="#{datalist.getType()}"/> 
     <!-- column for testing purpose--> 
     <p:column> 
      <h:outputText value="#{oldValue}" /> 
     </p:column> 
     <p:column headerText="Type" sortBy="#{datalist.type}"> 
      <p:cellEditor> 
       <f:facet name="output"> 
        <h:outputText value="#{datalist.type}" /> 
       </f:facet> 
       <f:facet name="input"> 
        <h:inputText value="#{datalist.type}" binding="#{inputUpdateType}" required="true" /> 
       </f:facet> 
      </p:cellEditor> 
     </p:column> 
     <p:column headerText="Date" sortBy="#{datalist.lastModifyDate}"> 
      <h:outputText value="#{datalist.lastModifyDate}"/> 
     </p:column> 
     <p:column headerText="Edit"> 
      <p:rowEditor rendered="#{!datalist.dependenciesFound}"/> 
      <h:graphicImage value="../images/attention.png" rendered="#{datalist.dependenciesFound}" /> 
     </p:column> 
     <p:column headerText="Delete"> 
      <p:commandLink action="#{types.delete(datalist.type)}" rendered="#{!datalist.dependenciesFound}" update=":formTable"> 
       <p:graphicImage url="../images/delete.png" /> 
       <p:confirm header="Confirmation" message="Are you sure?" icon="ui-icon-alert" /> 
      </p:commandLink> 
      <h:outputText value="Not editable because of dependency" rendered="#{datalist.dependenciesFound}"/> 
     </p:column> 
     <!-- We edit/update the value here. This is where the old value is needed--> 
     <p:ajax event="rowEdit" listener="#{types.update(oldValue, inputUpdateType.value)}"/> 
    </p:dataTable> 
    <p:confirmDialog global="true" showEffect="fade" hideEffect="fade"> 
     <p:commandButton value="Yes" type="button" styleClass="ui-confirmdialog-yes" icon="ui-icon-check" /> 
     <p:commandButton value="No" type="button" styleClass="ui-confirmdialog-no" icon="ui-icon-close" /> 
    </p:confirmDialog> 
</h:form> 

나는이 다소 이해할 수있는 희망 :

여기에 중요한 XHTML 부분입니다. 제 프로젝트에서는 Primefaces 6.1을 사용합니다. 이 테이블의 행을 편집하려면 이전 값을 저장하고 하단의 ajax 호출에서 해당 값을 사용해야합니다. 응용 프로그램 또는 오히려 DB 테이블이 요구 (id는이 값입니다) 때문에 나는 이것을 필요합니다. 편집 된 값은 변수 inputUpdateType에 저장됩니다.

내 문제 : 나는 새로운 가치와 충돌 편집을 입력하면

이전 값은 응용 프로그램이 업데이트 방법를 입력 편집 된 값과 동일합니다. 어떤 값이 전달되었는지 확인하기 위해 업데이트 메소드에서 로그 항목을 만들었습니다. c:setui:param과 동일합니다.

내가 뭘 잘못하고 있니? 이 작품을 어떻게 만들 수 있습니까?

편집 : 나는 내 콩에 저장된 이전 값을 얻으려고 방법은 다음과 은이 :

<p:ajax event="rowEdit" listener="#{types.update(types.dataListOld.toArray()[rowIndex].getType(), inputUpdateType.value)}"/> 
+0

https://stackoverflow.com/questions/3342984/jstl-in-jsf2-facelets-makes-sense를 읽어서 시작하십시오. iteratimg h/ui/p 컴포넌트에서 jstl'c : set'을 통해 var를 할당 할 수 없습니다. Thw jstl이 그들 앞에 처리됩니다. – Kukeltje

+0

좋아요, 그래서 거기서 읽을 수있는 것부터 시도해 보니 그것을 할 수 없습니다. 다른 방법이 있습니까? 이제 엔티티 bean을 편집하여 이전 값을 저장할 수 있으며 작동하지만이 솔루션이 마음에 들지 않습니다. – kinglite

+0

컨트롤러에 이전 엔티티의 인스턴스를 유지하고 비교하십시오. 왜 ui 레이어에서 컨트롤/서비스 레이어로이 값을 전달해야합니까? 그것은 이미 (사용 가능) 서버 측이어야합니다. – Kukeltje

답변

0

그래서, 그것은 값이 방법을 실행하기 전에 업데이트됩니다 아약스와 같다. 따라서 이전 값은 서버 측에서 더 이상 사용할 수 없습니다. 불행히도 변수를 JSF에 저장하는 것은 불가능합니다. 왜냐하면 c : set은 JSF 태그보다 먼저 처리되기 때문에 어떤 값도 보유하지 않을 것입니다 (범위를 지정하지 않으면 별칭 역할을하게되고 결국 메서드가 실행되기 전에 업데이트됩니다).).

내가 셀 편집 대신 기본 행 편집 모드의 모드 사용이 모든 것을 작동하게합니다. 내 값 목록에 두 개의 항목이 이유

다음
public void onCellEdit(CellEditEvent event){ 
    //We will call this method on edits made to a cell 
    //Values are stored in a Arraylist. Number of entries depend on how many components there are in the edited cell/facet 
    //Here we have two because in "input" there are two components defined (although both are rendered at the same time). 
    @SuppressWarnings("unchecked") 
    ArrayList<String> oldValue = ((ArrayList<String>)(event.getOldValue())); 
    @SuppressWarnings("unchecked") 
    ArrayList<String> newValue = ((ArrayList<String>)(event.getNewValue())); 

    //In the end both entries have the same value, so we just take the first. 
    if(newValue != null && !newValue.equals(oldValue)) { 
     update(oldValue.get(0), newValue.get(0)); 
     FacesMessage msg = new FacesMessage(FacesMessage.SEVERITY_INFO, "Cell Changed", "Old: " + oldValue.get(0) + ", New:" + newValue.get(0)); 
     FacesContext.getCurrentInstance().addMessage(null, msg); 
    }  
} 

이다 ("입력"의 두 가지 구성 요소) :

<f:facet name="input"> 
     <h:inputText value="#{datalist.type}" binding="#{inputUpdateType}" required="true" rendered="#{!datalist.dependenciesFound}"/> 
     <h:outputText value="#{datalist.type}" rendered="#{datalist.dependenciesFound}"/> 
    </f:facet> 
</p:cellEditor> 

이것으로 나는 이전 및 새 값을 얻을 수 있어요