• 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
  • Tim Cooke
  • Jeanne Boyarsky
  • Liutauras Vilda
Sheriffs:
  • Frank Carver
  • Henry Wong
  • Ron McLeod
Saloon Keepers:
  • Tim Moores
  • Frits Walraven
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Himai Minh

ActionForm returning null values with Ajax call for Action

 
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have created following struts-ajax application

Testing struts

var req;
var which;

function retrieveURL(url)
{
if (window.XMLHttpRequest)
{
// Non-IE browsers
req = new XMLHttpRequest();
req.onreadystatechange = processStateChange;
try
{
req.open(�POST�, url, true);
}
catch (e)
{ alert(e); }

req.send(null);
}
else
{
if (window.ActiveXObject)
{
// IE
req = new ActiveXObject(�Microsoft.XMLHTTP�);
if (req)
{
try
{
req.onreadystatechange = processStateChange;
//var url2 = url + �?i=� + document.getElementById(�id�).value + �&n=� + document.getElementById(�name�).value + �&a=� + document.getElementById(�author�).value + �&p=� + document.getElementById(�price�).value;
req.open(�POST�, url, true);
req.send();
}
catch(e){alert(�Error=�+e);}
}
}
}
}

function processStateChange()
{
if (req.readyState == 4)
{
if (req.status == 200)
{
document.getElementById(�theTable�).innerHTML = req.responseText;
}
else
{
alert(�Problem: � + req.status + � � + req.statusText);
}
}
}

Title:
Author:
Add Book

�����������������������������������������
This one is the BookForm
package pack;

import javax.servlet.http.*;
import org.apache.struts.action.*;

public class BookForm extends ActionForm
{
private String mTitle= null;
private String mAuthor= null;
public String getTitle() { System.out.println(�in getTitle�);return mTitle; }
public void setTitle(String aTitle) { mTitle= aTitle; }
public String getAuthor() { return mAuthor; }
public void setAuthor(String aAuthor) { mAuthor= aAuthor; }
public void reset(ActionMapping aMapping, HttpServletRequest aRequest)
{
mTitle= null;
mAuthor= null;
} // reset()
} // BookForm class
�����������������������������������������-This is BookAction Class
package pack;

import javax.servlet.*;
import javax.servlet.http.*;
import org.apache.struts.action.*;
import java.sql.*;
import java.io.*;

public class BookAction extends Action
{
public ActionForward execute(ActionMapping aMapping, ActionForm aForm, HttpServletRequest request, HttpServletResponse response) throws ServletException
{
BookForm f = (BookForm) aForm;
String title = f.getTitle();
String author = f.getAuthor();
System.out.println(�>>> Saved: � + title + � by � + author);
try
{
String html=�";

Class.forName(�org.gjt.mm.mysql.Driver�);

Connection con=DriverManager.getConnection(�jdbc:mysql://Mac5:3306/person�,�root�,�admin�);

Statement st=con.createStatement();
st.executeUpdate(�insert into newtable values (��+title+��, ��+author+��);�);
//st.executeUpdate(�delete from newtable�);

ResultSet rs=st.executeQuery(�select * from newtable;�);

html = ��;

while(rs.next())
{
title=rs.getString(1);
author=rs.getString(2);
html+=�"+title+�"+author+�";
}
html += ��;

response.setContentType(�text/html�);
PrintWriter out = response.getWriter();
out.println(html);

request.setAttribute(�myhtml�, html);

return null;
}
catch(Exception e)
{System.out.println(e);}
return aMapping.findForward(�saved�);
} // perform()
} // BookAction class
�����������������������������������������-
This is savedOk.jsp

�����������������������������������������-
This is struts-config.xml

�����������������������������������������
Problem is that when i click on Add..(ajax call through open()) Button i am getting null values from BookForm where as if i clicks on submit(normal call to action) it works perfectly.
plz anybody master help me out of this��..

thanks
Manoj(manoj.amrutkar@yahoo.com)
 
Ranch Hand
Posts: 4864
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It's not clear from your code what URL you are using when you make the AJAX call. If you're calling a struts action, though, you must include the parameters in the query string that you want Struts to put in the ActionForm.

For example, since you have setTitle() and setAuthor() methods in your ActionForm, you must pass the parameters "title" and "author" in the query string. You must also make sure that your strings are encoded for use in a URL. for example:

http://myserver.com:8080/myApplication/bookAction.do?author=Ernest+Hemingway&title=for+whom+the+bell+tolls

If you're not passing parameters with the correct names, that's why they're null.

Personally, I don't think making AJAX calls to Struts actions is such a good idea. Struts is a framework made for form submissions, not AJAX calls.

I use a combingation of AJAX calls and regular form submissions in my applications. For form submissions, I use Struts For AJAX calls I use The DWR Framework. It's made especially for AJAX, and it handles the low level code for you. All you have to do is define what Classes and methods you want to call, and DWR provides you with a JavaScript function to call in your JSP that will automatically make an AJAX call to the method on the server.
[ April 27, 2006: Message edited by: Merrill Higginson ]
 
Manoj Amrutkar
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Merrill,
Well thanks a lot for your great and quick response and guess what that hit the bulls eye. I mean just the perfect solution to my problem.
The thing is that i was not passing any values with url. I am thinking that it will just work like simple struts(BookForm will automatically map the parameters in m.jsp title and author).
As such i am very new to Struts. I read some short article on DWR. If u have any details plz forward those( on my mail id if u dont mind).

manoj.amrutkar@yahoo.com

Thanks once again..

Manoj Amrutkar
 
That's a very big dog. I think I want to go home now and hug this tiny ad:
Garden Master Course kickstarter
https://coderanch.com/t/754577/Garden-Master-kickstarter
reply
    Bookmark Topic Watch Topic
  • New Topic