I implemented straight AJAX:

I have a element that has a onclick event defined. The onclick event calls a
JavaScript function.

So my element looks like this:

<span onclick=" addToExpandedNodeList(this.id)" />


And the Javascript functions (one is used the get the XmlHttpObject needed
to make AJAX calls):


// Get the XmlHttpObject to use for making AJAX calls
function getXmlHttpObject() {
  var xmlHttp = null;
  try {
  // Firefox, Opera 8.0+, Safari
    xmlHttp = new XMLHttpRequest();
  }
  catch (e) {
  // Internet Explorer
    try {
      xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
    }
    catch (e) {
      try
      {
        xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
      }
      catch (e) {
        alert("Your browser does not support AJAX!");
        return false;
      }
    }
  }
  return xmlHttp;
}

function addToExpandedNodeList(id) {
  var xmlHttpObj = getXmlHttpObject();
  if (xmlHttpObj) {
    var d = new Date();
    var t = d.getTime();
    xmlHttpObj.open("GET","display/display.action?idToChange=" + id +
"&time=" + t);
    xmlHttpObj.onreadystatechange = function() {
      if (xmlHttpObj.readyState == 4) {
        //alert("returned");
      }
    };
    xmlHttpObj.send();
  } else {
    alert("Could not get the XmlHttpObj!!");
  }
}


The XmlHttpObject is used to do the call to the server. I use it to open the
display action (with namespace display, that is why display/display.action)
and pass it an id. You'll also notice that I pass the time across as without
it, the browser does not want to refresh (can't remember which one, think it
was IE).

When the action returns a result, you will handle it with:
    xmlHttpObj.onreadystatechange = function() {
      if (xmlHttpObj.readyState == 4) {
        //alert("returned");
      }
    };

You'll see that I commented a line there where I have an alert. I don't have
to set anything when the action returns the result, so I don't use this.

You might want to think of using the AJAX theme on your components though.
For that you will have to read the tutorials on the pages I sent you
originally.

Jo





-----Original Message-----
From: Johnson nickel [mailto:[EMAIL PROTECTED] 
Sent: 31 January 2008 08:42 AM
To: user@struts.apache.org
Subject: RE: Struts 2 onchange event to get values from databases


Thanks for your quick response.
            I have created Jsp and action and struts.xml. you are mistaken
me(imlazy), i'm not
asking  the code for my requirement.

         I want to display the userdetails, at the time of  Onchange event.
I don't want 
to use javascript. My details are getting from db. If you provide some
examples in AJAX 
than it will be helpful.

Johan Snyman wrote:
> 
> Dude,
> 
> Me thinks you're lazy for not going through the tutorials supplied on the
> documentation site: http://struts.apache.org/2.x/docs/home.html
> 
> You'll have to set up 'n struts.xml file (saved in WEB-INF/classes folder)
> that looks something like this:
> 
> <!DOCTYPE struts PUBLIC
>     "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
>     "http://struts.apache.org/dtds/struts-2.0.dtd";>
> <struts>
>     <!-- <constant name="struts.devMode" value="false" /> -->
>     <constant name="struts.enable.DynamicMethodInvocation" value="false"
> />
>     
>     <package name="mypackage " extends="struts-default">
> 
>     <action name="display" class="com.imlazy.DisplayUserDetails">
>       <result>userdetails,jsp</result>
>     </action>
> </struts>
> 
> You'll have to implement the action class, which has the values you want
> to
> pass to your jsp declared with getters and setter for each of them
> (JavaBean
> style):
> 
> package com.imlazy;
> 
> import ******;
> 
> @SuppressWarnings("serial")
> public class DisplayUserDetails extends ActionSupport {
> 
>    private String userName;
>    private String userAddress;
>    private List<String> userNameList;
>    // etc
> 
>   @Override
>   public String execute() throws Exception {
>     // HERE YOU DO YOUR DB ACCESS AND WHATEVER TO POPULATE YOUR FIELDS
>   }
> 
>   public String getUserName() {
>     return userName;
>   }
> 
>   public void setUsername(String userName) {
>     this.userName = username;
>   }
> 
>   // Other getters and setters
> }
> 
> And finally your jsp that will be displaying the fields:
> 
> <%@ page contentType="text/html;charset=UTF-8" language="java" %>
> <%@ taglib prefix="s" uri="/struts-tags" %>
> 
> <html>
>   <head>
>       <titleUser Details</title>
>   </head>
> 
>   <body>
>     <s:form>
>       <s:combobox label="Users" list="HERE YOU MUST DEFINE THE LIST"
> headerKey="-1" />
>       
>       <s:textfield key="userName">
>       <!-- HERE FOLLOWS YOUR OTHER FIELD -->
>     </s:form>
>   </body>
> </html>
> 
> 
> 
> This is just touching what you need to do and know. Most important is to
> understand. Go through the tutorial, know what you need to get an action
> to
> display what you want to display (choose one user and just display the
> values for that user). Only then start thinking about AJAX and how you
> should be changing what is displayed as the combobox is changed.
> 
> 
> 
> Jo
> 
> 
> 
> 
> 
> -----Original Message-----
> From: Johnson nickel [mailto:[EMAIL PROTECTED] 
> Sent: 31 January 2008 07:35 AM
> To: user@struts.apache.org
> Subject: RE: Struts 2 onchange event to get values from databases
> 
> 
> 
>  I am very new to Struts 2 and Ajax. If u have any samples please send it.

> 
> Johan Snyman wrote:
>> 
>> Hi Johnson (and others),
>> I'm not really as retarded as I sound in the previous mail, just
>> distracted
>> while I was composing the mail. Please contact me if you find it as
>> difficult to follow as I did after reading the first reply...
>> 
>> Jo
>> 
>> 
>> 
>> 
>> -----Original Message-----
>> From: Johan Snyman [mailto:[EMAIL PROTECTED] 
>> Sent: 30 January 2008 03:43 PM
>> To: 'Struts Users Mailing List'
>> Subject: RE: Struts 2 onchange event to get values from databases
>> 
>> Hi Johnson,
>> 
>> I'm no Struts expert but from my experience you have two ways of
>> implementing what you want to achieve.
>> 
>> 1. You can populate your dropdown box and have the fields empty. On your
>> element you can set the onchange to call a javascript function that calls
>> the same action, but passing it the content of your combobox, to do your
>> database lookup on the server side and then pass the info to your
>> userdetails.jsp where you can set your fields.
>> 
>> 2. You can use AJAX to do the call asynchrononously, that is the value is
>> passed to an action on the server and without the values passed back and
>> filled into your fields. Nowadays you'll find plenty of tutorials and
>> examples out there. Check out the following:
>> 
>> http://struts.apache.org/2.x/docs/ajax.html
>> (the official Struts AJAX guide)
>> 
>> http://www.roseindia.net/struts/struts2/struts2ajax/index.shtml
>> (easy login example)
>> 
>>
>
http://www.javaworld.com/javaworld/jw-08-2007/jw-08-ajaxtables.html?fsrc=rss
>> -index
>> (although is a rather tough example to follow if you're new to AJAX)
>> 
>> http://www.planetstruts.org/struts2-showcase/showcase.action
>> (showcase examples)
>> 
>> Hope this helps
>> 
>> Jo
>> 
>> 
>> 
>> -----Original Message-----
>> From: Johnson nickel [mailto:[EMAIL PROTECTED] 
>> Sent: 30 January 2008 03:26 PM
>> To: user@struts.apache.org
>> Subject: Struts 2 onchange event to get values from databases
>> 
>> 
>> Hi All,
>> 
>>          I am using Struts 2 application, i have userdetails.jsp its
>> contains  five text fields.
>> two buttons save and update. In my action i have written save() method
>> and
>> update() method.    
>> 
>>  In my jsp one drop down box its contains users. At the time of onchange
>> event to populate the text fileds values from databases.
>>     can u give me any solutions how to do this? 
>> 
>> -- 
>> View this message in context:
>>
>
http://www.nabble.com/Struts-2-onchange-event-to-get-values-from-databases-t
>> p15182158p15182158.html
>> Sent from the Struts - User mailing list archive at Nabble.com.
>> 
>> 
>> ---------------------------------------------------------------------
>> To unsubscribe, e-mail: [EMAIL PROTECTED]
>> For additional commands, e-mail: [EMAIL PROTECTED]
>> 
>> No virus found in this incoming message.
>> Checked by AVG Free Edition. 
>> Version: 7.5.516 / Virus Database: 269.19.16/1250 - Release Date:
>> 2008/01/29
>> 10:20 PM
>>  
>> 
>> No virus found in this outgoing message.
>> Checked by AVG Free Edition. 
>> Version: 7.5.516 / Virus Database: 269.19.16/1250 - Release Date:
>> 2008/01/29
>> 10:20 PM
>>  
>> 
>> 
>> ---------------------------------------------------------------------
>> To unsubscribe, e-mail: [EMAIL PROTECTED]
>> For additional commands, e-mail: [EMAIL PROTECTED]
>> 
>> No virus found in this incoming message.
>> Checked by AVG Free Edition. 
>> Version: 7.5.516 / Virus Database: 269.19.16/1250 - Release Date:
>> 2008/01/29
>> 10:20 PM
>>  
>> 
>> No virus found in this outgoing message.
>> Checked by AVG Free Edition. 
>> Version: 7.5.516 / Virus Database: 269.19.16/1250 - Release Date:
>> 2008/01/29
>> 10:20 PM
>>  
>> 
>> 
>> ---------------------------------------------------------------------
>> To unsubscribe, e-mail: [EMAIL PROTECTED]
>> For additional commands, e-mail: [EMAIL PROTECTED]
>> 
>> 
>> 
> 
> -- 
> View this message in context:
>
http://www.nabble.com/Struts-2-onchange-event-to-get-values-from-databases-t
> p15182158p15198502.html
> Sent from the Struts - User mailing list archive at Nabble.com.
> 
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: [EMAIL PROTECTED]
> For additional commands, e-mail: [EMAIL PROTECTED]
> 
> No virus found in this incoming message.
> Checked by AVG Free Edition. 
> Version: 7.5.516 / Virus Database: 269.19.16/1250 - Release Date:
> 2008/01/29
> 10:20 PM
>  
> 
> No virus found in this outgoing message.
> Checked by AVG Free Edition. 
> Version: 7.5.516 / Virus Database: 269.19.16/1250 - Release Date:
> 2008/01/29
> 10:20 PM
>  
> 
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: [EMAIL PROTECTED]
> For additional commands, e-mail: [EMAIL PROTECTED]
> 
> 
> 

-- 
View this message in context:
http://www.nabble.com/Struts-2-onchange-event-to-get-values-from-databases-t
p15182158p15199062.html
Sent from the Struts - User mailing list archive at Nabble.com.


---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

No virus found in this incoming message.
Checked by AVG Free Edition. 
Version: 7.5.516 / Virus Database: 269.19.16/1250 - Release Date: 2008/01/29
10:20 PM
 

No virus found in this outgoing message.
Checked by AVG Free Edition. 
Version: 7.5.516 / Virus Database: 269.19.16/1250 - Release Date: 2008/01/29
10:20 PM
 


---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to