如何通过休息呼叫使用输入表单获取数据?

时间:2017-10-02 08:01:44

标签: java html spring rest thymeleaf

我希望通过以下表单获取用户的姓名和电子邮件地址

    <!-- 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和thymeleaf。这是我的控制器。在控制器中,我想将参数传递给我从用户那里获取的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;
    }

}

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

2 个答案:

答案 0 :(得分:1)

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

答案 1 :(得分:1)

添加您的输入attibute名称,如此

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

再试一次。