现在想从前台的jsp页面中把一个js变量的值传递到后台的java action中(强调是java,不是JSP,ASP页面),现在只会struts2,spring啥的不会。
前台代码:
     <form> 
            DB   Alias:  <input type="text" id="txt1"  onblur="showHint(this.value)" />
      </form>
           <p>Search contents:  <span id="txtHint"></span></p> 
/*---------------------------------------------------------------------------------------------*/
 <script type="text/javascript" > function showHint(str)
{  
  var dbalias=str;
  if (str.length==0)
    { 
        document.getElementById("txtHint").innerHTML="";
        return;
    }
    
  xmlHttp=GetXmlHttpObject();
  if (xmlHttp==null)
    {
    alert ("Your web brower is not support for ajax,please download IE to have a try!!");
    return;
    }
var url="/BACC_Catalog/admin/Catalog-listDirTest";url=url+"?sid="+Math.random();
url=url+"?dbalias="+dbalias;
xmlHttp.onreadystatechange=function(){
     if (xmlHttp.readyState==4){
           alert(xmlHttp.readyState);
           document.getElementById("txtHint").innerHTML=xmlHttp.responseText;
           //alert(xmlHttp.responseText);
     }                          
};
xmlHttp.open("post",url,true);
xmlHttp.send(url); -------------------------------------------------------------------------->在这里想把url通过放入request的方式传给action}function GetXmlHttpObject()
{
  var xmlHttp=null;
  try
    {
    // Firefox, Opera 8.0+, Safari
    xmlHttp=new XMLHttpRequest();
    }
  catch (e)
    {
    // Internet Explorer
    try
      {
      xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
      }
    catch (e)
      {
      xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
      }
    }
  return xmlHttp;
}/*-----------------------------------后台action---------------------------------------*/
下面尝试在request中找到传过来的url地址,但是发现actioncontext里面根本没有传进来url;
1.使用request.getParameter/getAttribute 取得url值,结果为空;
2.下面代码,尝试打印出actioncontext里面request的值,发现也根本没有我要的东西;
 public  String listDirTest() throws Exception {
     
 System.out.println("-----------------------go into dirtest----------------------------------------");
/*  HttpServletRequest req = ServletActionContext.getRequest();
 System.out.println(req.getRequestURI());*/
 Map requestMap = (Map) ActionContext.getContext().get("request");
 System.out.println(requestMap.toString());
 catalogService.listAjax();
                return "cataloglistSucc";
 }  
请教,有啥办法能把ajax定义的值在action中取到,别粘贴jason代码了,不会,看不懂!

解决方案 »

  1.   

    struts方法有很多,一,ServletActionContext.getRequest().getParameter("你传的值")
    二,在方法里面定义你前台参数的成员变量(成员变量名必须和参数一致),并且添加get,set方法,然后就可以直接通过get方法取到
    三,定义一个包含你所传参数的实体类,在action方法里面引入实体类,并添加get,set方法,直接t通过实体类获取成员变量的方式
      

  2.   

    $.ajax({
       type: "POST", 
       url: 
       data:encodeURI(encodeURI(arrayStr)),
       dataType: "json", 
       async:false,
       success: function(){
       } 
    });data: 这个是传递你需要的参数的,如果有中文,使用encodeURI()可以防止乱码,没有中文的话直接写参数就行了。
    后台获取用request.getParameterMap();
      

  3.   

    url都是错的,你确定能跳转么
      

  4.   

    你先判断有没有发送请求到action中
      

  5.   

    xmlhttp.open("POST","action",true);
    xmlhttp.setRequestHeader("Content-type","application/x-www-form-urlencoded");
    xmlhttp.send("fname=Henry&lname=Ford");post请求应该是这样的,open中间是设置跳转路径,设置请求头,然后send里面带参数,&隔开,哪里来的那么多?号