using <form> within <foreach> a few time - java

I'm pretty new to Spring and I'm trying to use Spring MVC + JSP + JSTL. My goal is to make JSP which containts list of Users and allow to edit each User separately from others. So I think I should use separate <form> tag and separate <sumbit> button for every User in the list and my JSP looks like:
<c:forEach items="${userList}" var="currentUser" varStatus="index">
<form:form method="post" action = "edit" commandName="userList[${index}]">
<tr>
<td><form:input path = "userList[${index}].login" value = "${currentUser.login}" /></td>
<td><form:input path = "userList[${index}].password" value = "${currentUser.password}" /></td>
<td><form:input path = "userList[${index}].smtpServer" value = "${currentUser.smtpServer}" /></td>
<td><form:input path = "userList[${index}].popServer" value = "${currentUser.popServer}" /></td>
<form:hidden path="userList[${index}].id" value=""/>
<td>
<spring:message code="label.delete" />
</td>
</tr>
<input type="submit" value = "edit">
</form:form>
</c:forEach>
the idea is to have an opportunity to edit each user separately by pressing the button "edit". Ofcourse this code doesn't work. It gives me an exception:
java.lang.IllegalStateException: Neither BindingResult nor plain
target object for bean name 'userList[javax' available as request
attribute
I'm really totally noob at Spring and at web-programming too. I'll appreciate any help.

Your use of ${index} isn't what you think. To get the index of the current item you must append ".index" to your varStatus variable... in your case it would be ${index.index}. For clarity, consider naming your varStatus something other than index...
<c:forEach items="${userList}" var="currentUser" varStatus="uStatus">
<form:form method="post" action = "edit" commandName="userList">
<tr>
<td><form:input path = "userList[${uStatus.index}].login" value = "${currentUser.login}" /></td>
<td><form:input path = "userList[${uStatus.index}].password" value = "${currentUser.password}" /></td>
<td><form:input path = "userList[${uStatus.index}].smtpServer" value = "${currentUser.smtpServer}" /></td>
<td><form:input path = "userList[${uStatus.index}].popServer" value = "${currentUser.popServer}" /></td>
<form:hidden path="userList[${uStatus.index}].id" value=""/>
<td>
<spring:message code="label.delete" />
</td>
</tr>
<input type="submit" value = "edit">
</form:form>
</c:forEach>
Keep in mind, this doesn't make sense using a different form for each user... but say you wanted to edit any/all of your users at once....
<form:form method="post" action = "edit" commandName="userList">
<c:forEach items="${userList}" var="currentUser" varStatus="uStatus">
<tr>
<td><form:input path = "userList[${uStatus.index}].login" value = "${currentUser.login}" /></td>
<td><form:input path = "userList[${uStatus.index}].password" value = "${currentUser.password}" /></td>
<td><form:input path = "userList[${uStatus.index}].smtpServer" value = "${currentUser.smtpServer}" /></td>
<td><form:input path = "userList[${uStatus.index}].popServer" value = "${currentUser.popServer}" /></td>
<form:hidden path="userList[${uStatus.index}].id" value=""/>
<td>
<spring:message code="label.delete" />
</td>
</tr>
</c:forEach>
<input type="submit" value = "edit">
</form:form>

I dont understant why you are using userList[${index}] since you have a different form for each user. Anyway your code isnt right at this line commandName="userList[${index}]"
Here is what I suggest :
<c:forEach items="${userList}" var="currentUser" varStatus="index">
<form:form method="post" action = "edit" commandName="user">
<tr>
<td><form:input path = "login" value = "${currentUser.login}" /></td>
<td><form:input path = "password" value = "${currentUser.password}" /></td>
<td><form:input path = "smtpServer" value = "${currentUser.smtpServer}" /></td>
<td><form:input path = "popServer" value = "${currentUser.popServer}" /></td>
<form:hidden path="id" value=""/>
<td>
<spring:message code="label.delete" />
</td>
</tr>
<input type="submit" value = "edit">
</form:form>
</c:forEach>
According that your user class is user.

Related

Multiple submit buttons with different value

I need to capture the value of the submit button but pressing the button only sends me the value of the first generated button.
This is my code
modificarVivienda.jsp:
<table border="1">
<thead>
<tr>
<td>
Codigo vivienda
</td>
<td>
Direccion
</td>
<td>
Numero
</td>
<td>
Tipo vivienda
</td>
<td>
Condominio
</td>
<td>
Rut propietario
</td>
</tr>
</thead>
<tbody>
<c:forEach items="${lstviviendas}" var="v">
<tr>
<td>
${v.cod_vivienda}
</td>
<td>
${v.direccion}
</td>
<td>
${v.numero}
</td>
<td>
${v.tipo_vivienda}
</td>
<td>
${v.nombre_condominio}
</td>
<td>
${v.rut_propietario}
</td>
<input type="text" value="${v.cod_vivienda}" name="cod_vivienda" hidden="true">
<td>
<input type="submit" value="Modificar" id="btnModificar">
</td>
</tr>
</c:forEach>
</tbody>
</table>
Servlet:
#Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
int cod_vivienda = Integer.parseInt(request.getParameter("cod_vivienda"));
DAOVivienda dv = new DAOVivienda();
HttpSession session = request.getSession();
ArrayList<Vivienda> vivienda = dv.buscarPorId(cod_vivienda);
session.setAttribute("viviendaAModificar", vivienda);
response.sendRedirect("vivienda/modificar.jsp");
}
"I need to capture the value of the submit button"
None of your submit buttons have a name, so no value is submitted for the button.
I think you meant to say that you want the value of the related <input> named cod_vivienda.
"pressing the button only sends me the value of the first generated button"
That is not correct. All the cod_vivienda values are submitted. You probably just called getParameter("cod_vivienda"), and javadoc says:
If you use this method with a multivalued parameter, the value returned is equal to the first value in the array returned by getParameterValues.
As you can see, to get all the values, you have to call getParameterValues("cod_vivienda").
To actually get a single parameter named cod_vivienda, with the value matching the button pressed, you should use the <button> element, which can associate a value with the button, separate from the displayed text.
Replace:
<input type="text" value="${v.cod_vivienda}" name="cod_vivienda" hidden="true">
<td>
<input type="submit" value="Modificar" id="btnModificar">
</td>
With:
<td>
<button type="submit" name="cod_vivienda" value="${v.cod_vivienda}">Modificar</button>
</td>
Now, only the name/value pair of the button pressed will be submitted to the server.

Iterating a table row in jsp

1 [result page]2 [chooseMedicine page]How do i iterate a table row in my chooseMedicine.jsp page without loosing previous data selected by user on result.jsp page.I am loosing the previous selected medicine when i click on continue shopping.Is this possible by scriptlet tag?.....Thank you for your help.
<tr>
<td><input type="text" value="<%=request.getAttribute("Med")%>"></td>
<td><input type="text" value="<%=request.getAttribute("NumberOfTab")%>"</td>
<td><input type="text" value="<%=request.getAttribute("Cost")%>"/></td>
<td><input type="text" value="<%=request.getAttribute("TotalPrice")%>"/></td>
</tr>
You can try this
<% for(int i=0;i<10;i++){ %>
<tr>
<td><input type="text" value="<%=request.getAttribute("Med")%>"></td>
<td><input type="text" value="<%=request.getAttribute("NumberOfTab")%>" </td>
<td><input type="text" value="<%=request.getAttribute("Cost")%>"/></td>
<td><input type="text" value="<%=request.getAttribute("TotalPrice")%>"/></td>
</tr>
<% } %>
You can change the for loop condition based on your requirement.

Displaying List elements in JSTL <c:/forEach> with counter variable

I have a scriptlet for-loop that displays movie schedules from a List<Schedule>.
<%
#SuppressWarnings("unchecked")
List<Schedule> scheduleList =
(ArrayList<Schedule>) session.getAttribute("scheduleList");
for (int ctr = 0; ctr < scheduleList.size(); ctr++) {
%>
<tr>
<td class="center">
<input type="radio" name="selection" value="<%=ctr%>" required title="Please select a schedule." />
</td>
<td><%=scheduleList.get(ctr).getMallId()%></td>
<td class="center"><%=scheduleList.get(ctr).getScheduleCinema()%></td>
<td>PHP <%=scheduleList.get(ctr).getSchedulePrice()%></td>
<td><%=scheduleList.get(ctr).getScheduleDate()%></td>
<td><%=scheduleList.get(ctr).getScheduleTime()%></td>
<td class="center"><%=scheduleList.get(ctr).getScheduleSeats()%></td>
</tr>
<%
}
%>
Here is the Schedule bean object for reference:
public class Schedule {
private int scheduleId;
private int movieId;
private String mallId;
private int scheduleCinema;
private BigDecimal schedulePrice;
private Date scheduleDate;
private Time scheduleTime;
private int scheduleSeats;
// getters and setters
}
I have managed to convert it to JSTL with my desired number of iterations (related thread).
<c:forEach var="ctr" begin="0" end="${scheduleList.size()-1}">
<tr>
<td class="center">
<input type="radio" name="selection" value="${ctr}" required title="Please select a schedule." />
</td>
<td>${schedule.mallId}</td>
<td>${schedule.scheduleCinema}</td>
<td>${schedule.schedulePrice}</td>
<td>${schedule.scheduleDate}</td>
<td>${schedule.scheduleTime}</td>
<td>${schedule.scheduleSeats}</td>
</tr>
</c:forEach>
Output:
Firefox Inspector:
<tr>
<td class="center">
<input type="radio" title="Please select a schedule." required="" value="0" name="selection"></input>
</td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
</tr>
<tr>
<td class="center">
<input type="radio" title="Please select a schedule." required="" value="1" name="selection"></input>
</td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
</tr>
Unfortunately, the schedule details are not displaying.
How do I display the data of each Schedule element from the List<Schedule> in JSTL?
(The ctr is important by the way, its value will be needed in the next action.)
Instead of
<c:forEach var="ctr" begin="0" end="${scheduleList.size()-1}">
Use:
<c:forEach var="schedule" items="${scheduleList}" varStatus="ctr">
<tr>
<td class="center">
<input type="radio" name="selection" value="${ctr.index}" required title="Please select a schedule." />
</td>
For ctr you could maintain separate counter variable and increment within your forEach.
You can use varStatus as well as var variable in c:forEach
varStatus="loop"
and use it as ${loop.index}
e.g. see the example

How to validate <aui:select> in a Liferay Form?

In View.jsp I created a form by using . there are some inputs and 3 select-box on it, actually I want to validate all of them. for inputs I used but I don't know how can I validate my
Actually I want to validate these three :
<aui:select id="birthday_day" name="birthday_day">
<aui:select id="birthday_month" name="birthday_month">
<aui:select id="birthday_year" name="birthday_year">
This is my view.jsp
<portlet:actionURL var="myUrl">
</portlet:actionURL>
<aui:form enctype="multipart/form-data" action="<%= myUrl %>" method="POST" name="fm">
<table border="0" bgcolor=#ccFDDEE>
<tr>
<td colspan="2" align="center"><b>Recruiment Form</b></td>
</tr>
<tr>
<td colspan="2" align="center"></td>
</tr>
<tr>
<td><b>First Name:<span>*</span></b></td>
<td>
<aui:input name="fname" type="text">
<aui:validator name="required"/>
</aui:input>
</td>
</tr>
<tr>
<td><b>Last Name:<span>*</span></b></td>
<td>
<aui:input name="lname" type="text">
<aui:validator name="required"/>
</aui:input>
</td>
</tr>
<tr>
<td><b>Father Name:<span>*</span></b></td>
<td>
<aui:input name="father_name" type="text">
<aui:validator name="required"/>
</aui:input>
</td>
</tr>
<tr>
<td><b>Birth Date:<span>*</span></b></td>
<td>
<aui:select id="birthday_day" name="<portlet:namespace/>birthday_day">
<aui:option value="0">روز</aui:option>
<%
for(int i=1;i<=31;i++) {
%>
<aui:option value="<%=i%>"><%=i%></aui:option>
<%
}
%>
</aui:select>
/
<aui:select id="birthday_month" name="birthday_month">
<aui:option value="0">ماه</aui:option>
<%
String[] monthBirthDay = {"فروردین","اردیبهشت","خرداد","تیر","مرداد","شهریور","مهر","آبان","آذر","دی","بهمن","اسفند"};
for(int j=0;j<monthBirthDay.length;j++) {
%>
<aui:option value="<%=j+1%>"><%=monthBirthDay[j]%></aui:option>
<%
}
%>
</aui:select>
/
<aui:select id="birthday_year" name="birthday_year">
<aui:option value="0">سال</aui:option>
<%
String[] yearBirthDay = {"1370","1369","1368","1367","1366","1365","1364"};
for(int j=0;j<yearBirthDay.length;j++) {
%>
<aui:option value="<%=yearBirthDay[j]%>"><%=yearBirthDay[j]%></aui:option>
<%
}
%>
</aui:select>
</td>
</tr>
</table>
</aui:form>
How can I solve this issue??????
Please guide me.
<aui:select showEmptyOption="true" name="select hospital name" required="true">
</aui:select>
THis may help you.
just assign an id to the aui:select tag
<aui:select inlineField="true" label="xxx" name="xxx" id="xxxId">
and then you can get the value with this (javascript + AUI)
var selectedVal = A.one('#<portlet:namespace/>xxxId').val();
with the selected value you can perform any kind of validation.

how do I dynamically print out a page that is loaded into text boxes?

I have a blur function that writes to the data base
$(".thoughts_box").blur(function(){
var box_id= $(this).attr("id").split("_")[$(this).attr("id").split("_").length-1];
writeListValue(1,box_id,$(this).val());
});
Here is my table
<td><input class="printArea_1 thoughts_box" name="activities_thoughts_1" id="activities_thoughts_1" type="text" style="width:345px;" placeholder="Type or click list button >" /></td>
<td><input type="button" id="thoughts_1" class="list_btn ext_thoughts" value="List >" name="_1" /></td>
</tr>
here is how I am printing it out
function print_list() {
$('input[id=printLoad_1]').val($('input[class=printArea_1]').val());
$('input[id=printLoad_2]').val($('input[class=printArea_2]').val());
$('input[id=printLoad_3]').val($('input[class=printArea_3]').val());
$('input[id=printLoad_4]').val($('input[class=printArea_4]').val());
$('input[id=printLoad_5]').val($('input[class=printArea_5]').val());
for( var i=1; i<=5; i++ ) {
if( document.getElementById('printLoad_'+i).value === '' ) {
document.getElementById('num_'+i).style.display = 'none';
}
}
$(".printing_list").printElement(
{
overrideElementCSS:[
'/css/print_fixer.css',
{ href:'/css/print_fixer.css',media:'print'}],
//leaveOpen:true,
//printMode:'popup',
});
}
here is the page that prints out I need to find a way to dynamically print out what the user puts into the text fields. can anybody please help me.
<div class="printing_list" id="printList" >
<img id="print_logo" src="/images/print_header_med.png">
<div align="left" id="printHead_text"></div>
<br />
<div align="left" class="listPrint_info" style="width:700px;"></div>
<br /><br />
<table width="100%" style="line-height:0px; margin-left:20px;" >
<tr id="num_1">
<td><input id="printLoad_1" type="text" style="width:700px;" />
</td>
</tr>
<tr id="num_2">
<td><input id="printLoad_2" type="text" style="width:700px;" />
</td>
</tr>
<tr id="num_3">
<td><input id="printLoad_3" type="text" style="width:700px;" />
</td>
</tr>
<tr id="num_4">
<td><input id="printLoad_4" type="text" style="width:700px;" />
</td>
</tr>
<tr id="num_5">
<td><input id="printLoad_5" type="text" style="width:700px;" />
</td>
</table>
</div>
elclanrs has the right idea. The code you wrote isn't maintainable. Try something like this:
// My print_list function assumes that I have some textboxes that are class
// textboxClass
function print_list() {
Array.filter( document.getElementsByClassName('textboxClass'), function(elem){
$(".printing_list").printElement( elem );
});
}
If you assign class textboxClass to all of your text boxes, this will loop through each of them, and you can do whatever you want.

Categories

Resources