0

I'm learning java web app development.
I'm trying to update the user's data with @PutMapping request but my URL is not getting the right value. When I view the user profile my URL looks like baseURL/userProfile/username. Here username is a primary key (means id of the User class). When I click the update profile button the URL becomes /userProfile/userProfile/username. It should be the same as the previous one. Here are my both Get and Put request methods.

Get Method

@RequestMapping("/userProfile/{username}")
    public ModelAndView viewProfile(ModelAndView mv, @ModelAttribute("user") User user, 
            @PathVariable("username") String username) {

        user = userService.getUserDetailByEmailOrUsername(username, "UserProfile");

        System.out.println("Inside /profile\nUsername = " + user.getUsername());
        mv.setViewName("profile");

        mv.addObject("user", user);

        return mv;
    }

Put Method:

    @PutMapping("userProfile/{username}")
    public ModelAndView updateProfile(ModelAndView mv, User user, 
            @PathVariable("username") String username) {

        System.out.println("Inside /updateProfile\nUsername = " + user.getUsername());
        userService.updateUser(user);
        mv.setViewName("dashboard");
        mv.addObject("msg", "Profile Updated Successfully!");

        return mv;
    }

And here is the profile form in JSP

<form:form modelAttribute="user" class="signup-form bg-dark text-light d-flex flex-column align-items-center" action="userProfile/${username}" method="Post"
      oninput="result.value=!!confirm_password.value&&(uPassword.value==confirm_password.value)?'Matched!':'Both password fields must be same!'">
      <div class="text-center mb-4">
            <img class="mb-4" src="images/img_avatar2.png" alt="" width="72" height="72">
            <h2 class="form-signin-heading"></h2>
            <p>Here is your Profile Information</p>
            <hr>
            </div>
            <div class="input-group">
                 <div class="input-group-prepend">
                     <span class="input-group-text">
                         <span class="fa fa-user"></span>
                     </span>                    
                 </div>
                 <form:input type="text" class="form-control" name="username" path="username" disabled="true" placeholder="Username" autofocus=""/>
             </div>
             <br>
             <div class="input-group">
                <div class="input-group-prepend">
                    <span class="input-group-text">
                         <span class="fa fa-envelope"></span>
                    </span>                    
                </div>
                <form:input type="email" class="form-control" name="email" path="email" placeholder="Email Address"/>
             </div>
             <br>
             <div class="input-group">
                 <div class="input-group-prepend">
                      <span class="input-group-text">
                           <span class="fa fas fa-address-card"></span>
                      </span>                    
                  </div>
                  <form:input class="form-control" name="fullName" path="fullName" placeholder="Full Name(name & surname)"/>
              </div>
              <br>
              <br>
              <br>
              <div class="input-group">
                  <div class="input-group-prepend">
                      <span class="input-group-text">
                           <span class="fa fa-lock"></span>
                      </span>                    
                  </div>
                  <input type="password" class="form-control" name="oldPassword" placeholder="Old Password Here">
             </div>
             <br>
             <div class="input-group">
                 <div class="input-group-prepend">
                     <span class="input-group-text">
                         <span class="fa fa-lock"></span>
                     </span>                    
                 </div>
                 <input type="password" class="form-control" name="newPassword" placeholder="New Password Here">
             </div>
             <br>
             <div class="input-group">
                  <div class="input-group-prepend">
                      <span class="input-group-text">
                           <span class="fa fa-lock"></span>
                           <span class="fa fa-check"></span>
                           </span>                    
                  </div>
                  <input type="password" class="form-control" name="confirm_password" placeholder="Confirm New Password">
           </div>
           <output name="result" style="text-decoration: blink; color: red"></output>
           <br>

           <br>
           <div class="form-group">
           <button type="submit" class="btn btn-primary btn-lg">Update Profile</button>
    </div>
</form:form>

Please point out why I'm getting userProfile two times in the URL. I think the above code would be sufficient to find out what is wrong. If there is a need to expose more code/classes please let me know.

Regard Inzimam Tariq

Inzimam Tariq IT
  • 6,548
  • 8
  • 42
  • 69

0 Answers0