更新操作未执行-已解析[org.springframework.web.httprequestmethodnotsupportedexception:请求方法'post'不受支持]

uplii1fm  于 2021-07-05  发布在  Java
关注(0)|答案(3)|浏览(375)

我正试图在spring boot+thymeleaf中开发一个应用程序,我能够从mysql数据库的profile选项卡中检索登录用户的详细信息,但是,当我尝试更改一个或两个字段详细信息(更新)并点击更新按钮时,会显示一条错误消息-fri sep 04 20:39:47 ist 2020出现意外错误(type=method not allowed,status=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;
    }

文件页代码(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类的顶部,然后这个代码的工作和创建新用户改为更新。

vxf3dgd4

vxf3dgd41#

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

@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;
    }
nx7onnlm

nx7onnlm2#

您在is post中请求Map,但控制器已设置为接受put请求。

<form name="myForm" th:action="@{/profile}" th:object="${customer}"**method="post"**>

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

只要保持这些相似的方式两者应该是相同的。

oknrviil

oknrviil3#

您的控制器带有注解 @RequestMapping(value = "/profile", method = RequestMethod.PUT) 这使得它成为一个put端点。然而,你的要求显然是一个职位。如果我们看看你的html表单,它包含 method="post" . html表单只支持get和post作为有效方法,因此需要将端点更新为post端点。
tldr公司;

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

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

相关问题