1

I'm having any application in which I have email_id as unique and when the end user enters his email_id I have fired SQL query through Ajax which check whether this email_id exists or not. Until here it's working fine but now I what if that email_id exists then I want to display it on the JSP page like "This id is already taken" and vice-versa.

So my question is how to get the response from Struts app and give it to Ajax or JSP page and what to write in struts.xml result tag?

This is my Ajax call to EmailCheck.action:

$("#email").blur(function() {
      var EmailV = $("#email").val();
        if(EmailV.length > 10){
            $('#Loading').show();
            $.get("EmailCheck.action", {
                Email_Id: EmailV   
            }, function(response){
                $('#Info').fadeOut();
                $('#Loading').hide();
                setTimeout("finishAjax('Info', '"+escape(response)+"')", 450);
            });
            return false;
        }
    });

This is my struts.xml:

Should there be result tag for this Email type checker?

 <action name="EmailCheck"  class="org.register.customers.EmailCheckAction" method="EmailCheck">
          <result name="input">index.jsp</result>
          <result name="success">success.jsp</result>
          <result name="error">error.jsp</result>
  </action>

This is my Action code:

 public String EmailCheck() throws Exception{
         
     System.out.println(user.getEmail_Id());

       boolean create = Check_Email(user);

 //  "this is my sql call to check the existence of email"

    if (true) {       
       
        return "input";
    } else {          
        
        return ERROR;
    }        
}
Roman C
  • 49,761
  • 33
  • 66
  • 176

1 Answers1

1

If you call an Ajax action you can return a dispatcher result. In this case a rendered JSP fragment will output as a response. Also, you have options to return a stream, json, or none results. To your case I will consider the first option. First look at this answer to get you familiar how to return a stream result with text/html content type. Then in the JSP you should create a target div where you put the response.

<div id="result"/>

now modify your JS to insert result into div.

}, function(response){
  $("result").html(response);
Community
  • 1
  • 1
Roman C
  • 49,761
  • 33
  • 66
  • 176
  • hey Thanks for the reply but what should i write in struts.xml result type i.e is my strut.xml correct – Shoaib Doulatabad Nov 16 '13 at 13:55
  • Write `type="stream"` or `type="json"` or write nothing, read the answer and use first option, you didn't post `struts.xml`, so I can't see it. – Roman C Nov 16 '13 at 15:23
  • I have posted my struts.xml the above 2 code is that one and thanks for your comment it worked and also will you specify the way to write struts.xml as it will help me further – Shoaib Doulatabad Nov 16 '13 at 19:46
  • This is your ultimate guide http://struts.apache.org/release/2.3.x/docs/configuration-elements.html – Roman C Nov 16 '13 at 20:33
  • @ShoaibDoulatabad Just reminder, If it worked you can accept it as correct answer. – Roman C Nov 23 '13 at 20:37