2013-01-06 58 views
0

我有由下列文件的一个Struts2的Web应用程序:如何通过Ajax来发送请求参数Struts2的action类

member.jsp

<script type="text/javascript">  
    String str1 = "aaa"; 
    String str2 = "bbb"; 

    xmlhttp.open("GET", "http://localhost:8080/project/editprofile.action", true); 
    xmlhttp.send(null); 
</script> 

struts.xml

<action name="editprofile" method="editProfile" class="controller.ControllerSln"> 
    <result name="success" type="stream"> 
     <param name="contentType">text/html</param> 
     <param name="inputName">inputStream</param> 
    </result> 
</action> 

ControllerSln.java

public String editProfile() throws UnsupportedEncodingException { 
    return SUCCESS; 
} 

我想通过Ajax将字符串“aaa”和“bbb”发送到controller.ControllerSln#editProfile()方法。我怎样才能实现它?

+0

我不是你nderstand。为什么不能在请求中将它们作为参数传递? –

+0

这是我的第一个Web项目,我的信息是基本的,我在网上搜索过,但我无法理解,并将它们应用于我的项目,所以我在这里问过。 – selentoptas

+0

但我还是不明白。 –

回答

1

您的ControllerSln具有名为str1和str2的字符串属性。它们的getter和setter必须由eclipse自动创建。 之后,你的动作必须是这样的:http:// localhost:8080/project/editprofile.action?str1 =“+ str1 +”& str2 =“+ str2; 当你的动作开始时,struts会匹配参数,因为他们的名字是一样的.. 你可以看到打印str1和str2的您editProfile()方法。

+1

你怎么知道他在使用Eclipse? –

+0

我们正在与Sedat Kurt沟通,所以他知道我为我的java项目使用了eclipse。谢谢你的解决方案,它清楚地工作。 – selentoptas

0

给出完整的JavaScript调用Ajax代码,如果它是笑着的JavaScript

<script type="text/javascript"> 
     function updateProfile() 
     { 
      var xmlhttp; 
      if (window.XMLHttpRequest) 
      { 
       xmlhttp=new XMLHttpRequest(); 
      } 
      else 
      {// code for IE6, IE5 
       xmlhttp=new ActiveXObject("Microsoft.XMLHTTP"); 
      } 
      if (typeof xmlhttp == "undefined") 
      { 
       ContentDiv.innerHTML="<h1>XMLHttp cannot be created!</h1>"; 
      } 

      else{ 

       var str1 = "aaa"; 
       var str2 = "bbb"; 

       var str='?str1='+str1+'&str2='+str2; 
       var query='editProfile'+str; 
//str1 and str2 should be there at Controller.ControllerSln to fetch data from ajax 
       xmlhttp.open("GET",query,true); 
       xmlhttp.onreadystatechange=function() 
       { 
        if (xmlhttp.readyState==4 && xmlhttp.status==200) 
        { 
         document.getElementById("UpdatedProfile").innerHTML=xmlhttp.responseText; 
         //UpdatedProfile div where u want to display result of ajax 
        } 
       } 

       xmlhttp.send(); 
      } 
     } 

}

+0

在上面的解决方案明确Manish,我也保存了你的解决方案。谢谢你的努力。 – selentoptas

相关问题