如何在Spring后端中获取可变的HTML表单值(POST) [英] How to get variable HTML form values (POST) in Spring backend

查看:46
本文介绍了如何在Spring后端中获取可变的HTML表单值(POST)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对Java和Spring框架还很陌生,这可能很容易解决,但是我找不到针对此问题的任何解决方案,而其他解决方案都不适合我的问题.

i'm fairly new to Java and the Spring Framework and this might be easy to solve but I cant find any solutions to this problem and other solutions don't fit my problem.

我想实现一种动态形式,用户可以在其中插入多个电子邮件地址来发送邀请.这种形式可以由JS动态扩展.每次单击都会向我的表单添加另一个输入字段.所以现在我要发送给Spring Backend的值的数量可变.我以为我必须使用@ResponseBody和一个Map在其中存储POST值,然后对其进行迭代,并将其复制(例如)将它们复制到ArrayList或直接使用我的EmailService发送电子邮件.

I want to implement a dynamic form, where the user inserts multiple Email addresses to send invitations. This form can dynamically be extended by JS. Every click adds another input field to my form. So now I have a variable amount of values I want to send to my Spring Backend. I was thinking that I have to use @ResponseBody and a Map to store the POST values in it and then iterate over it and (for example) copy them into an ArrayList or directly use my EmailService to send out an Email.

问题是,Spring给我一个错误:

The problem is, Spring gives me an error:

已解决的由处理程序执行引起的异常:org.springframework.web.HttpMediaTypeNotSupportedException:内容类型'application/x-www-form-urlencoded; charset = UTF-8'不支持

Resolved exception caused by Handler execution: org.springframework.web.HttpMediaTypeNotSupportedException: Content type 'application/x-www-form-urlencoded;charset=UTF-8' not supported

这是我的HTML

<form method="post" id="send-invite-mail" th:action="@{/sendmail/sendInvitations}">
  <div id="formfields">
    <div class="form-group">
      <input type="email" class="form-control" id="email1" name="email1" placeholder="Enter Email-Address ..."/>
    </div>
  </div>
  <!-- more form-groups are added here by JS -->
  <button type="submit" id="submitInvitation" class="btn btn-primary">Invite</button>
</form>

这就是我在后端用来获取自己的价值观的方法

And this is what I use in the backend to get my values

@PostMapping("/sendmail/sendInvitations")
public void getInvitationList (@RequestBody Map<String, String> formData){
   List<String> adressList = new ArrayList<String>();
   for (Map.Entry<String, String> entry : formData.entrySet()) {
      adressList.add(entry.getValue());
   }
}

现在我不知道我是否做对了.感谢任何帮助.

Right now I have no idea if I'm doing this correct or not. Appreciate any help.

推荐答案

带有 method ='post'的HTML表单以 form-url编码的形式发送数据.因此,在这种情况下,Spring无法将其理解为RequestBody.因为,带有 @RequestBody 的POST数据期望内容类型为 application/json .因此,如果您想真正将数据作为json发送,则必须删除@RequestBody批注.

HTML forms with method='post' send data as form-url-encoded. So, In this case, Spring can not understand it as a RequestBody. Because, POST data with @RequestBody expects an content type as application/json. So, if you want to really send data as json, you have to remove the @RequestBody annotation.

最后,您可以使用 consums 指定内容类型并使用以下方式之一:

1. @RequestParam 而不是 @RequestBody :

Finally, you can specify content-type with consumes and use one of the following ways:

1. @RequestParam instead of @RequestBody:

@RequestMapping(value = "/sendmail/sendInvitations",
        method = RequestMethod.POST,
        consumes = MediaType.APPLICATION_FORM_URLENCODED_VALUE, 
        produces = MediaType.APPLICATION_JSON_VALUE)
public @ResponseBody returnType methodName(@RequestParam Map<String, String> name) {
    ...
}

2..删除 @RequestBody 并使用您的POJO类:

2. Remove @RequestBody and use your POJO class:

@RequestMapping(value = "/sendmail/sendInvitations",
        method = RequestMethod.POST,
        consumes = MediaType.APPLICATION_FORM_URLENCODED_VALUE, 
        produces = MediaType.APPLICATION_JSON_VALUE)
public @ResponseBody returnType methodName(YourPOJO pojo) {
    ...
}

希望对您有帮助.

这篇关于如何在Spring后端中获取可变的HTML表单值(POST)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆