Javascript 如何通过Ajax将请求参数发送到Struts2操作类

Javascript 如何通过Ajax将请求参数发送到Struts2操作类,javascript,ajax,jsp,struts2,Javascript,Ajax,Jsp,Struts2,我有一个Struts2 web应用程序,由以下文件组成: 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); </scr

我有一个Struts2 web应用程序,由以下文件组成:

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> 
ControllerSln.java

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

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

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

如果是simle javascript,请给出完整的javascript ajax调用代码

<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();
            }
        }

函数updateProfile()
{
var-xmlhttp;
if(window.XMLHttpRequest)
{
xmlhttp=新的XMLHttpRequest();
}
其他的
{//IE6、IE5的代码
xmlhttp=新的ActiveXObject(“Microsoft.xmlhttp”);
}
if(typeof xmlhttp==“未定义”)
{
ContentDiv.innerHTML=“无法创建XMLHttp!”;
}
否则{
var str1=“aaa”;
var str2=“bbb”;
变量str='?str1='+str1+'&str2='+str2;
var query='editProfile'+str;
//str1和str2应该位于Controller.ControllerSln,以便从ajax获取数据
open(“GET”,query,true);
xmlhttp.onreadystatechange=函数()
{
if(xmlhttp.readyState==4&&xmlhttp.status==200)
{
document.getElementById(“UpdatedProfile”).innerHTML=xmlhttp.responseText;
//UpdatedProfile div,您希望在其中显示ajax的结果
}
}
xmlhttp.send();
}
}
}

我不明白。为什么你不能在请求中把它们作为参数传递呢?这是我的第一个web项目,我的信息是基本的,我在web上搜索过,但我不能理解它们并将它们应用到我的项目中,所以我在这里问了。但我仍然不明白。你怎么知道他在使用Eclipse?我们正在与镇静的Kurt沟通他知道我在java项目中使用eclipse。谢谢你的解决方案,它工作得很清楚。在上面的解决方案工作得很清楚,Manish,我也保存了你的解决方案。谢谢你的努力。