英文:
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 = "/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 code (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>     
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 = "/profile", 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="post". 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 = "/profile", method = RequestMethod.PUT)
to
RequestMapping(value = "/profile", 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.
<form name="myForm" th:action="@{/profile}" th:object="${customer}" **method="post"**>
@RequestMapping(value = "/profile", 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 = "/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;
	}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。


评论