Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Java > Java Open Source > Struts
|
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Struts section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old January 4th, 2006, 09:01 AM
Registered User
 
Join Date: Jan 2006
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via Yahoo to vikassheelgupta
Default Struts with Ajax problem

Hi
i need your help on ajax with struts
what i did is i have a text box and a select box with one button what i want to do is i enter a value in the textbox then after press the button that value shuld be inserted in the data base and also displayed in the select box .
i can able to update the data base and create response like this
************************************************** *************

here is my action class:---
try
  {
     l_CatalougeImpl.addEcardCatagory(idField);// for data base insertion

            String value_idField=idField.trim();//get the value here in the idField variable

            StringBuffer l_stringBuffer= new StringBuffer();
            l_stringBuffer.append("<catagory>");
            l_stringBuffer.append("<CatagoryName>"+idField.tri m()+"</CatagoryName>");
            l_stringBuffer.append("</catagory>");
            addCatagory=true;

            if(addCatagory)
            {
            response.setContentType("text/xml");
            response.setHeader("Cache-Control", "no-cache");
            response.getWriter().write("<Catagories>"+l_string Buffer.toString()+"</Catagories>");
            }
            else
            {
                response.setStatus(HttpServletResponse.SC_NO_CONTE NT);
            }


          }catch(Exception e) {
                return mapping.findForward("error");
            }
          return mapping.findForward("ecards.TestAJAX"); // it will forward to some jsp page as per struts-config.xml mapping
        }
************************************************** *******************************
Here is my JSP code:--
<%@page contentType="text/html" %>
<%@ taglib uri="../WEB-INF/struts-bean.tld" prefix="bean" %>
<%@ taglib uri="../WEB-INF/struts-html.tld" prefix="html" %>
<%@ taglib uri="../WEB-INF/struts-logic.tld" prefix="logic" %>
<html:html>
<head>
<title><bean:message key="Ecards.SendEcardForm.title"/></title>
<script>
var req;
var catagory;
function validate()
{
var idField=document.getElementById("card_name");
var url = "<%=request.getContextPath()%>/ajaximpl.do?id=" +idField.value;
if (window.XMLHttpRequest)
{
req = new XMLHttpRequest();
}
else if (window.ActiveXObject)
{
req = new ActiveXObject("Microsoft.XMLHTTP");
}
req.open("GET", url, true);
req.onreadystatechange = callback;
req.send(null);
}
function callback()
{
if (req.readyState == 4)
{
//alert("Ajax response:"+req.responseText);
if (req.status == 200)
{
alert("update dom");
parseMessages();
}
else
{
alert("Ajax response:"+req.stutsText);
}
}
}
function parseMessages()
{
alert("inside the parsing Message");
var xml=req.responseXML;// Here i got the nulll value
alert("inside while loop");
var catagories=xml.getElementsByTagName("Catagories");
alert("catagories:--"+catagories);
if(catagories !=null)
{
var catagoryId=catagories.childNodes;
if(catagoryId !=null)
{
catagory=catagoryId.getElementsByTagName("Catagory Name");
}
else
{
alert("catagoryId has no value");
}
}
else
{
alert("catagories has no value");
}
}
alert("catagory value is :--"+catagory);
</script>
</head>
<body bgcolor="#FFFFFF">

<Center>
<table border="0" cellpadding="0" cellspacing="0" height='559' width='779'>
<tr>
<td valign="top" height='60'><jsp:include page="/header.jsp"/>
</td>
</tr>
<tr>
<td valign="top" height='439'>

<html:form action="/ajaximpl">
<table>
<tr><td><bean:message key="Ecards.SendEcardForm.catagory" /></td>
<td>

<logic:present name="Array_ecardlist" scope="request">
<html:select property='catagory' >
<html:options collection="Array_ecardlist" property="m_strCategoryId" labelProperty="m_strCategory_Name" />
</html:select>
</logic:present>

</td>
</tr>
<tr><td><bean:message key="Ecards.SendEcardForm.card_name" /></td>
<td>
<html:text property="card_name" />
</td>
</tr>
<tr>
<td>
<html:button property="button" onclick="return validate()"><bean:message key="button.save"/></html:button>
</td>
</tr>
</table>
</tr>
</table>
</html:form>

</td>
</tr>
<tr>
<td valign="top" height='60'><jsp:include page="/header.jsp"/></td>
</tr>
</table>
</Center>


</body>
</html:html>

************************************************** *************
Now my problem is when i call the parseMessages() function in java script then my req.responseXML is null i cant under stand why this happens
can u pls tell me why this happend
i shall be thank ful to you




Warm Regards
Vikas Sheel Gupta
09891424705
 
Old January 5th, 2006, 09:35 AM
Registered User
 
Join Date: Feb 2005
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Default

hi i need some struts sample pgm for beginers which could be easy,
like how to use chkbox,
validattion,
eroors,
bcos i an in struts proj ,i dont have any clue of struts,
plzz give ur hand to finish my project
Ragards
Rakesh
rakiein@yahoo.co.in

 
Old January 10th, 2006, 05:46 AM
Registered User
 
Join Date: Jan 2006
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via Yahoo to vikassheelgupta
Default

this get solved by checking the DOM in java script file

Warm Regards
Vikas Sheel Gupta
09891424705
 
Old February 13th, 2006, 07:38 AM
Registered User
 
Join Date: Feb 2006
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Solution Required for Problem in "Ajax in Struts-based Web Application"
================================================== =====================

In the Web-app, there is table to which multiple rows can be added ..the rows are filled by ajax calls...rows may be deleted as well in which case the deletion is done from client-side javascript call and then refreshing the undeleted rows using the same ajax call...

Now things work fine till the number of rows are limited to five(in mozilla-based browsers) and 3(in IE).....However if the no. of rows exceeds the aforementioned, then ajax-call ("send" part) fails to dispatch the request and the Javascript console displays the following error:

Error: [Exception... "Component returned failure code: 0x80040111
(NS_ERROR_NOT_AVAILABLE) [nsIXMLHttpRequest.status]" nsresult: "0x80040111
(NS_ERROR_NOT_AVAILABLE)" location: "JS frame ::
http://localhost:8080/catissuecore/D...enuSelected=16
:: processRequestOnSpecimenIdChange :: line 370" data: no]
Source File:
...............
Line: 370

Now line 370 is:
if (req.status == 200) {

....Any pointers ??



 
Old September 26th, 2007, 05:25 AM
Registered User
 
Join Date: Sep 2007
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via Yahoo to prateec
Default

Try return mapping.findForward(null); instead of return mapping.findForward("ecards.TestAJAX");


Cheers!
- Pratik
 
Old September 26th, 2007, 05:32 AM
Registered User
 
Join Date: Sep 2007
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via Yahoo to prateec
Default

Well, my problem might be one step ahead of you ...

I am getting the responseXML in reply from the Action class.
But at the end it only prints the XML data on the browser and the previous html content is getting lost!

My Action class look like this:
//Initializing the CountryID.
        String strCountryID = "1";
        if (request.getParameter("cid") != null) { //Empty Request Parameter
            strCountryID = request.getParameter("cid");
        } else {
            if (cForm.getCountryID().length() > 0) { // ActionForm
                strCountryID = cForm.getCountryID();
            }
        }
        //Setting the XML Doucment in request to enable AJAX functionality.
        response.setContentType("text/xml");
        response.setHeader("Cache-Control", "no-cache");
        response.getWriter().write(getStateListXML(stmt, strCountryID).toString());

        //Releasing the resources used.
        stmt = null;
        connection.close();
        connection = null;
        dataSource = null;
        // Moving to next action
        return null;

My JSP look like this:
<%@ taglib uri="/tags/struts-html" prefix="html"%>
<%@ taglib uri="/tags/struts-bean" prefix="bean"%>
<%@ taglib uri="/tags/struts-logic" prefix="logic"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html:html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=Shift_JIS">
<title>AJAX Test</title>
<script type="text/javascript">
    var xmlHttp;
    function ajaxFunction() {
      var field = document.getElementById("countryID");
      var url = "countrylistaction.do?cid=" + encodeURIComponent(field.value);

      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;
            }
        }
      } //End try-catch

      xmlHttp.open("GET", url, true);
      xmlHttp.onreadystatechange = callback;
      xmlHttp.send(null);
    }

    function callback() {
        if (xmlHttp.readyState == 4) { // Complete
            if (xmlHttp.status == 200) { // HttpRequest Success
                // update the HTML DOM
                parseMessage();
            }
        }
    }

    function parseMessage() {
        //alert(xmlHttp.responseXML.xml);
        var state = xmlHttp.responseXML.documentElement;
        setState(state.getElementsByTagName("state")[0].childNodes[1].nodeValue);
    }

    function setState(strState) {
        var cmbStateList = document.getElementById("stateID");
        //var newState = document.createElement('option');
        //newState.text = strState;
        //cmbStateList.add(newState, 0);
        cmbStateList.innerHTML = strState;
    }
</script>
</head>
<body>
<html:form action="countrylistaction">
    Happy AJAXing !<br>
    <html:select property="countryID" onchange="ajaxFunction();">
        <logic:present name="vctCountryList">
            <html:optionsCollection name="vctCountryList" value="countryid" label="countryname" />
        </logic:present>
    </html:select>
    <html:select property="stateID">
        <logic:present name="vctStateList">
            <html:optionsCollection name="vctStateList" value="stateid" label="statename" />
        </logic:present>
    </html:select>
</html:form>
</body>
</html:html>

Cheers!
- Pratik





Similar Threads
Thread Thread Starter Forum Replies Last Post
problem in using struts validator skmmail Struts 0 September 4th, 2007 06:00 AM
problem running struts Miss komal Struts 2 August 2nd, 2006 03:12 AM
problem in struts javed Struts 1 August 2nd, 2006 12:34 AM
Problem with Tomcat/Struts IronStar Apache Tomcat 1 February 16th, 2005 01:23 AM
problem in struts svarupula Apache Tomcat 1 January 24th, 2005 04:42 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.