aspose file tools*
The moose likes Struts and the fly likes How to call action class from Ajax Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "How to call action class from Ajax" Watch "How to call action class from Ajax" New topic
Author

How to call action class from Ajax

venkat penmatsa
Greenhorn

Joined: Jul 23, 2011
Posts: 17
Hi All,

I am new to ajax I am trying to call an action class from AJAX but i my code is not working please check my code and do the needful
i created a drop down which have value ,when i change the value then it should go to the action class and execute the logic.

<script type="text/javascript">
function selectDropdown(){
var xmlhttp;
xmlhttp=new XMLHttpRequest();

alert("xmlhttp :"+xmlhttp);
var a=document.getElementById("dropdown").value;
var url="adddropdown.do?a="+a;
xmlhttp.open("GET",url,true);
xmlhttp.onreadystatechange=function(){
if(this.readystate==4){
document.getElementById("dropdown1").innerHTML=xmlHttp.responseText;

}
}
}

</script>

<body>
<form name="form1" action="/adddropdown">
<table>
<tr>
<td>
<h1>Select</h1>
</td>
<td><select id="dropdown" name="dropdown"
onchange="selectDropdown()">
<option value="India">India</option>
<option value="US">US</option>
<option value="UK">UK</option>
<option value="Japan">Japan</option>
<option value="China">China</option>
</select></td>
</tr>
<tr>
<td>
<h1>Select submenu</h1>
</td>
<td><select id="dropdown1" name="dropdown1">
</select></td>
</tr>
</table>
</form>
</body>

struts-config.xml file

<action path="/adddropdown" type="com.Action.Questions">


action class


public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) {

System.out.println("sample");
String drvalue=request.getParameter("a");
System.out.println("drvalue :"+drvalue);
return mapping.findForward("questions");

}



In action class i am printing some sample value and request object but none of the values are printed. please help me in resolving my problem



Thanks in advance,
venkat.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60997
    
  65

Your URL is page relative. It should be server-relative, starting with the context path.

Also, Please be sure to use code tags when posting code to the forums. Unformatted or unindented code is extremely hard to read and many people that might be able to help you will just move along to posts that are easier to read. Please click this link ⇒ UseCodeTags ⇐ for more information. Properly indented and formatted code greatly increases the probability that your question will get quicker, better answers.

And why aren't you using jQuery to do your Ajax? Doing it "by hand" is fragile and error-prone.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
William P O'Sullivan
Ranch Hand

Joined: Mar 28, 2012
Posts: 859

I agree with Bear,

Use a tried and trusted framework. Do not reinvent the wheel!

WP
venkat penmatsa
Greenhorn

Joined: Jul 23, 2011
Posts: 17
Bear Bibeault wrote:Your URL is page relative. It should be server-relative, starting with the context path.

Also, Please be sure to use code tags when posting code to the forums. Unformatted or unindented code is extremely hard to read and many people that might be able to help you will just move along to posts that are easier to read. Please click this link ⇒ UseCodeTags ⇐ for more information. Properly indented and formatted code greatly increases the probability that your question will get quicker, better answers.

And why aren't you using jQuery to do your Ajax? Doing it "by hand" is fragile and error-prone.



Hi Bear,

Thanks for the replay , I am new to jquery and ajax could you please provide an example or tutorial link for implemeting jquery and ajax in struts.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How to call action class from Ajax