2014-10-09 54 views
1

我正在使用RichFaces的排序列表向用戶顯示一個表格自定義Command對象。用戶使用表單創建新的命令,然後將其添加到列表中。下面是orderingList實現:如何使用JSF 2和RichFaces將行對象傳遞給支持bean?

app.xhtml 

<rich:orderingList id="oList" value="#{commandBean.newBatch}" var="com" 
listHeight="300" listWidth="350" converter="commandConverter"> 

<f:facet name="header"> 
    <h:outputText value="New Batch Details" /> 
</f:facet> 
<rich:column width="180"> 
    <f:facet name="header"> 
     <h:outputText value="Command Type" /> 
    </f:facet> 
    <h:outputText value="#{com.commandType}"></h:outputText> 
</rich:column> 
<rich:column> 
    <f:facet name="header"> 
     <h:outputText value="Parameters" /> 
    </f:facet> 
    <h:outputText value="#{com.parameters}"></h:outputText> 
</rich:column> 
<rich:column> 
    <h:commandButton value="Remove #{com.id} : #{com.seqNo}" 
     action="#{commandBean.remove(com.id,com.seqNo)}" 
     onclick="alert('id:#{com.id} seqNo:#{com.seqNo}');"/> 
</rich:column> 

我的麻煩開始了,當我試圖執行一個刪除按鈕,它會發送一個命令的ID和SEQNO的支持bean(CB)從列表中刪除。下面是支持bean:

import javax.faces.bean.ManagedBean; 
import javax.faces.bean.SessionScoped; 

@ManagedBean 
@SessionScoped 
public class CommandBean implements Serializable{ 

private static final long serialVersionUID = 1L; 

private CommandType type; 
private String parameters; 

private List<Command> newBatch = new ArrayList<Command>(); 

private Set<Command> commandSet = new HashSet<Command>(); 

private String msg = "not removed"; 

public CommandType[] getCommandTypes() { 
    return CommandType.values(); 
} 

public void addCommand(CommandType type, String parameters) { 
    newBatch.add(new Command(type, parameters)); 
} 

CommandType getType() { 
    return type; 
} 

void setType(CommandType type) { 
    this.type = type; 
} 

String getParameters() { 
    return parameters; 
} 

void setParameters(String parameters) { 
    this.parameters = parameters; 
} 

public List<Command> getNewBatch() { 
    return newBatch; 
} 

public void setNewBatch(List<Command> newBatch) { 
    this.newBatch = newBatch; 
} 

public Set<Command> getCommandSet() { 
    return commandSet; 
} 

public void setCommandSet(Set<Command> commandSet) { 
    this.commandSet = commandSet; 
} 

String getMsg() { 
    return msg; 
} 

public void remove(Integer id, Integer seqNo) { 
    for(Command c : newBatch) { 
     if(c.getId() == id && c.getSeqNo() == seqNo) { 
      newBatch.remove(c); 
      msg = "removed " + c; 
      return; 
     } 
    } 
    msg = String.format("%d : %d", id,seqNo); 
} 
} 

當命令(COM)的ID和SEQNO通過#{cb.remove(com.id,com.seqNo)}通過它們都是0。我也讀的地方,空值轉換爲0的,所以這可以解釋它。我也嘗試通過#{cb.remove(com)}直接傳遞Command對象,但是當bean嘗試處理它時,命令是null。

我打賭有一個與作用域的東西了,但我太新,JSF弄明白......

UPDATE

我已經消除了衝突的@Named標籤並更新了html以反映bean的新名稱,即commandBean。儘管如此,仍有問題。

+0

這是一個快速驗證的概念,所以它的frankensteined來自一堆例子。有任何鏈接方便的地方,我可以找到*正確*的方式來做到這一點? – 2014-10-09 18:34:10

+0

通過EL將參數傳遞給方法僅適用於EL-3。您應該確定這是您的EL – kolossus 2014-10-11 03:37:34

回答

0

你可以通過這兩個值作爲請求參數:

<h:commandButton ... > 
    <f:param name="id" value="#{com.id}"/> 
    <f:param name="seqNo" value="#{com.seqNo}"/> 
</h:commandButton> 

,並得到在這樣的託管bean檢索它們:

HttpServletRequest request = ((HttpServletRequest)FacesContext.getCurrentInstance().getExternalContext().getRequest()); 
System.out.println(request.getParameter("id")); 
System.out.println(request.getParameter("seqNo")); 
+0

即使它看起來有點笨重,但它工作得很好! – 2014-10-10 15:05:39

+0

@AlexanderAavang你可能想寫一個包裝器方法來獲取請求對象。 :) – 2014-10-14 06:38:50

0

您正嘗試從循環結束後的for循環中使用的變量中獲取一個值。 @action在服務器端正在解析,到@var爲空時爲止。

你可以這樣做:

<a4j:commandButton action="#{commandBean.remove()}" … > 
    <a4j:param assignTo="#{commandBean.idToRemove}" value="#{com.id}"/> 
</a4j:commandButton> 

的A4J:PARAM解析客戶端上的值,單擊該按鈕時,將其發送到服務器。

+0

版本不幸的是,這樣做並不奏效。出於某種原因,remove方法從未被調用過。 – 2014-10-10 15:05:04

+0

@AlexanderAavang你可能需要用'a4j:commandButton'和'execute =「@ this」'來使它正常工作。 – 2014-10-10 15:20:15

相關問題