th:attr is showing value attribute as empty string - java

I've got a form where the inputted data gets mapped to the correct variable by using th:field. But when the page loads, I want the inputfield to show a default value. So that when the user does not enter any data, the default value gets mapped to the variable.
th:field overrides th:value, therefore I added:
th:attr="value=${field.placeholder}", which I found out about in another thread (link in comment), but this doesn't seem to work. When I inspect the element, it shows value as: value="".
I know field.placeholder isn't empty, because th:placeholder="${field.placeholder}" does show the value.
<form th:action="#{/generateData}" th:object="${stepObject}" method="post" class="form-group">
<div th:each="field, iterStat : ${stepEntry.value}">
<label th:text="${field.name}"></label><input type="hidden" th:field="*{name}" th:attr="value=${field.name}"/>
<input th:type="${field.type}" class="form-control" th:maxLength="${field.length}"
th:field="*{value}" th:attr="value=${field.placeholder}" th:placeholder="${field.placeholder}"/>
</div>
<button type="submit" class="btn btn-primary" id="generateButton">Generate away!</button>
</form>
So here in the hidden input it should have the value ${field.name}, but value is "".
in the other input it should have the value ${field.placeholder}, but this value also is "".
Any suggestions on why this is and how I could get this to work?
When I enter data in the inputfields, they get mapped to the value variable of stepObject. But it's important that the user can also just press the button without entering anything.
Thanks in advance!

I gave up on using the th:field and ended up just using the #RequestParam("form-name-attribute") in my controller method to get the (default) value and manually set it to the correct object.
I would've liked to do it with thymeleaf functionality though.
So if anyone still finds a solution for it, please let me know so I can learn! :-)

Related

How to fetch default value from an input into a servlet?

I have an input type which has a default value. I want to fetch its value inside of a servlet. So, which method will work in this case?
I tried using request.getParameter("FLC1"), but that didn't work as well.
Following is the input code:-
<input type="text" name="FLC1" id="FLC1" value="Floral Lavender Candle" disabled>
Thank you
Since your input element has a disabled attribute so it will not be submitted on form submit, instead make it a readonly and it should work.
<input type="text" name="FLC1" id="FLC1" value="Floral Lavender Candle" readonly>
Alternatively you can put a hidden filed in the form with same name attribute.
<input type="hidden" name="FLC1" value="Floral Lavender Candle">
You can:
Make the value server side an populate it in the jsp using el:
value="${myvariable}"
Store the value in a hidden field, it will be accessible then

get value of attribute outside form tag from jsp to servlet

i have one view where i m showing many records and now i m adding functionality for user to download those records which he will choose date from search textbox. As my search textbox in jsp outside of form tag and thus i m not getting the value of that parameter in my servlet..is there any way to get that value from outside of form tag? Here is my jsp
<div id="divOfDateTable">
Search:<input type="text" name="dropdown" id="datedropdown">
<button id="dateButton" name="dateSearch">Search</button>
</div>
<form action="Download_Servlet" class="download" method="post">
<input type="submit" id="downloadRecords" value="Download Order-records">
</form>
In my Servlet i want to get that parameter value.i.e;Date put by user.As per the requirement only the searched records by the user needs to be downoaded. So please guys help out..

How to pass value of more than one variable in jsp?

I want to pass values of two variables when a link is clicked to another page I am using query parameter but I am only able to send one variable through it. I know about session.setAttribute() but don't know how can I use it based upon links...Foreg:
<p> < </p>
<a href="Search.jsp?item=<%=search%><%session.setAttribute("val",value);%>" class="classname1" > > </a>
This is my code I know its wrong..I just want is If I click on first link than value1 should be passed and If I click on 2nd link value should be passed.P.S.:I have already passes search variable through query parameter but now If I try to pass second parameter through session only the final value i.e second initialized value only counts? what to do?
EDIT:
suppose my code is this:
<form class="navbar-form navbar-right" action="Search.jsp" method="get">
<input type="text" class="form-control" placeholder="Search..." name="search">
Here one variable search is passes through form How can I pass another variable value?should it be like:
<form class="navbar-form navbar-right" action="Search.jsp?item1=<%=value%>" method="get">
<input type="text" class="form-control" placeholder="Search..." name="search">
You can send multiple parameters like,
href="Search.jsp?item=<%=search%>&item2=value2&item3=value3.."
Also to add <%session.setAttribute("val",value1);%> will be executed at server side irrespective of the click of the hyperlink.
For the form you can add another input parameter in the form,
<input type="text" name="item1" value="<%=value%>">
You need to add some separator in between two values e.g.#
And while reading at server side you can split those values based on that separator
You may try using this example to send multiple values:
With same name
With diff name
Separate the two values by using &:
<a href="Search.jsp?item=<%=search%>&item2=<%=value2%>">
On your search.jsp fetch values as :
request.getParameter("item");
request.getParameter("item2");
You need to call session.getAttribute in the place where you are calling session.setAttribute and session.setAttribute should be called in controller or in the jsp before the link tag to set the value. Please separate values like Search.jsp?item1=value1&item2=value2

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

value input field in foreach to input field outside foreach

The problem I have is best explained with a code example:
I have the following VIEW.jsp:
<c:forEach var="widget" items="${widgets}">
<div class="drag">
<p>Id: ${widget.id}</p>
<input class="editWidget" type="image" src="/tis/img/icons/edit.png" alt="Edit widget">
<input class="idWidget" type="hidden" value="${widget.id}">
</div>
</c:forEach>
<div id="editDialog" title="Edit widget">
<fieldset>
<input class="editWidgetId" type="hidden" value="??" id="editWidgetId">
</fieldset>
</div>
editWidgetId should have its 'value' attribute filled with the value of idWidget inside the foreach loop. This value should be different for each element in the loop (element is selected by edit button).
The questions:
How can I get the value of one input field to another input field?
How can I do this when a foreach loop is present?
Thanks in advance
Given you want dynamic behavior, this work has to be executed on the client-side by JavaScript as at the server-side, you have many widgets to one editDialog and are lacking the client-side user event to make your decision.
What you want to do is assign a function handler (or statement in my example below) to each editWidget to change the value of the editWidgetId input box with the appropriate value:
<input onclick="document.getElementById('editWidgetId').value = '${widget.id}'" class="editWidget" type="image" src="/tis/img/icons/edit.png" alt="Edit widget" >
Haven't tested this but I hope you get the idea

Categories