Update operation is not performing -Resolved [org.springframework.web.HttpRequestMethodNotSupportedException: Request method 'POST' not supported]

huangapple go评论78阅读模式
英文:

Update operation is not performing -Resolved [org.springframework.web.HttpRequestMethodNotSupportedException: Request method 'POST' not supported]

问题

我正尝试开发一个使用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", "用户详细信息已成功更新!");
            model.setViewName("profile");
        }
    } else {
        model.addObject("exist", "请输入正确的电子邮件地址!");
        String email = (String) session.getAttribute("emailsession");
        Customer cust = cRepo.findByCustEmail(email);
        model.addObject("customer", cust);
        model.setViewName("profile");
    }
    
    return model;
}

Thymeleaf 代码(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">个人资料详情</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="输入ID" th:field="${customer.custId}" disabled="true" required="required" />
                    </div>
                    <!-- 其他表单字段... -->
                    <div class="form-group">
                        <input type="submit" class="btn btn-primary btn-block btn-lg" value="更新" />
                    </div>
                </form>
            </div>
        </div>
    </div>
</div>

我希望用户登录并访问时能够查看自己的个人资料(这一部分我已经实现了),当用户想要更新一些字段(1-2个,根据选择)并点击更新时,应能够更新详细信息(而不是创建新用户或记录),因为当我在类的顶部使用 @Controller 注解时,这段代码可以正常工作并更新数据,而不是创建新用户。

英文:

I'm trying to develop an application in spring boot + thymeleaf, and I'm able to retrieve the logged in user details in the profile tab from the MySQL database, but when I try to change one or two field details (update) and hit the update button it is showing me an error message - Fri Sep 04 20:39:47 IST 2020
There was an unexpected error (type=Method Not Allowed, status=405).
Request method 'POST' not supported

see my controller code (I'm using @RestController annotated on top of the class)-

@RequestMapping(value = &quot;/profile&quot;, 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(&quot;emailsession&quot;))) {
				
				cRepo.save(customer);
				model.addObject(&quot;msg&quot;, &quot;User Details has been successfully updated!!&quot;);
				model.setViewName(&quot;profile&quot;);
			}
				
		}else {
			
			model.addObject(&quot;exist&quot;, &quot;Please enter correct email address!&quot;);
			String email = (String) session.getAttribute(&quot;emailsession&quot;);
			Customer cust = cRepo.findByCustEmail(email);
			model.addObject(&quot;customer&quot;, cust);
			model.setViewName(&quot;profile&quot;);
			
		}
		
		return model;
	}

Thymleaf code (html) -

&lt;div align=&quot;center&quot; class=&quot;alert alert-success&quot; th:if=&quot;${msg}&quot; th:utext=&quot;${msg}&quot;&gt;&lt;/div&gt;
&lt;div align=&quot;center&quot; class=&quot;alert alert-danger&quot; th:if=&quot;${exist}&quot; th:utext=&quot;${exist}&quot;&gt;&lt;/div&gt;

	&lt;!-- Modal HTML --&gt;
&lt;div id=&quot;myModal&quot;&gt;
	&lt;div class=&quot;modal-dialog modal-login&quot;&gt;
		&lt;div class=&quot;modal-content&quot;&gt;
			&lt;div class=&quot;modal-header&quot;&gt;				
				&lt;h4 class=&quot;modal-title&quot;&gt;Profile Details&lt;/h4&gt;
			
			&lt;/div&gt;
			&lt;div class=&quot;modal-body&quot;&gt;
				&lt;form name=&quot;myForm&quot; th:action=&quot;@{/profile}&quot; th:object=&quot;${customer}&quot; method=&quot;post&quot;&gt;
					
					&lt;div class=&quot;form-group&quot;&gt;
						&lt;i class=&quot;fa fa-id-card&quot;&gt;&lt;/i&gt; 
						&lt;input name=&quot;id&quot; type=&quot;text&quot; class=&quot;form-control&quot; placeholder=&quot;Enter Id&quot; th:field=&quot;${customer.custId}&quot; disabled=&quot;true&quot; required=&quot;required&quot; /&gt;
					&lt;/div&gt;
					&lt;div class=&quot;form-group&quot;&gt;
						&lt;i class=&quot;fa fa-user&quot;&gt;&lt;/i&gt;
						&lt;input name=&quot;name&quot; type=&quot;text&quot; class=&quot;form-control&quot; placeholder=&quot;Enter Name&quot; th:field=&quot;${customer.custName}&quot;  required=&quot;required&quot; /&gt;
					&lt;/div&gt;
					&lt;div class=&quot;form-group&quot;&gt;
						&lt;i class=&quot;fa fa-envelope&quot;&gt;&lt;/i&gt; 
						&lt;input name=&quot;email&quot; type=&quot;email&quot; class=&quot;form-control&quot; placeholder=&quot;Enter Email&quot; th:field=&quot;${customer.custEmail}&quot; required=&quot;required&quot; /&gt;
					&lt;/div&gt;
					&lt;div class=&quot;form-group&quot;&gt;
						&lt;i class=&quot;fa fa-lock&quot;&gt;&lt;/i&gt;
						&lt;input name=&quot;password&quot; type=&quot;text&quot; class=&quot;form-control&quot; placeholder=&quot;Enter Password&quot; th:field=&quot;${customer.custPassword}&quot; required=&quot;required&quot; /&gt;					
					&lt;/div&gt;
					&lt;div class=&quot;form-group&quot;&gt;
						&lt;input type=&quot;submit&quot; class=&quot;btn btn-primary btn-block btn-lg&quot; value=&quot;Update&quot; /&gt;
					&lt;/div&gt;
				&lt;/form&gt;
			&lt;/div&gt;
		&lt;/div&gt;
	&lt;/div&gt;
&lt;/div&gt;     

I want when user login and visit he/she should be able to check his/her profile(which I'm able to do working code) and when the user wants to update few fields(1-2 based on choice) and hit update he/she should be able to update the details (not create new user or record) because when I use @Controller on top of class then this code work and create new user instead update.

答案1

得分: 1

你的控制器使用了@RequestMapping(value = "/profile", method = RequestMethod.PUT)进行了注解,这使它成为了一个PUT端点。然而,你的请求明显是一个POST请求。如果我们看一下你的HTML表单,它包含了method="post"。HTML表单只支持GET和POST作为有效的方法,所以你需要将你的端点更新为一个POST端点。

简而言之:

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

更新为

@RequestMapping(value = "/profile", method = RequestMethod.POST)
英文:

Your controller is annotated with @RequestMapping(value = &quot;/profile&quot;, method = RequestMethod.PUT) which makes it a PUT endpoint. However, your request is clearly a POST. If we look at your html form it contains method=&quot;post&quot;. HTML forms only support GET and POST as valid methods so you need to update your endpoint to be a POST endpoint.

tldr;

RequestMapping(value = &quot;/profile&quot;, method = RequestMethod.PUT)

to

RequestMapping(value = &quot;/profile&quot;, method = RequestMethod.POST) 

答案2

得分: 1

你在<form>中请求映射是POST,但控制器已设置为接受PUT请求。

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

保持这些内容类似,两者应该相同。

英文:

You request mapping in <form> is POST but Controller has set to accept request as PUT.

&lt;form name=&quot;myForm&quot; th:action=&quot;@{/profile}&quot; th:object=&quot;${customer}&quot; **method=&quot;post&quot;**&gt;


@RequestMapping(value = &quot;/profile&quot;, method = **RequestMethod.PUT**) 

Just keep these in similar way both should be same.

答案3

得分: 1

请检查我找到的内容并解决这个问题

@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", "用户详情已成功更新!");
            
            model.addObject("customer", exist);
            model.setViewName("profile");
        }
            
    } else {
        
        model.addObject("exist", "请输入正确的电子邮件地址!");
        String email = (String) session.getAttribute("emailsession");
        Customer cust = cRepo.findByCustEmail(email);
        model.addObject("customer", cust);
        model.setViewName("profile");
        
    }
    
    return model;
}
英文:

Please check what I find and resolve this.

@RequestMapping(value = &quot;/profile&quot; ,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(&quot;emailsession&quot;))) {
				
				**exist.setCustId(exist.getCustId());
				exist.setCustName(customer.getCustName());
				exist.setCustEmail(customer.getCustEmail());
				exist.setCustPassword(customer.getCustPassword());**
				
				cRepo.save(exist);
				model.addObject(&quot;msg&quot;, &quot;User Details has been successfully updated!!&quot;);
				
				model.addObject(&quot;customer&quot;, exist);
				model.setViewName(&quot;profile&quot;);
			}
				
		}else {
			
			model.addObject(&quot;exist&quot;, &quot;Please enter correct email address!&quot;);
			String email = (String) session.getAttribute(&quot;emailsession&quot;);
			Customer cust = cRepo.findByCustEmail(email);
			model.addObject(&quot;customer&quot;, cust);
			model.setViewName(&quot;profile&quot;);
			
		}
		
		return model;
	}

huangapple
  • 本文由 发表于 2020年9月4日 23:24:25
  • 转载请务必保留本文链接:https://go.coder-hub.com/63743981.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定