gpt4 book ai didi

java - Spring Web PUT 方法传递空值

转载 作者:行者123 更新时间:2023-12-02 12:21:48 24 4
gpt4 key购买 nike

我正在使用Spring MVC,在提交put方法时,我在 Controller 中获取了null值。代码:

<form:form action="../${user.id}" method="post" commandName="user">
<div class="form-group">
<label for="name">First Name</label>
<form:input path="name" class="form-control" id="name"
placeholder="Full Name" />
</div>
<input type="hidden" name="_method" value="put" />

<input type="submit" class="btn btn-success" value="SAVE">
</form:form>

我在接收端的放置映射

@PutMapping(path = "/{id}")
public String updateUser(@PathVariable(value = "id") long id, @ModelAttribute("user") User user, Model model) {
userService.updateUser(user);
model.addAttribute("user", userService.findById(id));
return "redirect:/users/" + id;
}

web.xml

<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/spring/root-context.xml</param-value>
</context-param>

<!-- Creates the Spring Container shared by all Servlets and Filters -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

<!-- Processes application requests -->
<servlet>
<servlet-name>appServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/spring/appServlet/servlet-context.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
<async-supported>true</async-supported>
</servlet>

<servlet-mapping>
<servlet-name>appServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

我有另一个来自 post 请求,该请求工作正常,但这个请求为空。请帮我解决这个问题

注意:Id 仍在获取值,但对象 user 却没有获取值

最佳答案

您已放置映射

@PutMapping(path = "/{id}")

但是你的表单有 POST 方法

<form:form action="../${user.id}" method="post" commandName="user">

关于java - Spring Web PUT 方法传递空值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45711896/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com