0

我有一个由以下文件组成的 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> 

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()方法。我怎样才能实现它?

4

2 回答 2

1

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

于 2013-01-06T02:20:06.660 回答
0

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

}

于 2013-01-06T03:41:00.553 回答