2014-04-17 77 views
0

我在使用JSF從xhtml頁面更新bean變量時遇到問題。我能夠用自定義的值更新Bean變量,但我希望從xhtml頁面中定義的變量中讀取值。JSF - 從xhtml更新Bean變量頁面

XHTML頁面的相關部分看起來是這樣的:

<?xml version="1.0" encoding="UTF-8"?> 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 

<html xmlns="http://www.w3.org/1999/xhtml" 
xmlns:h="http://java.sun.com/jsf/html" 
xmlns:f="http://java.sun.com/jsf/core" 
xmlns:ui="http://java.sun.com/jsf/facelets" 
xmlns:c="http://java.sun.com/jsp/jstl/core" 
xmlns:p="http://primefaces.org/ui"> 

<h:body> 
    <ui:repeat value="#{bean.variable}" var="results"> 
     <c:set target="#{bean}" property="otherVariable" value="results.name" /> 

     Testing if the bean-variable could be read in the loop: #{results.email} 

    </ui:repeat> 
</h:body> 
</html> 

在Bean類我打印otherVariable到標準輸出。每次都是null。當我將Tag中的值替換爲「5」時,將在Bean變量中正確設置該值。

那麼如何從xhtml頁面更新Bean變量?

+0

在列表「結果」中,可能有最後一個帶有屬性名稱的元素爲空或空值。 – Prasad

回答

0

您這裏有兩個問題:

  1. 你試圖設置在C值:設置標籤,而不使用適當的EL: 「#{} results.name」

  2. C:設置標籤進行評估的面孔生命週期中太早是一個UI的內容中可用:repeat標籤

在這種情況下,我通常只是通過這個值作爲參數傳遞給巴需要它的cking bean方法。例如:#{results.doSomething(results.name)}