Put a form key-values subset in a scope parameter - java

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.

Related

Spring, Java, HTML: How can I populate an HTML form with values?

I want to write a part of a website that lets the user alter the data of a pre existing book. For that, I am trying to use a form which works fine. I just can't figure out how to get the form to display data in the editing fields so the user doesn't have to enter everything again but can simply change some details. My HTML code looks like this:
<form method="post" role="form" class="ui form" id="bookForm" th:action="#{/editBook}" th:object="${bookForm}">
<div class="field">
<label for="name">Book</label>
<input id="name" name="name" th:field="*{name}" th:errorclass="fieldError" type="text" required="required"/><br/>
</div>
I include some more code about errors and other things but this is basically where I want the form not only to pass values to my java file but also to take values about the book and display them in the editing fields.
I think I need to pass the book that the user wants to edit into this form but I'm not sure how. I have tried:
<input type="hidden" id="currentBook" name="currentBook" th:value="${currentBook}"/>
right before the "div" statement and then passing the "currentBook" into HTML with
model.addAttribute("currentBook", currentBook);
in my #GetMapping method of that website. I then changed the "input" statement in my field as well to
<input id="name" name="name" th:field="*{name}" th:value="${currentBook.name}" th:errorclass="fieldError" type="text" required="required"/><br/>
currentBook.name will give me the name of that book just not within this context. Does anyone know what I'm doing wrong and how it will work?
Thank you in advance!

Spring and thymeleaf set and get value

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}"/>

Multiple Forms in a single JSP having fields with the same name/id attribute

I have two forms in a JSP page.
<form id="myForm1">
<input type="text" name="formText" id="formText"></input>
.
.
.
</form>
<form id="myForm2">
<input type="text" name="formText" id="formText"></input>
.
.
.
</form>
In my JavaScript I'm able to make a distinction between the two text boxes despite both of them having the same name/ID using the form ID like this
document.forms["myForm1"]["formText"].value and
document.forms["myForm2"]["formText"].value return different values.
How can I make this distinction in Java code? For example, what will request.getParameter("formText") return? I want to be able to make this distinction in my business end. Please help.
Element IDs should be unique within the entire document and that's a rule you should follow. Btw, you could use 2 input hidden fields with same name but different values, then on server side you should be able to reach your goal.
<form id="myForm1">
<input type="text" name="formText" id="formText"></input>
<input type="hidden" name="whichform" value="myForm1"></input>
.
.
</form>
<form id="myForm2">
<input type="text" name="formText" id="formText"></input>
<input type="hidden" name="whichform" value="myForm2"></input>
.
.
</form>
If your business logic for the two forms differs, you could consider targeting the form's actions to different handlers - one for each form.
<form id="myForm1" action="form1action.jsp">
In that case form1action.jsp will receive all values if form1 is submitted, form2action.jsp all values for form2.jsp
This allows a cleaner design as you wouldn't need to touch the form1 stuff if you add a field to form2.
Of cause Servlets might be better suited as handlers...

How to send label parameter from JSP to servlet?

I have a jQuery dialogue box which contains values as checkboxes. On selecting the checkboxes I am storing the selected values into label. Next I have to send these values from label as parameter through form to servlet but I don't know how to complete it.
Here is my code:
<form action="CallTimer" method="GET">
<label class="button2">Set Date: </label>
<input type="text" name="date" id="date" size="4">
<input type="Submit" name="Submit" value="Submit" id="Submit">
<br/>
Select Reporting Level
<label class="button2" style="display:none" id="depart"> Department</label>
</form>
I am retrieving these parameters in my Servlet as:
String reportname=request.getParameter("depart");
System.out.println(reportname);
But it is returning null values. Please help me.
Thanks in advance.
You have to use hidden input field:
<input type="hidden" name="depart" />
You need to understand what gets passed on form submission and what is not. In a nutshell, only values of the input fields get sent to the server. You have several ways to solve your problem:
Write value to a hidden input field
Modify the query string (what gets sent after ? in your GET request) during form submission (using java script):
?...&depart=xxx

What is <spring:bind> for? When to use it, and when not to use it?

I am having problems with submitting form data in spring. <spring:bind> seems to be a part of the solution. See my full problem here.
The documentation of BindTag found here is not clear to me. Why is <spring:bind> needed in some cases to submit data, while it is not needed in most cases?
What are the typical cases where the <spring:bind> must be used in order for a form to function properly?
You will find the tag <spring:bind> useful when you want to parse multiple objects from an input form. Here's a modified example from the Spring's doc (http://docs.spring.io/spring/docs/1.2.6/taglib/tag/BindTag.html):
<form method="post">
## now bind on the name of the company
<spring:bind path="company.name">
## render a form field, containing the value and the expression
Name: <input
type="text"
value="<c:out value="${status.value}"/>"
name="<c:out value="${status.expression}"/>">
</spring:bind>
<spring:bind path="address.street">
Name: <input
type="text"
value="<c:out value="${status.value}"/>"
name="<c:out value="${status.expression}"/>">
</spring:bind>
<input type="submit">
</form>
Although I've never used this tag myself, my understanding of the documentation is this. A tag will provide you with information about the binding status of a form property to a bean. For example in:
<form:form modelAttribute="employee">
<form:input path="name"/>
<spring:bind path="name"/>
<spring:bind path="employee"/>
</form:form>
The tag will display (or expose through a BindStatus object) all errors that have occurred with the name attribute (the first case) and all errors on the Employee entity and its attributes (the second case). I am not sure that this tag has anything to do with the succesfulness of submitting data, but rather that it's used as an information tool.

Categories