gpt4 book ai didi

java - 使用 Spring 获取表单数据时出现问题

转载 作者:行者123 更新时间:2023-11-30 03:02:15 25 4
gpt4 key购买 nike

我对前端开发非常陌生。我有一个简单的 Web 应用程序,您可以在其中提交表单,并且我希望能够在 REST 端点中获取表单数据。我正在使用包含 Spring MVC 的 Spring boot。

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");
}
}

最佳答案

Problem 1: HTML form controls are missing the name attribute

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]

Problem 2: @RequestBody as a controller method argument will give you the entire HTTP request body

public void createEntity(@RequestBody String payload)

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

name: [the value you specify for the control]

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

public void createEntity(@RequestParam String name)

这会将 HTTP 请求参数 name 映射到 Controller 方法参数 name

Problem 3: Having void as the return type for a controller method will force Spring MVC to look for a view with the same name as the method name

由于您将 Controller 方法声明为:

public void createEntity(@RequestBody String payload)

此方法退出后,Spring MVC 将查找名为 createEntity 的 View 。如果您有一个具有该名称的 View ,那就太好了。否则,一旦解决了问题 1 和 2,您将收到 404 - 未找到资源 错误。

关于java - 使用 Spring 获取表单数据时出现问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35664974/

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