• Jobs
  • About Us
  • professionals
    • Home
    • Jobs
    • Courses and challenges
  • business
    • Home
    • Post vacancy
    • Our process
    • Pricing
    • Assessments
    • Payroll
    • Blog
    • Sales
    • Salary Calculator

0

151
Views
How to check username availability using Ajax in Struts 2

I am trying to check username available or not when a user tries to create his account. when a user types it's username there should be an instant check that username is available or not and it will show a message in just below the username box.

I was tried to achieve it by calling an Ajax but not able to understand what to return basically and how will it work, actually I am very new in Struts 2 , I am able to check the username but did not under what to return.

My Ajax Call

<script>
            $(document).ready(function () {
                $("input").blur(function () {
                    var input = $(this).val();
                    alert(input);
                    $.ajax({
                        url:'checkUsername',
                        method:"POST",
                        data:{username:input},
                        success:function(data)
                        {
                            if(data!='0'){
                                $('#availability').html('<span>not available</span>')
                                $('#update').attr("disabled",true);
                            }
                            else{
                                $('#availability').html('<span>available</span>')
                                $('#update').attr("disabled",false);
                            }
                        }
                    })
                });
            });
        </script>

checkUsername Action

public String checkUsername() {
        try {
            setCtr(admin.checkUsername(username));
            if (ctr > 0) {
                System.out.println(ctr);
                setNoData(false);
            } else {
                setNoData(true);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "CHECKUSER";
    }

method to check username in dao

public int checkUsername(String username) throws Exception {
        ResultSet rs = null;
        Connection con = null;
        try {
            con = ConnectionManager.getConnection();
            System.out.println(username);
            String sql = "SELECT * FROM userinfo WHERE username =?";
            PreparedStatement ps = con.prepareStatement(sql);
            ps.setString(1, username);
            rs = ps.executeQuery();
            if (rs.next()) {
                return 1;
            }

        } catch (Exception e) {
            e.printStackTrace();

        } finally {
            if (con != null) {
                con.close();
            }
        }
        return 0;
    }
}

This method in dao is able to check the username but what to return in that ajax data which i am trying to check that the if rows > 0 it should print not available. How to return and how to check?
struts.xml

 <action name="checkUsername" class="com.redress.actions.AdminAction" method = "checkUsername"> </action>

Can anyone please correct me, how to achieve this?

about 3 years ago · Santiago Trujillo
1 answers
Answer question

0

Just return a json result. You must define it in the action configuration. I already explained how you can return a json result withoutstruts2-json-plugin . You will now use it to return a JSON object to the success callback function via jQuery ajax.

You need to add it to the build path of the project first. Then package your action configuration to extend the json-default package. You will add the JSON result type to the Struts configuration.

Make the noData (whatever) property you want to access have a getter and a setter.

Add the result of type JSON to the action configuration.

 <action name="checkUsername" class="com.redress.actions.AdminAction" method get = "checkUsername"> <result type="json"> <param name="root">action</param> </result> </action>

The action method must return the result code ActionSupport.SUCCESS .

In the success callback function you need to get the JSON object which you can check

 ActionSupport.Success:function(data){ if(!data.noData){ $('#availability').html('<span>not available</span>'); $('#update').attr("disabled",true); } else{ $('#availability').html('<span>available</span>'); $('#update').attr("disabled",false); } }
about 3 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Our process Sales
Legal
Terms and conditions Privacy policy
© 2025 PeakU Inc. All Rights Reserved.

Andres GPT

Recommend me some offers
I have an error