How do I submit the right value? - java

<c:forEach items="${movieList}" var="movie" varStatus="status">
<tr class="<c:if test="${status.count % 2 == 0}">even</c:if>">
<td>${movie.title}</td>
<td>${movie.genre}</td>
<td>${movie.year}</td>
<td>${movie.boxoffice}</td>
<td>
<form:form action=edit.htm>
<input type="hidden" name="edit" value="movie name">
<input type="submit" value="Edit">
</form:form>
<form:form action=delete.htm>
<input type="hidden" name="delete" value="movie name">
<input type="submit" value="Edit">
</form:form>
</tr>
</c:forEach>
This is the section of code I have at the moment. The idea is to display movie data, but also provide buttons to send the user to either a filled form page to edit the data or simply delete the respective data and redirect to the same page. I am just unsure as how to pass along the movie object. I know it is simple, but I can't find the reference I used previously...
Thanks.

U can try this :
put this object in a hidden input
<input type="hidden" id="hidden_object" value="" name="hidden_object"></input>
than you can access the value with the method getParameter()
OR
put this object in your session
in your jsp
<% session.setAttribute("movie", ${movie}); %>
good luck

Related

JSP FORM POST : empty attributes

I can't find one attribute sent from a JSP form.
<form name="shuntingForm" action="/TCS_FUND/ShuntAction.aen" method="POST">
<div class="field-item clearfix">
<input type="hidden" name="csrfPreventionSalt" value="<%=CsrfPreventionSalt%>" />
<input type="hidden" name="idSession" value="<%=idSession%>">
Funds Trading<br>
</div>
</form>
request.getAttribute("idSession") & request.getParameter("idSession") show NULL.
Any ideas ? thanks
I would simplify this to return a fixed value.
Change:
<input type="hidden" name="idSession" value="<%=idSession%>">
To:
<input type="hidden" name="idSession" value="123456789">
Once that works, figure out the difference.
You also may want to view the source of the web page. If your idSession does not have the value you expect, your input tag will show the wrong value field.

JSTL: Setting different values to a hidden attribute in a form inside a foreach on every iteration

I am iterating through a list on my JSP page using foreach. Based on this iteration, I set different values to table rows. In one column, there is a hyperlink which holds reference to a form so that onclick of this hyperlink, the form gets submitted. This form has a hidden attribute which should hold the value of a item in the list in the current iteration. Onclick, the form with this value of the list item in the hidden attribute gets submitted.
The problem is this that only the first item in the list is getting set to this hidden attribute in every iteration. The next value never gets assigned.
However, this is not the case for the other columns in the table. They have fresh values in each row/tuple.
<c:forEach items="${myList}" var="item">
<!--iterating through this list. Some code here-->
<form action="callthis.jsp" id="request_form" method="post" target="_blank">
<input type="hidden" id="requestxml" name="requestxml" value="${item.requestxml}" /></form>
<td>
<a href="javascript:document.getElementById('request_form').submit();"id="request">
requestXML </a>
</td>
<!--some code here-->
You have 2 options:
//1- Only one form
<form action="callthis.jsp" id="request_form" method="post" target="_blank">
<c:forEach items="${myList}" var="item">
<!--iterating through this list. Some code here-->
<input type="hidden" id="requestxml" name="requestxml" value="${item.requestxml}" />
<td>
requestXML
</td>
</c:forEach>
</form>
//2- render multiple forms with different ID using varStatus
<c:forEach items="${myList}" var="item" varStatus="status">
<!--iterating through this list. Some code here-->
<form action="callthis.jsp" id="form_${status.index}" method="post" target="_blank">
<input type="hidden" id="requestxml" name="requestxml" value="${item.requestxml}" /></form>
<td>
requestXML
</td>
</form>
</c:forEach>
You have to assign unique id to each form because there are multiple form with same id so it takes first one. Refer this code .This is just for example.
<c:forEach begin="1" end="5" var="item" varStatus="status">
<form action="callthis.jsp" id="request_form${status.index}" method="post"
target="_blank">
<input type="hidden" id="requestxml" name="requestxml" value="${item}" />
</form>
<td><a
href="javascript:document.getElementById('request_form${it‌​em}').submit();"
id="request"> requestXML </a></td>
</c:forEach>

jsp form,servlet doesn't get parameters

I have form in jsp
<table border="1">
<c:forEach items="${sessionScope.proizvodList}" var="proiz">
<tr>
<td>${proiz.sifra}</td>
<td> ${proiz.naziv}</td>
<td>${proiz.boja}</td>
<td>${proiz.dimenzije}</td>
<td>${proiz.tezina}</td>
<td><form action="KupiServlet" method="get">
<input type="text" name="kolicina"/>
<input type="hidden" name="id" value="${proiz.sifra}"/>
<input type="submit" value="Kupi"/>
</form>
</td>
</tr>
and in servlet in doGet method,i have
String id=request.getParameter("id");
String kol=request.getParameter("kolicina");
When I run this,i got error that both id and kol are null,so i guess servlet doesn't get these parameters..but i think that my form looks good..Does someone have idea why this happens?Thanks in advance!
Check this link - you cannot have a form inside a form.
Is it valid to have a html form inside another html form?

input value text field in jsp error

My index.jsp is as following:
<form action="FileUploadServlet" id="formSubmit" method="post" enctype="multipart/form-data">
<input type="text" id="txtFileName" value="${fname}"/>
<input type="file" name="fileName" id="selectedFile" style="display: none;">
<input type="button" id="btnBrowse" value="Browse..." onclick="document.getElementById('selectedFile').click();" />
<input type="submit" value="Upload" id="btnUpload">
</form>
with value=${fname} is get from dopost method in servlet when form summited
request.setAttribute("fname", fileName);
getServletContext().getRequestDispatcher("/index.jsp").forward(
request, response);
But it's weird that when I deployed index.jsp
My text field always show ${fname} in text area, even after form submitted, its still get that value (correctly it must show 'filename')
Does anyone meet this problem like me?
First thing is that, you are using wrong syntax to display value in JSP.
Below syntax is wrong.
value="${fname}"
We use expression tag fordisplaying value in JSP page. Expression tag convert into Java statement.
<%=request.getAttribute("fname")%>
When you first time open your index.jsp page, it will show you blank value and when request will come back from server side, then it will show you correct result.
Use below code.
<%if(request.getAttribute("fname")!=null){%>
<input type="text" value ="<%=request.getAttribute("fname") %>"/>
<%}else
{ %>
<input type="text"/>
<%} %>
In JSP, the correct syntax to retrieve the values is
<%=request.getAttribute("fname")%>
This is known as assignment OR expression tag (<%=%>).
Try this ...
<form action="FileUploadServlet" id="formSubmit" method="post" enctype="multipart/form-data">
<input type="text" id="txtFileName" value="<%=request.getAttribute("fname")%>"/>
<input type="file" name="fileName" id="selectedFile" style="display: none;">
<input type="button" id="btnBrowse" value="Browse..." onclick="document.getElementById('selectedFile').click();" />
<input type="submit" value="Upload" id="btnUpload">
</form>
More related to this, here.

setting class and other attributes for spring form tag element

I have the following page and it does not load
<form:form action="/test.htm" method="post" commandName="demoForm" >
<div id="testSection" style="margin-top: 1.5%;margin-left: 3.5%;">
<span class="test-container">
<label>UserName</label>
<span class="test-container-right">
<form:input path="username" value="${UNAME}" class="text simpleTextField" maxlength="200" style="width:60%" disabled/>
</span>
</span>
<span style="width:auto; padding-left: 30%; padding-bottom: 4%; text-align:center; float:right; clear:both;">
<input type="submit" class="btn" style="width:auto;" value="Save" />
</span>
</div>
</form:form>
but when I change it to
...
<span class="test-container-right">
<form:input path="username" />
</span>
...
it works and loads correctly. Why am I not allowed to set html properties for form:input spring tag. How can I achieve this? On inspecting the element it expands to be
<input id="username" type="text" value="" name="username"></input>
I need to populate its value as well as provide it with a class and additional attributes like width.
Ok I resolved some part of it. Looks like tags used inside form:input tag are different than regular html tags. All of them are listed here. For ex style is cssStyle.
So I change my code to
<form:input path="username" cssClass="text simpleTextField" maxlength="200" cssStyle="width:60%" disabled="true"/>
and now it works..
I still don't know how to populate value in this input. These seems no equivalent of value keyword.
#Aniket You actually have an equivalent , consider in a case you have to populate the select box with the values from the model attribute . You can make use of items attribute.
For instance ,
<tr>
<td>City :</td>
<td><form:select path="city" items="${cityList}" /></td>
</tr>
It will generate the select with the the list of objects.
cityList here refers the object that has been sent from the server side.
Hope this helps !

Categories