[英]How to send request parameters by Ajax to Struts2 action class
I have a Struts2 web application consisting of the following files: 我有一个由以下文件组成的Struts2 Web应用程序:
member.jsp
: 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
: 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
: ControllerSln.java
:
public String editProfile() throws UnsupportedEncodingException {
return SUCCESS;
}
I want to send the strings "aaa" and "bbb" by Ajax to controller.ControllerSln#editProfile()
method. 我想将Ajax发送的字符串“ aaa”和“ bbb”发送到controller.ControllerSln#editProfile()
方法。 How can I achieve it? 我该如何实现?
Your ControllerSln has String attributes which are called str1 and str2.Also their getter and setter must be created by eclipse automaticly. ControllerSln具有String属性,分别称为str1和str2,而且它们的getter和setter必须由eclipse自动创建。 After that , your action has to be like this : http://localhost:8080/project/editprofile.action?str1="+str1+"&str2="+str2; When your action starts,struts will match parameters because their names are the same.. You can see print str1 and str2 on your editProfile() method. 之后,您的操作必须是这样的: http:// localhost:8080 / project / editprofile.action?str1 =“ + str1 +”&str2 =“ + str2;当您的操作开始时,struts将匹配参数,因为它们的名称是相同。.您可以在editProfile()方法上看到print str1和str2。
Give the complete javascript ajax call code if it is simle javascript 如果是简单的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();
}
}
} }
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.