javahtmlspringrestthymeleaf

How to get data using an input form through a rest call?


I want to get the name and email address from a user through following form

    <!-- 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>

I have used html and thymeleaf here. This is my controller. In the controller I want to pass parameters to the sendUserRegisterEmail method which I was taken from the user.

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

I wrote a DTO class as well.

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

}

Currently I get no errors. But the email is not receiving. I have no any issues with the method which I used to send the email. Because I have used it in another instances. The only issue is passing form data to it. Please help me to resolve this


Solution

  • @RequestParam is used to pass the request parameters e.g.
    http://example.com/path?name=myName&email=myEmail

    Whereas @RequestBody is used to get the request's body, which in most cases for REST is a json (application/json) body. So your method will be

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