我正在学习Java Web App开发。 我正在尝试通过@PutMapping请求更新用户的数据,但是我的URL值不正确。当我查看用户个人资料时,我的URL看起来像baseURL/userProfile/username。这里的username是主键(User类的ID)。当我单击更新配置文件按钮时,URL变为/userProfile/userProfile/username。它应该与上一个相同。这是我的Get和Put请求方法。
获取方法
@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;
}
放置方法:
@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;
}
这是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>
请指出为什么我userProfile两次获得该URL。我认为上述代码足以找出问题所在。如果需要公开更多代码/类,请告诉我。
问候Inzimam Tariq
版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。