Samir Amanov
Samir Amanov

Reputation: 169

Pass String from thymeleaf to controller

I need pass String from the text area to doc variable in the controller. Please help.

HTML:

<div>
<textarea rows="10" cols="100" name="description"></textarea>
button class="button" onclick="window.location.href ='/send';">Send</button>
</div>

Controller:

@GetMapping("/send")
public String send(String doc) {

    service.sendDoc(doc);

    return "mainpage";
}

Upvotes: 0

Views: 2134

Answers (3)

Samir Amanov
Samir Amanov

Reputation: 169

I found answer to this question:

Get value from Thymeleaf to Spring Boot

<form th:action="@{/send}" method="get">
   <textarea  th:name="doc" rows="10" cols="100" name="doc"></textarea>
   <button type="submit">Send</button>
</form>


@GetMapping("/send")
public String send(@RequestParam(name="doc", required = false) String doc) {
    //change required = false as per requirement
    System.out.println("Doc: "+doc);
    return "textarea-input";
}

Note: use "th:field" for Entity/Model

Upvotes: 0

AmirBll
AmirBll

Reputation: 1119

you can use post method:

 <form action="/send" method="POST">
        <textarea rows="10" cols="100" name="description"></textarea>
        <button type="submit">Submit</button>
 </form >

contorller:

@PostMapping("/send")
public String send(@RequestParam("description") String description) {

    service.sendDoc(description);
    return "mainpage";

}

Upvotes: 1

Istiaque Hossain
Istiaque Hossain

Reputation: 2357

to getting data from front end by using GET method is a bad decision.....
by the way you can try this code

<form action="/send" method="GET">
    <textarea rows="10" cols="100" name="description"></textarea>
    <button type="submit">Submit</button>
</form >

controller code like this

@GetMapping("/send")
public String send(HttpServletRequest request) {

    String doc= request.getParameter("description");
    service.sendDoc(doc);

    return "mainpage";
}

Upvotes: 0

Related Questions