Calling struts action from $ajax
Hi Folks
This is my first experiment with jquery and ajax
What i want to achieve is to call an action from the jsp using jquery and supposedly an ajax request . Nothing seems to be happening . What am i missing
The JSP and Action class code can be viewed below
Here is my jsp snippet
<html>
<head>
<script type="text/javascript" src="jquery-1.2.6.min.js"></script>
<script type="text/javascript">
$(document).ready(function()
{
//hide the all of the element with class *_body
$(".about_this_rfx_body").hide();
$(".attachment_body").hide();
$(".questionnaire_body").hide();
$(".iteam_for_quotes_body").hide();
//toggle the component for About this RFx body
$(function(){
$('about_this_rfx a').click(function(){
$.ajax({
type:"GET",
dataType:"xml",
url:"http://localhost:9080/enterprize-sourcing/RFxForXML.do?rfxId=487085942",
error: function(){
alert("error, while hitting the action class");
},
success: function(xml){
alert("im inside success");
$(xml).find('rfx').each(function() {
var title = $(this).find('').text()
$('<li></li>').html(title).appendTo('#about_this_rfx ol');
}
); //close each
}
});
}); // close click
}); // close $
$(".about_this_rfx").click(function()
{
});
//toggle the component for Attachments body
$(".attachment_head").click(function()
{
$(this).next(".attachment_body").slideToggle(300);
});
//toggle the component for Questionnaire body
$(".questionnaire_head").click(function()
{
$(this).next(".questionnaire_body").slideToggle(300);
});
//toggle the component for Items for Quotes body
$(".iteam_for_quotes_head").click(function()
{
$(this).next(".iteam_for_quotes_body").slideToggle(300);
});
});
</script>
</head>
<body>
<b>Review and Submit</b>
<div id="about_this_rfx">
<a href="#">About this RFx</a>
<ol></ol>
</div>
</body>
</html>
and here is the action class
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.DownloadAction;
public class RFxForXMLAction extends DownloadAction {
@Override
protected StreamInfo getStreamInfo(ActionMapping actionMapping, ActionForm actionForm,
HttpServletRequest request, HttpServletResponse response) throws Exception {
CommonsHttpClientUtil httpClientUtil = new CommonsHttpClientUtil();
HttpCallResponse httpCallResponse = httpClientUtil.get(getUrl(request));
String xmlString = httpCallResponse.payloadToString();
String contentType = "text/xml";
return new ByteArrayStreamInfo(contentType,xmlString.getBytes());
}
private String getUrl(javax.servlet.http.HttpServletRequest request){
SourcingRestWebPropertyUtil propertyUtil = SourcingRestWebPropertyUtil.getInstance();
StringBuilder uri = new StringBuilder(propertyUtil.getProperty("internal.rest.url"));
long rfxId = Long.parseLong(request.getParameter("rfxId"));
uri.append("/rfxs/").append(rfxId);
uri.append("?username");
uri.append("=");
uri.append(SourcingRestWebUtil.getClientUserId(request));
return uri.toString();
}
protected class ByteArrayStreamInfo implements StreamInfo {
protected String contentType;
protected byte[] bytes;
public ByteArrayStreamInfo(String contentType, byte[] bytes) {
this.contentType = contentType;
this.bytes = bytes;
}
public String getContentType() {
return contentType;
}
public InputStream getInputStream() throws IOException {
return new ByteArrayInputStream(bytes);
}
}
}
Cheers
n4v33n