2012-05-31 89 views
2

昨天我發佈了一個關於不得不按兩次按鈕才能使其工作的問題。我收到了很好的幫助,這是stackoverflow的標誌,但問題仍然存在。我將代碼減少到最低限度,問題依然存在。我仔細閱讀了一條BalusC的建議,希望能在窗體中找到一個表單。我當然沒有什麼可以看到的,所以我會發布我的代碼,希望額外的雙眼會看到一些東西。繼續按下按鈕兩次

我有一個模板,我從歡迎(登錄部分)調用。這轉到具有命令按鈕的userInfo。這是我神祕地按下兩次的命令按鈕。在第二次按下命令按鈕將帶我到userPhoto。一切都縮減到最低限度,以便我可以發佈。

master.xthml:

<?xml version="1.0" encoding="UTF-8"?> 
<!-- 
To change this template, choose Tools | Templates 
and open the template in the editor. 
--> 
<!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:f="http://java.sun.com/jsf/core" 
xmlns:ui="http://java.sun.com/jsf/facelets" 
xmlns:h="http://java.sun.com/jsf/html" 
xmlns:p="http://primefaces.org/ui"> 
<h:head> 
<title>Master template</title> 
</h:head> 
<h:body> 
<p:layout fullPage="true" > 
    <p:layoutUnit position="north" size="254"> 
     Top 
    </p:layoutUnit> 

    <p:layoutUnit position="east" size="50" resizable="true"> 
     Hello 
    </p:layoutUnit> 

    <p:layoutUnit position="south" size="30"> 
     south 
    </p:layoutUnit> 

    <p:layoutUnit position="center"> 
     <ui:insert name="AreaOne">Default text</ui:insert> 
    </p:layoutUnit> 
</p:layout> 

</h:body> 
</html> 

welcome1.xhtml:

<?xml version="1.0" encoding="UTF-8"?> 
<!-- 
To change this template, choose Tools | Templates 
and open the template in the editor. 
--> 
<!DOCTYPE html> 
<html xmlns="http://www.w3.org/1999/xhtml" 
xmlns:ui="http://java.sun.com/jsf/facelets" 
xmlns:h="http://java.sun.com/jsf/html" 
xmlns:p="http://primefaces.org/ui"> 
<ui:composition template="master.xhtml"> 

    <ui:define name="AreaOne"> 
     <h:form id="form1"> 
      <p:commandButton type="submit" value="Login" action="userInfo" /> 
     </h:form> 
     <p:messages /> 
    </ui:define> 
</ui:composition> 
</html> 

最後但並非最不重要的userInfo.xhtml與需要被按壓兩次按鈕:

<?xml version="1.0" encoding="UTF-8"?> 
<!-- 
To change this template, choose Tools | Templates 
and open the template in the editor. 
--> 
<!DOCTYPE html> 
<html xmlns="http://www.w3.org/1999/xhtml" 
xmlns:ui="http://java.sun.com/jsf/facelets" 
xmlns:h="http://java.sun.com/jsf/html" 
xmlns:p="http://primefaces.org/ui"> 
<ui:composition template="master.xhtml"> 

    <ui:define name="AreaOne"> 
     <h:form id="formP"> 
      <p:commandButton type="submit" value="photos" action="userPhoto" /> 
     </h:form> 
     <p:messages /> 
    </ui:define> 
</ui:composition> 
</html> 

我沒有看到任何形式嵌套在任何其他形式,但SOMETHING是錯誤的,我不知道什麼。也許BalusC是正確的,它與ajax有關,但我也沒有看到。

感謝您的幫助。 Ilan

我在userPhoto頁面上添加了一個按鈕,該按鈕返回到userInfo頁面。登錄按鈕是唯一一個第一次按下的按鈕。當我使用命令按鈕在userInfo和userPhoto之間來回切換時,它總是需要2次按下。我會顯示userPhoto的中心

<ui:composition template="master.xhtml"> 
    <ui:define name="AreaOne"> 
     <h:form id="form3"> 
      <p:commandButton type="submit" value="home page" action="userInfo" /> 
     </h:form> 
     <p:messages /> 
    </ui:define> 
</ui:composition> 
+0

說,如果您用'默認文本'''(僅用於測試) 在展示中沒有看到他們在佈局中使用了' Daniel

+0

嗨,Daniel。這將使其自動遞歸調用。默認文本是這樣的,只要我不

+0

就會出現SOMETHING。我的意思是說,你只留下了master.xhtml中的表單。 – Daniel

回答

7

你在那裏有一個非常具體的問題。您完全通過ajax進行導航,而不是通過普通的同步請求進行導航,整個視圖將被新視圖替換。新視圖中的表單不再具有視圖狀態,這確實與JSF issue 790有關。由於表單不存在於同一視圖中,因此也無法引用<p:commandButton>update中的表單。

畢竟,不建議通過ajax完全導航。它使你的頁面不可收藏,不可搜索。我建議由

<p:button ... outcome="otherViewId" /> 

這取代一切形式的

<p:commandButton ... action="otherViewId" /> 

將通過正常的同步請求瀏覽和將創建其中任何形式將自己的視圖狀態的新觀點。請注意,<p:button>不需要<h:form>,如有必要,可以省略它。


無關到具體的問題,我也建議把master.xhtml/WEB-INF文件夾,以便在最終用戶永遠無法通過輸入/猜測它的URL在瀏覽器地址欄中提出要求。另請參閱Which XHTML files do I need to put in /WEB-INF and which not?

+0

謝謝。我不知道那個由ajax導航的commandButton。我將不得不搜索一些關於什麼時候通過ajax進行導航以及何時不通過的信息。我注意到它並沒有改變狀態欄中的頁面值,這似乎有點奇怪,但我不知道該怎麼做。按鈕是答案。如果我不強制使用表單標籤也很好。 –

+0

不客氣。 – BalusC

+0

@Balusc同樣的問題在這裏。如果我需要採取行動(在支持bean中處理東西)而不是直接結果。在這種情況下,我得到了兩個點擊問題。我通過編程的方式從動作方法做了一個重定向,而不是返回一個字符串來解決這個問題(不是那種模仿結果=?)。有沒有更簡單的方法來做到這一點?我有一個登錄頁面/表單,也是一個網站菜單欄(在ui:include中)有條件呈現的登出表單。修復之前:從登錄頁面登錄重定向到主頁。用戶需要點擊兩次才能註銷。如果他們先導航到其他頁面,則不需要。 – BillR