Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

206
Visualizações
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?

over 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

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); } }
over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda