最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How can i pass two params @Path and @PathParam - Stack Overflow

programmeradmin1浏览0评论

I'm trying to create authentification method using java angular js and mysql. I know how to pass one parameter but I couldn't pass two can you please help me.

Here is my DAO method :

public Client authentifier(int numeropte,String mdp) {

            System.out.println(numeropte + mdp);

            try {
            Connection con = Connexion.getConnection();
            PreparedStatement ps = con.prepareStatement("select * from client WHERE numeropte = ? AND mdp = ?");
            ps.setInt(1, numeropte);
            ps.setString(2, mdp);
            Client e = new Client();
            ResultSet rs = ps.executeQuery();

            while (rs.next()) {

                e.setIdclient(rs.getInt("idclient"));
                e.setNomplet(rs.getString("nomplet"));
                e.setMail(rs.getString("mail"));
                e.setNumeropte(rs.getInt("numeropte"));
                e.setMdp(rs.getString("mdp"));

            }
            rs.close();
            return e;

            }catch (Exception e) {
                System.out.println("Erreur avec  authentifier() -->" + e.getMessage());
                return (null);
            }

        }

Here is my controller method in wehere I would Like to pass the 2 params :

@GET
    @Path("authentifier/{numeropte}{mdp}")
    @Consumes(MediaType.APPLICATION_JSON)
    @Produces(MediaType.APPLICATION_JSON)
    public Client authentifier(@PathParam("numeropte , mdp") int numeropte, String mdp) {
        ClientDao dao = new ClientDao();
        System.out.println(numeropte);
        return dao.authentifier(numeropte,mdp);
    }

and here is the angular js line where also I'll pass the 2 params :

$http.get('rest/client/authentifier/'+$scope.nouveauClient.numeropte+$scope.nouveauClient.mdp).then(function(data){
                 alert(data.data);
                })

I'm trying to create authentification method using java angular js and mysql. I know how to pass one parameter but I couldn't pass two can you please help me.

Here is my DAO method :

public Client authentifier(int numeropte,String mdp) {

            System.out.println(numeropte + mdp);

            try {
            Connection con = Connexion.getConnection();
            PreparedStatement ps = con.prepareStatement("select * from client WHERE numeropte = ? AND mdp = ?");
            ps.setInt(1, numeropte);
            ps.setString(2, mdp);
            Client e = new Client();
            ResultSet rs = ps.executeQuery();

            while (rs.next()) {

                e.setIdclient(rs.getInt("idclient"));
                e.setNomplet(rs.getString("nomplet"));
                e.setMail(rs.getString("mail"));
                e.setNumeropte(rs.getInt("numeropte"));
                e.setMdp(rs.getString("mdp"));

            }
            rs.close();
            return e;

            }catch (Exception e) {
                System.out.println("Erreur avec  authentifier() -->" + e.getMessage());
                return (null);
            }

        }

Here is my controller method in wehere I would Like to pass the 2 params :

@GET
    @Path("authentifier/{numeropte}{mdp}")
    @Consumes(MediaType.APPLICATION_JSON)
    @Produces(MediaType.APPLICATION_JSON)
    public Client authentifier(@PathParam("numeropte , mdp") int numeropte, String mdp) {
        ClientDao dao = new ClientDao();
        System.out.println(numeropte);
        return dao.authentifier(numeropte,mdp);
    }

and here is the angular js line where also I'll pass the 2 params :

$http.get('rest/client/authentifier/'+$scope.nouveauClient.numeropte+$scope.nouveauClient.mdp).then(function(data){
                 alert(data.data);
                })
Share Improve this question edited Jan 29, 2018 at 12:48 Andrew 49.7k15 gold badges98 silver badges144 bronze badges asked Jan 29, 2018 at 11:40 A.AouA.Aou 551 silver badge10 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 4

All it takes is a additional @PathParam("mdp") annotation in the signature of your controller method.

@GET
@Path("authentifier/{numeropte}/{mdp}")
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
public Client authentifier(@PathParam("numeropte") int numeropte, 
                           @PathParam("mdp") String mdp) {

    ClientDao dao = new ClientDao();
    System.out.println(numeropte);
    return dao.authentifier(numeropte,mdp);
}

Furthermore, make sure you specify your path well. In your example you declared

@Path("authentifier/{numeropte}{mdp}")

JAX-RS won't be able to know where numeropte start and where it ends. Separate them from each other like this

@Path("authentifier/{numeropte}/{mdp}")

and then

$http.get('rest/client/authentifier/'+$scope.nouveauClient.numeropte+'/'+$scope.nouveauClient.mdp).then(function(data){
             alert(data.data);
})

Put a @PathParam("pathParamName") for each method parameter

public Client authentifier(
    @PathParam("numeropte") int numeropte, 
    @PathParam("mdp") String mdp
)

and make sure you follow the path specified.

You forgot to put the slash between numeropteand mdp at your @Pathannotation.

Your authentifier function should be like the following

@GET
@Path("authentifier/{numeropte}/{mdp}")
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
public Client authentifier(@PathParam("numeropte") int numeropte, @PathParam("mdp") String mdp) {
    ...
}

And the same in your Angular code

$http.get('rest/client/authentifier/'+$scope.nouveauClient.numeropte+'/'+$scope.nouveauClient.mdp).then(function(data) { alert(data.data); })
发布评论

评论列表(0)

  1. 暂无评论