2016-02-27 48 views
0

我对前端开发非常陌生。我有一个简单的web应用程序,您可以在其中提交表单,并且我希望能够在REST端点中获取表单数据。我正在使用包含Spring MVC的Spring引导。使用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"); 
    } 
} 
+0

你能展示你的配置吗? –

+0

在@ manish的回答中查看**问题1 **。这真的可能是这个问题,因为没有名字的输入可以被认为是不正确的语法。 –

回答

1

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

<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表单确实是你已经发布的东西,你会得到以下值​​(你已经解决了之后的问题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错误。

+0

**问题2 **:在这种情况下'StringHttpMessageConverter'将被使用。而且这个转换器不需要名称约定,因为它返回请求主体的所有文本。 –

+0

**问题3 **:对于REST Controller的方法,'void'返回类型是OK的。 –

+0

这工作,谢谢你礼貌地回答这个问题。 –

相关问题