ADF頁面片段中藉助javascript使用Form提交Post請求

由於在頁面片段中不能直接添加form標籤,因爲整個頁面片段處於一個大的form標籤之中,而form標籤不能嵌套,有時候我們需要在頁面片段中提交一個post請求,並跳轉到其他URL時可以藉助javascript的dom來生成一個form。

[html] view plaincopy
  1. <af:goButton text="Go Button" id="gb1">  
  2.           <af:clientAttribute name="userName" value="aaaaaaaaaa"/>  
  3.           <af:clientListener type="click" method="openPostWindow"/>  
  4.         </af:goButton>  

例如在以上的Go Button中添加一個javascript的 click事件,打開一個post請求,並且將參數userName傳給請求。

javascript代碼如下:

[javascript] view plaincopy
  1. function openPostWindow(e) {  
  2.     var button = e.getSource();  
  3.     var newForm = document.createElement("form");  
  4.     newForm.id = "postForm";  
  5.     newForm.action = "http://showing:7101/PostRequestDemo/postrequestadapterservlet";  
  6.     newForm.target = "_blank";  
  7.     newForm.method = "post";  
  8.     var usernameInput = document.createElement("input");  
  9.     usernameInput.name = "userName";  
  10.     usernameInput.type = "hidden";  
  11.     usernameInput.value = button.getProperty("userName");  
  12.     newForm.appendChild(usernameInput);  
  13.     document.body.appendChild(newForm);  
  14.     newForm.submit();  
  15.     document.body.removeChild(newForm);  
  16. }  
如此變可以提交post請求了。另外發現在ADF中,當URL地址中爲faces時,不能接收post請求的參數,只能接收get方法提交的參數。

例如將上述javascript中的http://showing:7101/PostRequestDemo/postrequestadapterservlet改爲http://showing:7101/PostRequestDemo/faces/postrequestadapterservlet
時,就接收不到userName參數了。

轉自:http://blog.csdn.net/ygj26/article/details/8067940

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章