Javascript动态生成Form表单


主要是在利用Struts中的DispatchAction开发的使用,用于隐藏方法参数。因为在使用DispatchAction的时候,可能在地址栏会暴露后台方法名,所以采用了这种动态生成Form表单,并利用Post方式提交的方法对DispatchAction中的方法参数进行隐藏。这样在超链接或者利用button进行提交的时候可以在一定程序上对后台代码方法进行隐藏。当然这种方法也并不能做到绝对的方法隐藏,只是在某种程度上能起到一定的效果。以下是Javascript代码:

  Js代码   

function forward(methodName){ 
    //create a form 
 var tempForm = document.createElement("form"); 
 tempForm.action="/question.do"; 
 tempForm.method="post"; 
 document.body.appendChild(tempForm); 
  
    //create a submit button  
 var tempInput = document.createElement("input"); 
 tempInput.type="hidden"; 
 tempInput.name="method";  
 tempInput.value=methodName; //the parameter of method in the code of DispatchAction. 
 
 tempForm.appendChild(tempInput); 
    //submit the form 
 tempForm.submit(); 
}

  当然,还可以对以上代码进行改写,在此只提供一种思路


« 
» 
快速导航

Copyright © 2016 phpStudy | 豫ICP备2021030365号-3