You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@struts.apache.org by Anjib Mulepati <an...@hotmail.com> on 2010/12/02 17:44:02 UTC

Struts 1 and AJAX

Struts 1.3.8

I am writing and application where end user insert the email address  
and get inserted in table shown on web page after successfully validation.

For that I am doing following:

1. Get email from user in text box.
2. When user press add button AJAX post is called, email get validated 
in Struts Action execute(), and get back the email to view again to 
display on table.

Now I am having couple of problems
1. My record in the table disappear if page is refresh.
2. And If some of my email is incorrect I don't want that to be get back 
to view again. I just want to show a error.

How can i solve these problem?

Anjib


---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org


Re: Struts 1 and AJAX

Posted by Dave Newton <da...@gmail.com>.
On Thu, Dec 2, 2010 at 12:33 PM, Anjib Mulepati wrote:

> 1. By refresh I mean when user press F5 all content lost.
>

That is the nature of Ajax-heavy applications. You need to store a means of
retrieving the object/data you're currently working with. One place to store
it is in the session.

Dave

Re: Struts 1 and AJAX

Posted by Anjib Mulepati <an...@hotmail.com>.
1. By refresh I mean when user press F5 all content lost.
2. I will try second one.

Anjib

On 12/2/2010 11:58 AM, Dave Newton wrote:
> On Thu, Dec 2, 2010 at 11:44 AM, Anjib Mulepati wrote:
>
>> 1. My record in the table disappear if page is refresh.
>>
> Don't refresh? Otherwise you need a mechanism to "remember" what you're
> working with, like the session.
>
>
>> 2. And If some of my email is incorrect I don't want that to be get back to
>> view again. I just want to show a error.
>>
> Look at the results of the AJAX call. If it's an error, display a message.
> Should be trivial if using JSON, only marginally less trivial if you're not.
>
> Dave
>


---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org


RE: Struts 1 and AJAX

Posted by "Biesbrock, Kevin" <Bi...@aoins.com>.
> Although personally I'd still use JSON; easier.

> Dave

Yep.

So a successful response could look like this:

response = {
	validEmail:true,
	emailAddress:'user@struts.apache.org'
}

and a failed response could look like this:
response = {
	validEmail:false,
	emailAddress:'user@struts.apache.org'
}

Then your post could look like this:
$.post(
	"validateEmail.do",
	{sendValue: email},
	function(data){
		if (data.validEmail) {
			addToTable(data.emailAddress); //add email to
table
		} else {
			displayErrorMessage("Invalid Email.  Please try
again.");
		}
	}
);

That is a very basic way of doing it.  The JSON response coming in is
very library-specific.  It appears you could be using jquery?  If that's
the case, then this should work as is as long as your set the response
header to be 'text/json'.  :)

Hope that helps,

Beez


---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org


RE: Struts 1 and AJAX

Posted by "Biesbrock, Kevin" <Bi...@aoins.com>.
> From: Anjib Mulepati [mailto:anjibcs@hotmail.com] 

> It still add blank row in table when invalid.

The 'returnValue' variable is actually a jQuery object.  I think you
might have to do like returnValue.data or something to get the actual
response text out of it. 


Beez


---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org


Re: Struts 1 and AJAX

Posted by Anjib Mulepati <an...@hotmail.com>.
It still add blank row in table when invalid.

Anjib
On 12/3/2010 11:36 AM, Biesbrock, Kevin wrote:
> Gosh, you're so close.
>
> If you change this method:
> function fnClickAddEmail() {
>       var email = $("input#email").val();
>       $.post(
>           "validateEmail.do",
>           {sendValue: email},
>           function(returnValue){   //Does this function called even my
> email is invalid? Am I getting email.jsp page in "returnValue"?
>               addToTable(returnValue);
>           },
>           "text"
>       );
> }
>
> To this:
> function fnClickAddEmail() {
>       var email = $("input#email").val();
>       $.post(
>           "validateEmail.do",
>           {sendValue: email},
>           function(returnValue){
> 		if (returnValue == "") {
> 			// Show Error Message
> 		} else {
> 			addToTable(returnValue);
> 		}
>           },
>           "text"
>       );
> }
>
> This should get your implementation working for you.  You're still not
> utilizing JSON, but with the modified method it should work for you.
>
>
> Beez
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
> For additional commands, e-mail: user-help@struts.apache.org
>
>
>


---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org


Re: Struts 1 and AJAX

Posted by Dave Newton <da...@gmail.com>.
Have you actually checked the object you're getting back to see (a) what it
is, and (b) how to access and/or manipulate it? These are all JavaScript
questions, not Struts, at least as far as I can tell.

Dave

On Fri, Dec 3, 2010 at 12:05 PM, Anjib Mulepati <an...@hotmail.com> wrote:

> I make following change to test but still same problem
>
>
> function fnClickAddEmail() {
>    var email = $("input#email").val();
>    $.post(
>        "validateEmail.do",
>        {sendValue: email},
>        function(returnValue){
>           if(returnValue == "-1"){  //Changed here
>                displayError(email);
>            }else{
>                addToTable(returnValue);
>            }
>        },
>        "text"
>    );
> }
>
> And
>
>
> <% if(request.getAttribute("validEmail").equals("true")){ %>
> <%= request.getAttribute("emailAddress") %>
> <% }else{ %>
> <%= -1 %> // Changed here
> <%} %>
>
>
>
> Anjib
>
>
>
>
> On 12/3/2010 11:36 AM, Biesbrock, Kevin wrote:
>
>> Gosh, you're so close.
>>
>> If you change this method:
>> function fnClickAddEmail() {
>>      var email = $("input#email").val();
>>      $.post(
>>          "validateEmail.do",
>>          {sendValue: email},
>>          function(returnValue){   //Does this function called even my
>> email is invalid? Am I getting email.jsp page in "returnValue"?
>>              addToTable(returnValue);
>>          },
>>          "text"
>>      );
>> }
>>
>> To this:
>> function fnClickAddEmail() {
>>      var email = $("input#email").val();
>>      $.post(
>>          "validateEmail.do",
>>          {sendValue: email},
>>          function(returnValue){
>>                if (returnValue == "") {
>>                        // Show Error Message
>>                } else {
>>                        addToTable(returnValue);
>>                }
>>          },
>>          "text"
>>      );
>> }
>>
>> This should get your implementation working for you.  You're still not
>> utilizing JSON, but with the modified method it should work for you.
>>
>>
>> Beez
>>
>>
>> ---------------------------------------------------------------------
>> To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
>> For additional commands, e-mail: user-help@struts.apache.org
>>
>>
>>
>>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
> For additional commands, e-mail: user-help@struts.apache.org
>
>

Re: Struts 1 and AJAX

Posted by Anjib Mulepati <an...@hotmail.com>.
I make following change to test but still same problem

function fnClickAddEmail() {
     var email = $("input#email").val();
     $.post(
         "validateEmail.do",
         {sendValue: email},
         function(returnValue){
            if(returnValue == "-1"){  //Changed here
                 displayError(email);
             }else{
                 addToTable(returnValue);
             }
         },
         "text"
     );
}

And

<% if(request.getAttribute("validEmail").equals("true")){ %>
<%= request.getAttribute("emailAddress") %>
<% }else{ %>
<%= -1 %> // Changed here
<%} %>


Anjib




On 12/3/2010 11:36 AM, Biesbrock, Kevin wrote:
> Gosh, you're so close.
>
> If you change this method:
> function fnClickAddEmail() {
>       var email = $("input#email").val();
>       $.post(
>           "validateEmail.do",
>           {sendValue: email},
>           function(returnValue){   //Does this function called even my
> email is invalid? Am I getting email.jsp page in "returnValue"?
>               addToTable(returnValue);
>           },
>           "text"
>       );
> }
>
> To this:
> function fnClickAddEmail() {
>       var email = $("input#email").val();
>       $.post(
>           "validateEmail.do",
>           {sendValue: email},
>           function(returnValue){
> 		if (returnValue == "") {
> 			// Show Error Message
> 		} else {
> 			addToTable(returnValue);
> 		}
>           },
>           "text"
>       );
> }
>
> This should get your implementation working for you.  You're still not
> utilizing JSON, but with the modified method it should work for you.
>
>
> Beez
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
> For additional commands, e-mail: user-help@struts.apache.org
>
>
>


---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org


RE: Struts 1 and AJAX

Posted by "Biesbrock, Kevin" <Bi...@aoins.com>.
Gosh, you're so close.

If you change this method:
function fnClickAddEmail() {
     var email = $("input#email").val();
     $.post(
         "validateEmail.do",
         {sendValue: email},
         function(returnValue){   //Does this function called even my 
email is invalid? Am I getting email.jsp page in "returnValue"?
             addToTable(returnValue);
         },
         "text"
     );
}

To this:
function fnClickAddEmail() {
     var email = $("input#email").val();
     $.post(
         "validateEmail.do",
         {sendValue: email},
         function(returnValue){
		if (returnValue == "") {
			// Show Error Message
		} else {
			addToTable(returnValue);
		}
         },
         "text"
     );
}

This should get your implementation working for you.  You're still not
utilizing JSON, but with the modified method it should work for you.


Beez


---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org


Re: Struts 1 and AJAX

Posted by Anjib Mulepati <an...@hotmail.com>.
To make my point clear I am adding detail code so someone can help me.

Description:
JSP page display the text box, add button and blank table to the user (I 
am using DataTable plugin). User enter the email and press add. This 
email is send to server with AJAX call. In server in Struts action class 
email is validated and set appropriate status for "validEmail" variable. 
Which in turn is put in request along with email to make it available.

Problem:
Now after validation. I am mapping a view page to show either email or 
blank testing the request object variable "validEmail". When doing so in 
my table row is populated with email (when valid) as well as blank (when 
invalid).
I couldn't find a way to make decision inside " addToTable()" function.

Note: I try to put comment on the code itself where I am confuse.

1. JSP Page (index.jsp)
-------------------------------

<html>
<head>
<link rel="stylesheet" href="css/dynamic_css.css" media="screen" 
type="text/css" />
<link rel="stylesheet" href="css/form.css" media="screen" type="text/css" />

<script type="text/javascript" src="js/enhance.js"></script>
<script type="text/javascript">
             enhance({
                 loadScripts: [
                     'js/jquery.js',
                     'js/jquery.dataTables.js',      //This is the 
DataTable plugin I am using to add valid emails.
                     'js/onload.Grouping.js'        // My JS file to do 
all other operations.
                 ],
                 loadStyles: [
                     'css/enhanced.css',
                     'css/table.css'
                 ],
                 forcePassText: "",
                 forceFailText: ""
             });
</script>

<title>:: <bean:message key="title.login" /> ::</title>
</head>

<body>
<div id="everyoneneedsawrapper">

<div id="error" style="float:left; display: block;">
<span id="errorMessage" style="font-weight: bold; color: #b80000;" 
aria-labelledby="errors" aria-live="assertive">
<html:errors />
</span>
</div>

<div id="topOptions" style="margin-left: 13%; width: 87%; clear: both;">
<table>
<tr valign="top">
<td width="49%" >
<form action="/"  name="addEmail">
<label for="email">
<bean:message key="option1.msg" />
</label><br/>
<input type="text" id="email">
<input title="Add Email" type="button" value="Add" 
onclick="fnClickAddEmail();" >
</form>

</td>
</tr>
</table>
</div>
<br />
<hr style="margin-left: 13%;">
<br/>

             //Table area defined to populate the validate emails after 
verification on the server.

<div id="emailList" style="margin-left: 13%; width: 87%; clear: both;" >
<table id="emailListTable" class="display" width="100%" border="0" >
<caption class="tableCaption">
<H1><bean:message key="msg.emailList" /></H1>
</caption>
<thead>
<tr>
<th title="Email Address" id="emailAddress" class="header">
<a href="#" style="color: black;">
<bean:message key="label.emailAddress" />
</a>
</th>
<th title="Delete" id="delete" class="header">
<center><a href="#" style="color: black;">
<bean:message key="label.deleteEmail" />
</a></center>
</th>
</tr>
</thead>

<tbody>

</tbody>
</table>
</div>
</div>
</body>
</html>

2. JavaScript File (onload.Grouping.js)
----------------------------------------------------

/* Global var for counter */
var id = 1;

$(document).ready(function() {
    //Initialized DataTable plugin
     var oTable = $('#emailListTable').dataTable();
});

function fnClickAddEmail() {
     var email = $("input#email").val();
     $.post(
         "validateEmail.do",
         {sendValue: email},
         function(returnValue){   //Does this function called even my 
email is invalid? Am I getting email.jsp page in "returnValue"?
             addToTable(returnValue);
         },
         "text"
     );
}

function addToTable(email){
    //When this will be called? After displaying the result view?
    // How to put consition here to dispaly email when valid and error 
on div area when email is invalid.

     //Add to DataTable table row using its API
     var html = '<center><input type="checkbox" id="cb' + id + '" value="'
             + email + '" /></center>';
         $('#emailListTable').dataTable().fnAddData( [
             email,
             html ] );
         id++;
}

3. Action Class (ValidateEmailAction.java)
---------------------------------------------------------

public class ValidateEmailAction extends org.apache.struts.action.Action {

     /* forward name="success" path="" */
     private static final String SUCCESS = "success";
     private static final String FAILURE = "failure";

     @Override
     public ActionForward execute(ActionMapping mapping, ActionForm form,
             HttpServletRequest request, HttpServletResponse response)
             throws Exception {

         String email = (String) request.getParameter("sendValue");
         request.setAttribute("emailAddress", email);
         EmailInspector insp = new EmailInspector();

         // set the level of validation
         insp.setEmailInspectionLevel(EmailInspector.SYNTAX_VALIDATION);

         // perform validation of email address
         try {
             insp.validate(email);
             request.setAttribute("validEmail", "true");
         } catch (Exception e) {
             request.setAttribute("validEmail", "false");
         }
         return mapping.findForward(SUCCESS); // Do we need to send this?
     }
}

4. Struts config file (struts-config.xml)
----------------------------------------------------

<action path="/validateEmail" type="auth.actions.ValidateEmailAction" >
<forward name="success" path="email.jsp" />
</action>

5. View Page (email.jsp)
--------------------------------
//Do we really need this page? Can't we just send the result (validEmail 
only) to the function.
<% if(request.getAttribute("validEmail").equals("true")){ %>
<%= request.getAttribute("emailAddress") %>
<% }else{} %>

I will be glad to explain further if I am not clear.

Thanks
Anjib




---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org


RE: Struts 1 and AJAX

Posted by "Biesbrock, Kevin" <Bi...@aoins.com>.
You *need* a means of telling your application whether the validation
was successful or a failure.  All you're currently doing is reiterating
to your server of a successful or failed validation...which it already
knows by virtue of your validation method.  As Dave and I have both
suggested, a JSON response is probably the best and simplest method to
tell your client what's going on.

I didn't read through this, but looks promising for your use case:
http://www.selfcontained.us/2008/08/29/teaching-an-old-framework-new-tri
cks/ 

...oooor:
http://www.google.com/search?q=struts1+json&ie=utf-8&oe=utf-8&aq=t&rls=o
rg.mozilla:en-US:official&client=firefox-a


Beez
r 5347 

-----Original Message-----
From: Anjib Mulepati [mailto:anjibcs@hotmail.com] 
Sent: Thursday, December 02, 2010 3:34 PM
To: Struts Users Mailing List
Subject: Re: Struts 1 and AJAX

I am trying with simple things but result are not as I expected.

After call to Struts action I get either SUCCESS or FAILURE view. And
these same page are displayed in the row itself. I was planning to
display email if it is valid(SUCCESS) or error message on page (in div
area fro error) if the email is invalid.

As shown before both view are mapped to different page. And in AJAX call
upon success it is calling function to add row.

I think these are two conflicting or equivalent steps I am taking. So I
am missing some point.

Can anyone tell me how can I solve this issue?

Anjib

On 12/2/2010 2:09 PM, Dave Newton wrote:
> You *could* do that, or do it in the view layer, or just serialize a 
> rational JSON object to the response and skip all the other issues, 
> and make things easier.
>
> Dave
>
> On Thu, Dec 2, 2010 at 2:06 PM, Anjib Mulepati<an...@hotmail.com>
wrote:
>
>> So do I need to map two different view in action class as follow
>>
>>
>> {
>>         String email = (String) request.getParameter("sendValue");
>>         // perform validation of email address
>>         try {
>>             insp.validate(email);
>>            //iF EMAIL IS VALID it return email and map email.jsp page
>>             request.setAttribute("returnValue",  email);
>>             return mapping.findForward(SUCCESS); //View 1
>>
>>         } catch (Exception e) {
>>            //If email is invalid it retun null with error message and

>> map to blank page.
>>             request.setAttribute("returnValue",  "-1");
>>
>>             ActionErrors errors = new ActionErrors();
>>             errors.add("error", new ActionMessage(e.getMessage(),
false));
>>             this.saveErrors(request, errors);
>>             return mapping.findForward(FAILURE); //View 2
>>
>>         }
>> }
>> On 12/2/2010 1:42 PM, Dave Newton wrote:
>>
>>> On Thu, Dec 2, 2010 at 1:25 PM, Anjib Mulepati wrote:
>>>
>>>   But problem I am having is I can't tap the condition where email 
>>> is
>>>> invalid.
>>>>
>>>> $.post(
>>>>         "validateEmail.do",
>>>>         {sendValue: email},
>>>>         function(returnValue){
>>>>             addToTable(returnValue); //add email to table
>>>>
>>> "Tap it" in that function: isn't the returnValue either the email 
>>> (if
>>> successful) or nothing (if not)?
>>>
>>> Although personally I'd still use JSON; easier.
>>>
>>> Dave
>>>
>>>
>> ---------------------------------------------------------------------
>> To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
>> For additional commands, e-mail: user-help@struts.apache.org
>>
>>


---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org




---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org


Re: Struts 1 and AJAX

Posted by Anjib Mulepati <an...@hotmail.com>.
I am trying with simple things but result are not as I expected.

After call to Struts action I get either SUCCESS or FAILURE view. And 
these same page are displayed in the row itself. I was planning to 
display email if it is valid(SUCCESS) or error message on page (in div 
area fro error) if the email is invalid.

As shown before both view are mapped to different page. And in AJAX 
call  upon success it is calling function to add row.

I think these are two conflicting or equivalent steps I am taking. So I 
am missing some point.

Can anyone tell me how can I solve this issue?

Anjib

On 12/2/2010 2:09 PM, Dave Newton wrote:
> You *could* do that, or do it in the view layer, or just serialize a
> rational JSON object to the response and skip all the other issues, and make
> things easier.
>
> Dave
>
> On Thu, Dec 2, 2010 at 2:06 PM, Anjib Mulepati<an...@hotmail.com>  wrote:
>
>> So do I need to map two different view in action class as follow
>>
>>
>> {
>>         String email = (String) request.getParameter("sendValue");
>>         // perform validation of email address
>>         try {
>>             insp.validate(email);
>>            //iF EMAIL IS VALID it return email and map email.jsp page
>>             request.setAttribute("returnValue",  email);
>>             return mapping.findForward(SUCCESS); //View 1
>>
>>         } catch (Exception e) {
>>            //If email is invalid it retun null with error message and map to
>> blank page.
>>             request.setAttribute("returnValue",  "-1");
>>
>>             ActionErrors errors = new ActionErrors();
>>             errors.add("error", new ActionMessage(e.getMessage(), false));
>>             this.saveErrors(request, errors);
>>             return mapping.findForward(FAILURE); //View 2
>>
>>         }
>> }
>> On 12/2/2010 1:42 PM, Dave Newton wrote:
>>
>>> On Thu, Dec 2, 2010 at 1:25 PM, Anjib Mulepati wrote:
>>>
>>>   But problem I am having is I can't tap the condition where email is
>>>> invalid.
>>>>
>>>> $.post(
>>>>         "validateEmail.do",
>>>>         {sendValue: email},
>>>>         function(returnValue){
>>>>             addToTable(returnValue); //add email to table
>>>>
>>> "Tap it" in that function: isn't the returnValue either the email (if
>>> successful) or nothing (if not)?
>>>
>>> Although personally I'd still use JSON; easier.
>>>
>>> Dave
>>>
>>>
>> ---------------------------------------------------------------------
>> To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
>> For additional commands, e-mail: user-help@struts.apache.org
>>
>>


---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org


Re: Struts 1 and AJAX

Posted by Dave Newton <da...@gmail.com>.
You *could* do that, or do it in the view layer, or just serialize a
rational JSON object to the response and skip all the other issues, and make
things easier.

Dave

On Thu, Dec 2, 2010 at 2:06 PM, Anjib Mulepati <an...@hotmail.com> wrote:

> So do I need to map two different view in action class as follow
>
>
> {
>        String email = (String) request.getParameter("sendValue");
>        // perform validation of email address
>        try {
>            insp.validate(email);
>           //iF EMAIL IS VALID it return email and map email.jsp page
>            request.setAttribute("returnValue",  email);
>            return mapping.findForward(SUCCESS); //View 1
>
>        } catch (Exception e) {
>           //If email is invalid it retun null with error message and map to
> blank page.
>            request.setAttribute("returnValue",  "-1");
>
>            ActionErrors errors = new ActionErrors();
>            errors.add("error", new ActionMessage(e.getMessage(), false));
>            this.saveErrors(request, errors);
>            return mapping.findForward(FAILURE); //View 2
>
>        }
> }
> On 12/2/2010 1:42 PM, Dave Newton wrote:
>
>> On Thu, Dec 2, 2010 at 1:25 PM, Anjib Mulepati wrote:
>>
>>  But problem I am having is I can't tap the condition where email is
>>> invalid.
>>>
>>> $.post(
>>>        "validateEmail.do",
>>>        {sendValue: email},
>>>        function(returnValue){
>>>            addToTable(returnValue); //add email to table
>>>
>>
>> "Tap it" in that function: isn't the returnValue either the email (if
>> successful) or nothing (if not)?
>>
>> Although personally I'd still use JSON; easier.
>>
>> Dave
>>
>>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
> For additional commands, e-mail: user-help@struts.apache.org
>
>

Re: Struts 1 and AJAX

Posted by Anjib Mulepati <an...@hotmail.com>.
So do I need to map two different view in action class as follow

{
         String email = (String) request.getParameter("sendValue");
         // perform validation of email address
         try {
             insp.validate(email);
            //iF EMAIL IS VALID it return email and map email.jsp page
             request.setAttribute("returnValue",  email);
             return mapping.findForward(SUCCESS); //View 1
         } catch (Exception e) {
            //If email is invalid it retun null with error message and 
map to blank page.
             request.setAttribute("returnValue",  "-1");
             ActionErrors errors = new ActionErrors();
             errors.add("error", new ActionMessage(e.getMessage(), false));
             this.saveErrors(request, errors);
             return mapping.findForward(FAILURE); //View 2
         }
}
On 12/2/2010 1:42 PM, Dave Newton wrote:
> On Thu, Dec 2, 2010 at 1:25 PM, Anjib Mulepati wrote:
>
>> But problem I am having is I can't tap the condition where email is
>> invalid.
>>
>> $.post(
>>         "validateEmail.do",
>>         {sendValue: email},
>>         function(returnValue){
>>             addToTable(returnValue); //add email to table
>
> "Tap it" in that function: isn't the returnValue either the email (if
> successful) or nothing (if not)?
>
> Although personally I'd still use JSON; easier.
>
> Dave
>


---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org


Re: Struts 1 and AJAX

Posted by Dave Newton <da...@gmail.com>.
On Thu, Dec 2, 2010 at 1:25 PM, Anjib Mulepati wrote:

> But problem I am having is I can't tap the condition where email is
> invalid.
>
> $.post(
>        "validateEmail.do",
>        {sendValue: email},
>        function(returnValue){
>            addToTable(returnValue); //add email to table


"Tap it" in that function: isn't the returnValue either the email (if
successful) or nothing (if not)?

Although personally I'd still use JSON; easier.

Dave

Re: Struts 1 and AJAX

Posted by Anjib Mulepati <an...@hotmail.com>.
I am trying to validate email user have entered in textbox. I am using 
AJAX call for validation. So I am sending my email as post request and 
if validated it will add email in the table in the web page. This is 
what I want to happen.

But problem I am having is I can't tap the condition where email is 
invalid. Right now I am mapping blank page and null value in request 
object for invalid condition hoping to test the null value in view to 
take necessary action.

In AJAX upon successful call it simply add the email to the row of the 
table.

My code is as follow

1. I have AJAX post as follow:

$.post(
         "validateEmail.do",
         {sendValue: email},
         function(returnValue){
             addToTable(returnValue); //add email to table
         },
         "text"
     );

2. I have action as:
{
         String email = (String) request.getParameter("sendValue");
         // perform validation of email address
         try {
             insp.validate(email);
            //iF EMAIL IS VALID it return email and map email.jsp page
             request.setAttribute("returnValue",  email);
             return mapping.findForward(SUCCESS);
         } catch (Exception e) {
            //If email is invalid it retun null with error message and 
map to blank page.
             request.setAttribute("returnValue",  null);
             ActionErrors errors = new ActionErrors();
             errors.add("error", new ActionMessage(e.getMessage(), false));
             this.saveErrors(request, errors);
             return mapping.findForward(FAILURE);
         }
}


3. I have action mapping as

<action path="/validateEmail"
                 type="actions.ValidateEmailAction" >
<forward name="success" path="/pages/email.jsp" />
<forward name="failure" path="/pages/blank.jsp" />
</action>

4. Email.jsp simply display email
<%= request.getAttribute("returnValue") %>*
*
5. blank.jsp is just blank**page*

Anjib
*






On 12/2/2010 12:49 PM, Dave Newton wrote:
> On Thu, Dec 2, 2010 at 12:43 PM, Anjib Mulepati wrote:
>
>> Ok second also doesn't work.
>>
> "It doesn't work" actually isn't descriptive enough to allow anybody to help
> effectively--you'd need to say what actually happens, what you *expect* to
> happen, what's in your JavaScript console, what the action is returning to
> the Ajax call (HTML? JSON? XML?), and details like that.
>
> Otherwise we're all just guessing, and that takes too long.
>
>
>> My page display email if valid and null if invalid.
>>
> We have no idea what you're doing with the returned data other than calling
> a JS function you don't show.
>
> *Where I am doing wrong?
>
>
> Step 1: What does the action return for success? For failure?
> Step 2: How do you determine in the JS if it's a success or failure?
>
> Dave
>


Re: Struts 1 and AJAX

Posted by Dave Newton <da...@gmail.com>.
On Thu, Dec 2, 2010 at 12:43 PM, Anjib Mulepati wrote:

> Ok second also doesn't work.
>

"It doesn't work" actually isn't descriptive enough to allow anybody to help
effectively--you'd need to say what actually happens, what you *expect* to
happen, what's in your JavaScript console, what the action is returning to
the Ajax call (HTML? JSON? XML?), and details like that.

Otherwise we're all just guessing, and that takes too long.


> My page display email if valid and null if invalid.
>

We have no idea what you're doing with the returned data other than calling
a JS function you don't show.

*Where I am doing wrong?


Step 1: What does the action return for success? For failure?
Step 2: How do you determine in the JS if it's a success or failure?

Dave

Re: Struts 1 and AJAX

Posted by Anjib Mulepati <an...@hotmail.com>.
Ok second also doesn't work.

1. I have AJAX post as follow:

$.post(
         "validateEmail.do",
         {sendValue: email},
         function(returnValue){
             addToTable(returnValue);
         },
         "text"
     );

2. I have action as:
{
         String email = (String) request.getParameter("sendValue");
         // perform validation of email address
         try {
             insp.validate(email);
             request.setAttribute("returnValue",  email);
             return mapping.findForward(SUCCESS);
         } catch (Exception e) {
             request.setAttribute("returnValue",  null);
             ActionErrors errors = new ActionErrors();
             errors.add("error", new ActionMessage(e.getMessage(), false));
             this.saveErrors(request, errors);
             return mapping.findForward(FAILURE);
         }
}

My page display email if valid and null if invalid.

3. I have action mapping as

<action path="/validateEmail"
                 type="actions.ValidateEmailAction" >
<forward name="success" path="/pages/email.jsp" />
<forward name="failure" path="/pages/blank.jsp" />
</action>

4. Email.jsp simply display email
<%= request.getAttribute("returnValue") %>*
*
5. blank.jsp is just blank**page*

*Where I am doing wrong?

Anjib
*
*

On 12/2/2010 11:58 AM, Dave Newton wrote:
> On Thu, Dec 2, 2010 at 11:44 AM, Anjib Mulepati wrote:
>
>> 1. My record in the table disappear if page is refresh.
>>
> Don't refresh? Otherwise you need a mechanism to "remember" what you're
> working with, like the session.
>
>
>> 2. And If some of my email is incorrect I don't want that to be get back to
>> view again. I just want to show a error.
>>
> Look at the results of the AJAX call. If it's an error, display a message.
> Should be trivial if using JSON, only marginally less trivial if you're not.
>
> Dave
>


Re: Struts 1 and AJAX

Posted by Dave Newton <da...@gmail.com>.
On Thu, Dec 2, 2010 at 11:44 AM, Anjib Mulepati wrote:

> 1. My record in the table disappear if page is refresh.
>

Don't refresh? Otherwise you need a mechanism to "remember" what you're
working with, like the session.


> 2. And If some of my email is incorrect I don't want that to be get back to
> view again. I just want to show a error.
>

Look at the results of the AJAX call. If it's an error, display a message.
Should be trivial if using JSON, only marginally less trivial if you're not.

Dave