使用Spring获取表单数据的麻烦

时间:2016-02-27 02:28:19

标签: java html spring spring-mvc

我是前端开发的新手。我有一个简单的webapp,您可以在其中提交表单,我希望能够在REST端点中获取表单数据。我正在使用包含Spring MVC的Spring启动。

HTML:

<div class="modal-header modal-header-info">
    <button type="button" class="close" data-dismiss="modal"
        aria-hidden="true">&times;</button>
    <h4 class="modal-title">Add Entity</h4>
</div>
<div class="modal-body">
    <form role="form" method="post" action="/createEntity">
        <div class="form-group">
            <label for="name">Name:</label> <input type="text"
                class="form-control" id="name">
        </div>
        <button type="submit" class="btn btn-info">Submit</button>
    </form>
</div> 

Java:

@RequestMapping(value = "/createEntity", method = RequestMethod.POST)
public void createEntity(@RequestBody String payload) {
    System.out.println("Hello world!");
    System.out.println(payload);
}

我收到此错误:

Failed to read HTTP message:
           org.springframework.http.converter.HttpMessageNotReadableException: 
           Required request body is missing: public void    main.java.info.spring.data.neo4j.controllers.CreateEntityController.createEntity(java.lang.String)

如何从表单中获取“名称”值?如果我取出@RequestBody部分,则打印出“hello world”属性。

编辑:我的每个请求的配置:

@EnableTransactionManagement
@Import(RepositoryRestMvcConfiguration.class)
@EnableScheduling
@EnableAutoConfiguration
@ComponentScan(basePackages = { "main.java.info.spring.data.neo4j.services",
        "main.java.info.spring.data.neo4j.controllers" })
@Configuration
@EnableNeo4jRepositories(basePackages = "main.java.info.spring.data.neo4j.repositories")
public class MyNeo4jConfiguration extends Neo4jConfiguration {

    public static final String URL = System.getenv("NEO4J_URL") != null ? System.getenv("NEO4J_URL")
            : "http://localhost:7474";

    @Override
    public Neo4jServer neo4jServer() {
        return new RemoteServer(URL, "neo4j", "LOL my pass");

    }

    @Override
    public SessionFactory getSessionFactory() {
        return new SessionFactory("main.java.info.spring.data.neo4j.domain");
    }
}

1 个答案:

答案 0 :(得分:2)

  

问题1 :HTML表单控件缺少name属性

HTML表单以名称 - 值对提交。无法提交没有name属性的表单控件,因为它们的名称未知。 id属性仅用于引用客户端的控件。

<input type="text" class="form-control" id="name">

缺少name属性。它应该是:

<input type="text" class="form-control" id="name" name="name"/>

这将在HTTP请求正文中将控件作为表单参数提交,如:

name: [the value you specify for the control]
  

问题2 @RequestBody作为控制器方法参数将为您提供整个HTTP请求正文

public void createEntity(@RequestBody String payload)

表示您希望将整个HTTP请求正文作为String传递给控制器​​方法createEntity。如果您的HTML表单确实是您发布的内容,那么您将获得payload的以下值(在您解决问题1之后):

name: [the value you specify for the control]

如果这是您想要的,请保留@RequestBody注释,但我怀疑您只对name请求参数的值感兴趣。如果是这种情况,则需要将方法声明更改为(在解决问题1之后):

public void createEntity(@RequestParam String name)

这会将HTTP请求参数name映射到控制器方法参数name

  

问题3 :将void作为控制器方法的返回类型将强制Spring MVC查找与方法名称相同的视图

因为您将控制器方法声明为:

public void createEntity(@RequestBody String payload)

Spring MVC将在此方法退出后查找名为createEntity的视图。如果你有一个具有该名称的视图,那就好了。否则,一旦解决了问题1和2,您将收到404 - Resource not found错误。