由於在頁面片段中不能直接添加form標籤,因爲整個頁面片段處於一個大的form標籤之中,而form標籤不能嵌套,有時候我們需要在頁面片段中提交一個post請求,並跳轉到其他URL時可以藉助javascript的dom來生成一個form。
- <af:goButton text="Go Button" id="gb1">
- <af:clientAttribute name="userName" value="aaaaaaaaaa"/>
- <af:clientListener type="click" method="openPostWindow"/>
- </af:goButton>
例如在以上的Go Button中添加一個javascript的 click事件,打開一個post請求,並且將參數userName傳給請求。
javascript代碼如下:
- function openPostWindow(e) {
- var button = e.getSource();
- var newForm = document.createElement("form");
- newForm.id = "postForm";
- newForm.action = "http://showing:7101/PostRequestDemo/postrequestadapterservlet";
- newForm.target = "_blank";
- newForm.method = "post";
- var usernameInput = document.createElement("input");
- usernameInput.name = "userName";
- usernameInput.type = "hidden";
- usernameInput.value = button.getProperty("userName");
- newForm.appendChild(usernameInput);
- document.body.appendChild(newForm);
- newForm.submit();
- document.body.removeChild(newForm);
- }
例如將上述javascript中的http://showing:7101/PostRequestDemo/postrequestadapterservlet改爲http://showing:7101/PostRequestDemo/faces/postrequestadapterservlet
時,就接收不到userName參數了。
轉自:http://blog.csdn.net/ygj26/article/details/8067940