• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

How to call action class from Ajax

 
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Sheriff
Posts: 67637
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 859
IBM DB2 Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I agree with Bear,

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

WP
 
venkat penmatsa
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Without subsidies, chem-ag food costs four times more than organic. Or this tiny ad:
free, earth-friendly heat - a kickstarter for putting coin in your pocket while saving the earth
https://coderanch.com/t/751654/free-earth-friendly-heat-kickstarter
reply
    Bookmark Topic Watch Topic
  • New Topic