由于在页面片段中不能直接添加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);
}
如此变可以提交post请求了。另外发现在ADF中,当URL地址中为faces时,不能接收post请求的参数,只能接收get方法提交的参数。
例如将上述javascript中的http://showing:7101/PostRequestDemo/postrequestadapterservlet改为http://showing:7101/PostRequestDemo/faces/postrequestadapterservlet
时,就接收不到userName参数了。
                  
                  
                  
                  
                            
本文介绍了一种在页面片段中无法直接使用form标签时,利用JavaScript动态创建form元素以发起POST请求的方法,并附带示例代码。同时指出,在某些情况下,如URL中包含'faces'时,可能无法通过POST方式传递参数。
          
      
          
                
                
                
                
              
                
                
                
                
                
              
                
                
              
            
                  
					6万+
					
被折叠的  条评论
		 为什么被折叠?
		 
		 
		
    
  
    
  
            


            