Spring and thymeleaf set and get value - java

I send object myObject to view, one of values is put in input inside form with post method. How is it possible to set value to view and then get it by submit this form (but with another object type)?
I tried something like:
<form action="#" th:action="#{/myMethod}" th:object="${anotherObject}" method="post" xmlns="http://www.w3.org/1999/html">
<input style="display:none" type="text" th:name="*{fieldFromAnotherObject}" th:value="${sendedObject.fieldFromSendedObject}"/>
but the result is that name field in this html element is empty:
(value is correct). And then I have null as fieldFromAnotherObject field of anotherObject in my backend.

I am not sure why you are using two objects one for binding values to view and one for submitting,if both are of same class one object would be sufficient.Using th:field you can achieve this with single object. Try below code it should work.
<form action="#" th:action="#{/myMethod}" th:object="${anotherObject}" method="post" >
<input style="display:none" type="text" th:field="*{fieldFromSendedObject}"/>
Just in case if both are of different class objects, try below code
<input style="display:none" type="text" th:name="anotherObject.fieldFromSendedObject" th:value="${sendedObject.fieldFromSendedObject}"/>

Related

Get value of <td> in Java - request.getParameter

Im trying to get the values of some td elements where the data consist of data from MySQL table. It displays the data fine in my browser (e.g. if i change type from "hidden" to "submit"), but when I try to get the value i only get null.
Here are my jsp and it displays the correct results in the browser.
<td>
<form action="history.jsp" method="get">
<input type="hidden" name="res" value="<%=his.getRes()%>"/>
</form>
</td>
When i try to print the values however, I only get "null" at of evey :
<%
String res = request.getParameter("res");
System.out.print(res);
%>
I'm still very new, so it's proberly a straight forward answer. Thank you in advance for the help.
I suggest that you change the name of your variable :
String newname = request.getParameter("res");
System.out.println(newname)
One can submit (=send) only one <form>. So one must assume there is just one single td with one <form>. Forms also may not be nested in an outer form.
It need some way to submit the form.
So experiment first with:
<td>
<form action="history.jsp" method="get">
<input type="text" name="res" value="<%=his.getRes()%>"/>
<input type="submit" value="Send"/>
</form>
</td>
This will show whether his.getRes() yielded something. And allows a manual submit in the browser.

can't reference iteration variable in Thymeleaf

I try to iterate a list of items using th:each,
<div class="row" th:each="item : *{items}">
<input type="text" th:field="item.name"/>
</div>
it works if I access the iteration variable using th:text, but throws
java.lang.IllegalStateException: Neither BindingResult nor plain
target object for bean name 'item' available as request attribute
when I use th:field to access it, where did I do wrong?
Something like this could work:
<div class="row" th:each="item, stat : *{items}">
<input type="text" th:field="*{items[__${stat.index}__].name}"/>
</div>
Take a peek here for more info: http://forum.thymeleaf.org/I-have-problem-in-binding-the-list-of-objects-contained-inside-a-object-on-the-form-using-thymeleaf-td3525038.html
th:field is broken in several ways, this is one of them.

Put a form key-values subset in a scope parameter

I've got a form (not linked with Spring pojos) with questions and answers.
I want to retrieve a String which contains only the key-values, like Q1=A1_3&Q2=A2_1 and so on. I was doing that using Spring #RequestBody annotation.
The problem is that if I put an hidden field ("code") in the form, I'll get its value at the end of this string, and I don't want to mix the questions-answers key-values with the hidden field value.
This is my JSP:
<form name="pollform" method="post" action="1/submitpoll.do">
<input type="hidden" name="code" value="C1"></input>
<h4>T1Q1. How do you blabla?</h4>
<input type="radio" name="Q1" value="A1_1">Very Good</input><br/>
<input type="radio" name="Q1" value="A1_2">Could be better</input><br/>
<input type="radio" name="Q1" value="A1_3">Bleah.</input><br/>
<h4>T1Q2. Are you even bliblibli?</h4>
<input type="radio" name="Q2" value="A2_1">Yes sure</input><br/>
<input type="radio" name="Q2" value="A2_2">Not everytime</input><br/>
<input type="radio" name="Q2" value="A2_3">Never</input><br/>
<input type="submit" value="Submit"/>
</form>
This way, using #RequestBody in my controller I'll get something like Q1=A1_2&Q2=A2_3&code=C1 (and it's not what I want since I have to convert the result string in a HashMap later).
So, how can I get only the questions key-values of the form, excluding the hidden input value?
Should I use jstl or something else?
If you need map of question-answer pairs and the hidden code parameter, I suggest to introduce model object, which will contain the code and the map, and let the spring make the mapping for you. Checkout this example. There is map of contact details to be saved by controller, similar to your map of answers to questions.

How to pass two objects to use in a form using thymeleaf?

My problem is the following :
I've 2 differents objects that I've to fill from a single form.
With 1 object, I simply do in the newFoo.html:
<form th:object="${foo}" th:action="#{/foo}" method="post">
<input type="text" th:field="*{name}"/>
<button type="submit">Go</button>
</form>
and in the FooController:
#RequestMapping(value = "/foo/new", method = RequestMethod.GET)
public String newFoo(final Foo foo, Model model) {
return "newFoo";
}
#RequestMapping(value = "/foo/new", method = RequestMethod.POST)
public String saveFoo(final Foo foo, final BindingResult bindingResult, Model model) {
fooService.save(foo);
return "redirect:/foo/new";
}
Let's say I've an other object bar with a "status" variable in it. How can I do to pass that object so I can submit the input within the same form?
Like:
<form th:object="${foo} && ${bar}" th:action="#{/foo}" method="post">
<input type="text" th:field="*{name}"/>
<input type="text" th:field="*{status}"/>
<button type="submit">Go</button>
</form>
So far I tried to do with to fieldset with a th:object in it, that doesn't work, I tried to put two th:object in the form, that doesn't work either.
The only way I found is to build an other object containing those two objects, and pass it. That works well, but I can't create that kind of object, it's nonsense (even if it works).
Of course, the objects aren't as simple as Foo and Bar here, otherwise I would have merge those two. But that's not something I can do.
Is it even possible to pass two objects like that to use in a form ?
Thanks already.
I don't think you need to use two th:objects. Just use th:value
<form th:action="#{/foo}" method="post">
<input type="text" th:value="${foo.name}" name="name"/>
<input type="text" th:value="${bar.status}" name="status"/>
<button type="submit">Go</button>
</form>
I would think Spring is smart enough, on the controller side, to use its mapping techniques to map your fields to their proper command object, foo or bar.
i used a div tag to surround the form input for my second object and added a th:object..... the controller processed it and added it to the database.
<form method=post th:object="${object1}" >
<div th:object="${object2}" >
code......
</div>
<input type="submit" />
</form>

Defining POJO id in Spring form for single object

this is what controller sends to me:
model.addAttribute("weather", weatherService.getWeatherByCity(id));
this is my JSP:
<form:form commandName="newWeather" method="post" action="edit">
<c:forEach items="${cities}" var="city">
<form:input path="temperature"></form:input>
<input type="submit" value="Submit">
</c:forEach>
</form:form>
Problem:
I get one object from database named weather. I want to edit that by changing temperature. So I must send back atleast id and field temperature. I know how to send back temperature as shown, but how can I send back my id.
I think I can get it from model by ${weather.id}, but how can I place it in form?
<input type="hidden" name="id" value="${weather.id}">

Categories