【发布时间】:2020-12-23 21:25:11
【问题描述】:
我正在尝试在 spring boot + thymeleaf 中开发应用程序,并且我能够从 MySQL 数据库的配置文件选项卡中检索登录的用户详细信息,但是当我尝试更改一两个字段详细信息时(更新)并点击更新按钮,它向我显示 错误消息 - Fri Sep 04 20:39:47 IST 2020 出现意外错误(类型=不允许方法,状态=405)。 不支持请求方法“POST”
查看我的控制器代码(我正在使用在类顶部注释的@RestController)-
@RequestMapping(value = "/profile", method = RequestMethod.PUT)
public ModelAndView updateProfile(@ModelAttribute Customer customer, HttpSession session) {
ModelAndView model = new ModelAndView();
Customer exist = cRepo.findByCustEmail(customer.getCustEmail());
if(exist != null) {
if(exist.getCustEmail().equals(session.getAttribute("emailsession"))) {
cRepo.save(customer);
model.addObject("msg", "User Details has been successfully updated!!");
model.setViewName("profile");
}
}else {
model.addObject("exist", "Please enter correct email address!");
String email = (String) session.getAttribute("emailsession");
Customer cust = cRepo.findByCustEmail(email);
model.addObject("customer", cust);
model.setViewName("profile");
}
return model;
}
Thymleaf 代码 (html) -
<div align="center" class="alert alert-success" th:if="${msg}" th:utext="${msg}"></div>
<div align="center" class="alert alert-danger" th:if="${exist}" th:utext="${exist}"></div>
<!-- Modal HTML -->
<div id="myModal">
<div class="modal-dialog modal-login">
<div class="modal-content">
<div class="modal-header">
<h4 class="modal-title">Profile Details</h4>
</div>
<div class="modal-body">
<form name="myForm" th:action="@{/profile}" th:object="${customer}" method="post">
<div class="form-group">
<i class="fa fa-id-card"></i>
<input name="id" type="text" class="form-control" placeholder="Enter Id" th:field="${customer.custId}" disabled="true" required="required" />
</div>
<div class="form-group">
<i class="fa fa-user"></i>
<input name="name" type="text" class="form-control" placeholder="Enter Name" th:field="${customer.custName}" required="required" />
</div>
<div class="form-group">
<i class="fa fa-envelope"></i>
<input name="email" type="email" class="form-control" placeholder="Enter Email" th:field="${customer.custEmail}" required="required" />
</div>
<div class="form-group">
<i class="fa fa-lock"></i>
<input name="password" type="text" class="form-control" placeholder="Enter Password" th:field="${customer.custPassword}" required="required" />
</div>
<div class="form-group">
<input type="submit" class="btn btn-primary btn-block btn-lg" value="Update" />
</div>
</form>
</div>
</div>
</div>
</div>
我希望当用户登录和访问时,他/她应该能够检查他/她的个人资料(我能够执行工作代码)以及当用户想要更新几个字段时(基于 1-2选择)并点击更新,他/她应该能够更新详细信息(而不是创建新用户或记录),因为当我在类顶部使用 @Controller 时,此代码工作并创建新用户而不是更新。
【问题讨论】:
标签: java spring hibernate spring-mvc spring-data-jpa