Spring Web PUT方法传递空值

时间:2017-08-16 11:07:25

标签: java spring spring-mvc

我正在Spring MVC工作,在提交put方法时,我在控制器中获得了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不是

1 个答案:

答案 0 :(得分:1)

你已经放置了映射

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

但您的表单有POST方法

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