File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes HTML, CSS and JavaScript and the fly likes html:select AJAX question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Engineering » HTML, CSS and JavaScript
Bookmark "html:select AJAX question" Watch "html:select AJAX question" New topic
Author

html:select AJAX question

Vicky Pandya
Ranch Hand

Joined: Dec 16, 2004
Posts: 148
In my below code
<html : select property="bussUnits" multiple="true" onchange="retrieveURL('../searchResultCountAction.do');">
<html : options collection="businessList" labelProperty="label" property="value"/>
</html : select>

retrieveURL is my AJAX JS method, which needs to be called onchange or onclick event of html : select (struts tag). What I want to achieve is: to calculate number of results based on user's search criteria. eventually I would need to put onclick or onchange on each control of search page. I have searchForm (actionForm) attached with this action in struts-config file. Problem is, form is not getting populated when I tried to print its values in action class. I believe this is becasue onclick or onchange javascript handler is being called before struts sets up values in actionForm?
Ryan Asleson
Author
Ranch Hand

Joined: Sep 15, 2005
Posts: 39
Are you trying to send form values along with your Ajax request? If so, you need to manually create a query string with the name/value pairs of the form values to send to the server. Then, the query string needs to be part of the URL or posted as part of the Ajax request.

Ajax requests using XMLHttpRequest do not automatically send form values the way a normal browser GET or POST does. You have to send them yourself somehow.


Ryan Asleson<br />Co-Author, <a href="http://www.amazon.com/Foundations-Ajax-Foundation-Ryan-Asleson/dp/1590595823/sr=1-1/qid=1158926537/ref=pd_bbs_1/002-7412154-2924860?ie=UTF8&s=books" target="_blank" rel="nofollow">Foundations of Ajax</a> and <a href="http://www.amazon.com/Pro-Ajax-Java-Frameworks/dp/1590596773/ref=sr_11_1/002-7412154-2924860?ie=UTF8" target="_blank" rel="nofollow">Pro Ajax and Java Frameworks</a><br /><a href="http://taconite.sf.net" target="_blank" rel="nofollow">Taconite</a> Lead Developer
Vicky Pandya
Ranch Hand

Joined: Dec 16, 2004
Posts: 148
Thanks Ryan. I was thinking the same what you mentioned about creating query string. Now my question: Is this a elegant way of achieving this?

I have search page, when user makes selction on each of the criteria I want to display number of results found as count on the same page using AJAX request. Have few multi select selection boxes, drop down, free form text etc. If we build query string with all of these, do you think it is right way of achieving this or any other nice way?
Ryan Asleson
Author
Ranch Hand

Joined: Sep 15, 2005
Posts: 39
You can try searching the Web to see if there are any JavaScript libraries that will build the query string for you. There might be but I'm not sure.

You can also try the Taconite framework (taconite.sf.net). Taconite will automatically build the query string for you; see the docs for examples. Taconite integrates quite well with Struts because of this.
Vicky Pandya
Ranch Hand

Joined: Dec 16, 2004
Posts: 148
Ryan:- I have managed to build query string in javascript and passing to action class. On each onclick event for all UI controls, I am making db query in action class to find out how many records are returned for search criteria and wanting to print on jsp page.

I have following code in processStagesChange() function for AJAX
function processStateChange() {
if (req.readyState == 4) { // Complete
if (req.status == 200) { // OK response

<% if (request.getSession().getAttribute("searchResult") != null){
java.util.List riskResultSize = (java.util.List)request.getSession().getAttribute("searchResult");
if ( riskResultSize != null && riskResultSize.size() > 0 ){ %>
document.getElementById("searchTotal").innerHTML = <%= riskResultSize.size()%>;
<%}


}else{%>

document.getElementById("searchTotal").innerHTML = 0;
<%}%>



} else {
alert("Problem: " + req.statusText);
}
}
}

Results found for your search criteria <span id="searchTotal"> </span>

<html : select property="bussUnits" styleId="buUnits" multiple="true" onchange="retrieveURL('../searchResultCountAction.do');">
<html : options collection="businessList" labelProperty="label" property="value"/>
</html : select>

This doesn't seem to be printing search count correctly immediately. After I click on Search button, correct number gets displayed (search result count).

FYI: In action class its printing correct results with correct result count.

What am I doing wrong?
[ November 17, 2005: Message edited by: Vicky Pandya ]
Vicky Pandya
Ranch Hand

Joined: Dec 16, 2004
Posts: 148
Never mind. I got this working. Removed all session business and also scriptlet from javascript function. Instead creating an xml in struts action and reading in jsp in processStateChange.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: html:select AJAX question