【问题标题】:Update operation is not performing -Resolved [org.springframework.web.HttpRequestMethodNotSupportedException: Request method 'POST' not supported]更新操作未执行 - 已解决 [org.springframework.web.HttpRequestMethodNotSupportedException:不支持请求方法“POST”]
【发布时间】: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


    【解决方案1】:

    您的控制器带有 @RequestMapping(value = "/profile", method = RequestMethod.PUT) 注释,这使其成为 PUT 端点。但是,您的请求显然是 POST。如果我们查看您的 html 表单,它包含method="post"。 HTML 表单仅支持 GET 和 POST 作为有效方法,因此您需要将端点更新为 POST 端点。

    tldr;

    RequestMapping(value = "/profile", method = RequestMethod.PUT)
    

    RequestMapping(value = "/profile", method = RequestMethod.POST) 
    

    【讨论】:

    • 当我更改为 RequestMapping(value = "/profile", method = RequestMethod.POST) 它正在数据库中添加新记录(新用户)。但是我想更新详细信息,因为在配置文件选项卡中,用户应该更新详细信息而不是添加新的。你现在可以建议吗?
    • 这与您现有的问题无关,但通常所做的是将表的主键通过端点传递并使用它来确定您是否需要进行插入或更新。如果主键不为空,您知道它应该是更新,反之亦然
    • 我的问题既是错误也是更新查询,因为我正在进行更新并将表的正确主键设置为非空,这意味着主键已经存在于数据库中所以它应该被覆盖而不是创建一个新的。你能建议我吗?我是新手,正在寻求帮助。
    • 你能帮帮我吗?
    【解决方案2】:

    您请求的映射是 POST,但控制器已设置接受请求作为 PUT。

    <form name="myForm" th:action="@{/profile}" th:object="${customer}" **method="post"**>
    
    
    @RequestMapping(value = "/profile", method = **RequestMethod.PUT**) 
    

    只需以相似的方式保持这些都应该相同。

    【讨论】:

    • 我已经进行了更改,现在我已经编写了 POST 的两个地方,但是当我尝试从那里更新一个或两个字段时,它正在将数据添加到数据库中(新用户)。我怎样才能更新而不是添加一个新的?
    • 你能帮帮我吗?
    • 更新-替换-cRepo.save(customer);与 - cRepo.save(exists);
    • @AjayKumar 谢谢它的工作,我们需要做的唯一额外的事情是在保存信息之前设置值。
    • 当然你会提供/更改一些数据来更新。
    【解决方案3】:

    请检查我的发现并解决这个问题。

    @RequestMapping(value = "/profile" ,method = RequestMethod.POST)
        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"))) {
                    
                    **exist.setCustId(exist.getCustId());
                    exist.setCustName(customer.getCustName());
                    exist.setCustEmail(customer.getCustEmail());
                    exist.setCustPassword(customer.getCustPassword());**
                    
                    cRepo.save(exist);
                    model.addObject("msg", "User Details has been successfully updated!!");
                    
                    model.addObject("customer", exist);
                    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;
        }
    

    【讨论】:

      猜你喜欢
      • 2020-05-19
      • 2022-01-15
      • 1970-01-01
      • 2020-10-29
      • 2021-09-20
      • 2022-01-06
      • 1970-01-01
      • 2018-09-11
      • 2020-10-02
      相关资源
      最近更新 更多