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?

Roman C
  • 49,761
  • 33
  • 66
  • 176
Pallab
  • 53
  • 1
  • 6
  • If you don't know how to make Ajax call to Struts action then you missed reading [this](https://stackoverflow.com/a/22345760/573032) answer. Also in your question there're not enough details about your problem. – Roman C Feb 05 '22 at 09:22
  • @RomanC I just want to check username availability , I am not getting idea how to return when i am printing the result it's printing some Html code, can you please help me – Pallab Feb 12 '22 at 15:09

1 Answers1

1

Simply return a json result. You should define it in the action config. I've already explained how you can return a json result without struts2-json-plugin. Now you will use it to return an JSON object to the success callback function via jQuery ajax.

First you need to add it to the project build path. Then make package of your action config to extend json-default package. It will add JSON result type to the Struts config.

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

Add result of type JSON to the action config.

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

The action method should return ActionSupport.SUCCESS result code.

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

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);
  }
}
Roman C
  • 49,761
  • 33
  • 66
  • 176
  • whenever i am adding that it's stopped running, it seems project not found – Pallab Feb 15 '22 at 17:35
  • how did you do that? did you add a plugin to the build path? – Roman C Feb 15 '22 at 19:16
  • yes i have added json plugin in the build path – Pallab Feb 16 '22 at 08:19
  • and also put the action to the package. – Roman C Feb 16 '22 at 12:00
  • yes i am putting that checkUsername action inside an another package like.. action – Pallab Feb 16 '22 at 12:19
  • yes, it should work. I don't understand your problem – Roman C Feb 16 '22 at 12:44
  • that package thing solved but when i am making an alert of the success result it seems undefined. alert(data.noData); It seems undefined – Pallab Feb 16 '22 at 14:35
  • in your action there should be `getNoData()` – Roman C Feb 16 '22 at 15:34
  • i am trying this similar thing in an other project but there every time ajax is going into error, it's never going into success function , why is this happen? how to fix it? if i am changing the return in action to (return ActionSupport.SUCCESS) and doing the result type ="json" ,it's taking me to the error function, otherwise it's going to success function, can you tell me why is this happening and way to fix it – Pallab Feb 19 '22 at 09:30
  • Nobody knows it without code. Check xhr status. Also you should know that comments section is not for long running discussions. If the answer helped you then you should accept it the checkbox on the left of the answer and upwote if you have enough reputation. Then you can ask a new question which is relevant to your problem. – Roman C Feb 19 '22 at 11:14