Foren

Problem while passing values to action class from radio button

Seeya S Kudtarker, geändert vor 11 Jahren.

Problem while passing values to action class from radio button

Regular Member Beiträge: 187 Beitrittsdatum: 16.01.13 Neueste Beiträge
I am having a jsp form where I have to mark attendance for each employee and store the results in the database. My jsp snippet for marking attendance is as follows:

<aui:form name="updateDailyAttendance" action="<%=updateDailyAttendanceURL.toString() %>" method="post" >

<portlet:renderURL var="viewEmployeeDataURL"/>
<liferay-ui:search-container delta="20" emptyResultsMessage="No Results Found">
<liferay-ui:search-container-results total="<%= EmployeeAttendanceDetails .size() %>"
results="<%= ListUtil.subList(EmployeeAttendanceDetails , searchContainer.getStart(), searchContainer.getEnd()) %>" />
<liferay-ui:search-container-row modelVar="search"
className="com.test.mis.portal.model.Employee">

<liferay-ui:search-container-column-text name='Employee Name' value='<%=String.valueOf(search.getEmpFname()) + " " + String.valueOf(search.getEmpLname())%>' href="" />

<liferay-ui:search-container-column-text name='Employee Id' value='<%=String.valueOf(search.getEmpId())%>' href="" />

<liferay-ui:search-container-column-text name = "Attendance Status" >

<label>Present</label><input type = "radio" name ='updateattendance + <%=String.valueOf(search.getEmpId())%>' value = "present" />
<label>Absent</label><input type = "radio" name= 'updateattendance + <%=String.valueOf(search.getEmpId())%>' value = "absent"/>
</liferay-ui:search-container-column-text>
</liferay-ui:search-container-row>
<liferay-ui:search-iterator searchContainer="<%=searchContainer %>" paginate="<%=true %>" />
</liferay-ui:search-container>
<input type = "submit" value = "Update"/>
</aui:form>


And I use the following functions to mark the attendance:
public void updateDailyAttendance(ActionRequest areq, ActionResponse aRes) throws Exception{

int totalEmployees = EmployeeLocalServiceUtil.getEmployeesCount();
String attendanceValue = getAttendanceValue(areq);
for (int i = 0; i < totalEmployees; i++) {
long attPKey = CounterLocalServiceUtil.increment(Employee.class.getName());

Attendance newAttendanceInstance = new AttendanceImpl();
newAttendanceInstance.setAttId(attPKey);
newAttendanceInstance.setAttStatus(attendanceValue);
AttendanceLocalServiceUtil.addAttendance(newAttendanceInstance);
}

}

private String getAttendanceValue(ActionRequest areq) {
Enumeration parameters = areq.getParameterNames();

while (parameters.hasMoreElements()) {
String parameterName = parameters.nextElement().toString();
if (parameterName.startsWith("updateattendance")) {
return areq.getParameter(parameterName);
}
}
throw new IllegalStateException("Parameter updateattendance is not found");
}



The problem that I am facing is that whatever attendance I mark for the first employee (Present/Absent) the same is stored for the other employees. The error I think is in the above for loop which I have italicized. How should I rectify this code such that for each employee the correct attendance status is stored?
thumbnail
Masroor Khan, geändert vor 11 Jahren.

RE: Problem while passing values to action class from radio button

Regular Member Beiträge: 124 Beitrittsdatum: 09.09.08 Neueste Beiträge
Hi,

String attendanceValue = getAttendanceValue(areq);

you should get attendanceValue inside for loop by using i value.


Regards,

Masroor Khan
Seeya S Kudtarker, geändert vor 11 Jahren.

RE: Problem while passing values to action class from radio button

Regular Member Beiträge: 187 Beitrittsdatum: 16.01.13 Neueste Beiträge
Mansoor,
Thanks for the reply.
I tried the following by putting String attendanceValue = getAttendanceValue(areq); in the loop but it still gives me the same results. i.e. the value entered for the first employee (P/A) becomes the value for the other employees too.

int totalEmployees = EmployeeLocalServiceUtil.getEmployeesCount();

for (int i = 0; i < totalEmployees; i++) {

String attendanceValue = getAttendanceValue(areq);
long attPKey = CounterLocalServiceUtil.increment(Employee.class.getName());

Attendance newAttendanceInstance = new AttendanceImpl();
newAttendanceInstance.setAttId(attPKey);
newAttendanceInstance.setAttStatus(attendanceValue);
AttendanceLocalServiceUtil.addAttendance(newAttendanceInstance);
}
}

Seeya