如何通过REST调用使用输入表单获取数据?

内容来源于 Stack Overflow,并遵循CC BY-SA 3.0许可协议进行翻译与使用

  • 回答 (2)
  • 关注 (0)
  • 查看 (130)

我想通过以下表格从用户那里获得姓名和电子邮件地址

    <!-- Contatct Us-->
<section>
    <div class="grey-bg">
        <div class="container p-y-40">
            <h1>Contact Us</h1>
            <div class="row">
                <div class="col-md-6">
                    <div class="form-horizontal contact-us" th:action="@{/contact-us}">
                        <div class="form-group">
                            <label for="FirstName" class="col-sm-3 control-label mandatory">Name</label>
                            <div class="col-sm-9">
                                <input type="text" class="form-control" th:value="${name}" placeholder=""/>
                                <input name="name" type="text" th:value="${name}" placeholder=""/>
                                <span th:text = "${name}"/>
                            </div>

                        </div>
                        <!-- -->
                        <div class="form-group">
                            <label for="Email" class="col-sm-3 control-label mandatory">Email</label>
                            <div class="col-sm-9">
                                <input type="email" class="form-control" th:value="${email}" placeholder=""/>
                                <input type="email" name="email" class="form-control" th:value="${email}" placeholder=""/>

                            </div>
                        </div>
                        <!-- -->
                        <div class="form-group">
                            <label for="Telephone" class="col-sm-3 control-label">Telephone</label>
                            <div class="col-sm-9">
                                <input type="text" class="form-control" th:value="${telephone}" placeholder=""/>
                            </div>
                        </div>
                        <!-- -->
                        <div class="form-group">
                            <label for="Message" class="col-sm-3 control-label ">Message</label>
                            <div class="col-sm-9">
                                <textarea class="form-control" rows="4" th:value="${message}"> </textarea>
                            </div>
                        </div>
                        <!--  -->
                        <div class="form-group">
                            <div class="col-sm-9 col-sm-offset-3 col-md-offset-3">
                                <input type="button" value="Send" class="btn btn-primary btn-flat w-min-120"
                                       data-toggle="modal" data-target="#myModal" />
                            </div>
                        </div>
                        <!--  -->
                    </div>

                </div>
                <div class="col-md-6">
                    <iframe src="https://www.google.com/maps/embed?pb=!1m18!1m12!1m3!1d63371.792586563766!2d79.85603378454613!3d6.922006503004973!2m3!1f0!2f0!3f0!3m2!1i1024!2i768!4f13.1!3m3!1m2!1s0x0%3A0x5f6f0f981a359f05!2sDialog+Axiata+PLC!5e0!3m2!1sen!2slk!4v1503969814382" width="100%" height="350" frameborder="0" style="border:0" allowfullscreen="true"></iframe>
                </div>
            </div>
        </div>
    </div>
</section>

我在这里用过html和hymeleaf。这是我的控制器。在控制器中,我希望将参数传递给sendUserRegisterEmail方法,该方法是从用户那里提取的。

@Autowired
    private EmailService emailService;

    @ResponseBody
    @RequestMapping(value = "/contact-us", method = RequestMethod.POST)
    public String showContactForm(@RequestParam("name") String contactName, @RequestParam("email") String contactEmail) {
        emailService.sendUserRegisterEmail(contactName, contactEmail);
        return "index";
    }

我还写了一个DTO类

public class ContactUsDto {

    private String name;

    private String email;

    private String telephone;

    private String message;

    public ContactUsDto(String name, String email, String telephone, String message) {
        this.name = name;
        this.email = email;
        this.telephone = telephone;
        this.message = message;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

}

目前我没有错误。但这封邮件没有收到。我对发送电子邮件的方法没有任何问题。因为我在另一个例子中用过它。唯一的问题是将表单数据传递给它。请帮我解决这个问题

提问于
用户回答回答于

@RequestParam用于传递请求参数,例如 http://example.com/path?name=myName&email=myEmail

然而@RequestBody,用于获取请求的主体,在大多数情况下,REST是json(application / json)主体。所以你的方法将是

@ResponseBody
@RequestMapping(value = "/contact-us", method = RequestMethod.POST)
public String showContactForm(@RequestBody ContactUsDto contactUsDto ) {
    emailService.sendUserRegisterEmail(contactUsDto.getContactName(), contactUsDto.getContactEmail());
    return "index";
}
用户回答回答于

添加到你的输入类型名称如下

 <input type="email" name="email" class="form-control" th:value="${email}" placeholder=""/>
 <input name="name" type="text" th:value="${name}" placeholder=""/>

扫码关注云+社区

领取腾讯云代金券