代码之家  ›  专栏  ›  技术社区  ›  user7036414

传递null值的Spring web PUT方法

  •  0
  • user7036414  · 技术社区  · 7 年前

    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>
    

    我在接收端的put映射

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

    <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 请求,该请求工作正常,但此请求正在变为null。请帮我解决这个问题

    注意:Id仍在获取值,但对象 user 不是

    1 回复  |  直到 7 年前
        1
  •  1
  •   StanislavL    7 年前

    您已放置映射

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

    但是你的表单有POST方法

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