Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to pass only string in thymeleaf form?

I have a little problem. When I have an object with some fields, it's easy to pass these fields through form:

Controller:

@RequestMapping("/")
public String hello(Model model) {
    model.addAttribute("test", Test);
    return "index";
}

html:

<form th:action="@{/process}"  method="post" th:object="${test}">
<input type="text" th:field="*{value}"/>
<input type="submit" />
</form>

But what if I don't want to have an object and pass only string? Something like that:

Controller:

@RequestMapping("/")
public String hello(Model model) {
    model.addAttribute("test", "test string");
    return "index";
}

html:

<form th:action="@{/process}"  method="post">
<input type="text" th:field="${test}"/>
<input type="submit" />
</form>

doesn't work. Thanks for help!

For next question in comments:

index.html:

<form th:action="@{/process}"  method="post">
<textarea th:text="${sourceText}"/>
<input type="submit" />

ggg.html:

<textarea th:text="${sourceText}"/>

controller:

@RequestMapping("/")
public String hello(Model model) {
    model.addAttribute("sourceText", "asdas");
    return "index";
}

@RequestMapping("/process")
public String process(Model model, @ModelAttribute(value = "sourceText") String sourceText) {
    return "ggg";
}
like image 987
Helosze Avatar asked Jan 04 '17 15:01

Helosze


People also ask

How do you pass parameters in Thymeleaf?

Request parameters can be easily accessed in Thymeleaf views. Request parameters are passed from the client to server like: https://example.com/query?q=Thymeleaf+Is+Great! In the above example if parameter q is not present, empty string will be displayed in the above paragraph otherwise the value of q will be shown.

How do you check for String equality in Thymeleaf?

How do you check for string equality in Thymeleaf? You can even compare String objects in thymeleaf. Under the hood, Thymeleaf uses the String. compareTo() method from the Comparable interface.

What is th text in Thymeleaf?

The Thymeleaf th:text tag will replace all the text in your h1 tag, that is the reason your output only shows "TITLE". You should place the <small> tags outside your h1 tag.


2 Answers

th:field is used only if you declare object like th:object.

<form th:action="@{/process}"  method="post">
<input type="text" th:value="${sourceText}" name="sourceText"/>
<input type="submit" />
</form>

Spring matches values by "name" attribute. Just catch it with @RequestParam in controller like

@RequestMapping("/process")
public String process(Model model, @RequestParam String sourceText) {
    return "ggg";
}
like image 103
Алексей Виноградов Avatar answered Sep 28 '22 01:09

Алексей Виноградов


According how Spring MVC works you can't use a string as an object in a form, you need an object to encapsulate that string, because the form structure is Object and then any field linked to it.

In your case, I would create a view form object for those common situations, something like formView with the String attribute text. And you could use the same object in similar situations.

Other option if you don't want to create this extra object, you could send the data by AJAX, and build the data array to send to the controller in javascript.

Personally I would go for the first option, is more reusable.

Hope this help you

like image 36
cralfaro Avatar answered Sep 28 '22 01:09

cralfaro