通过单选按钮将值传递给动作类 [英] Passing values to action class from radio button
问题描述
我有一个jsp表单,我必须在其中标记每个员工的出勤并将结果存储在数据库中.我用于标记出勤的jsp片段如下:
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:
<portlet:defineObjects />
<%
List<Employee> EmployeeAttendanceDetails = MISPortalActionUtil.getEmployeeData();
%>
<portlet:renderURL var="viewMarkAttendanceURL"/>
<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>mark attendance</title>
</head>
<body>
Mark Attendance for Today:
<%= new java.util.Date() %>
<portlet:actionURL name="updateDailyAttendance" var="updateDailyAttendanceURL" />
**<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>**
我使用以下功能来标记出勤: 公共无效updateDailyAttendance(ActionRequest areq,ActionResponse aRes)引发异常{
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");
}
我面临的问题是,无论我为第一位员工标记的出勤(在职/缺席)都存储在其他员工的出勤中.我认为的错误是在上面的for循环中,我已将其斜体化了.我应该如何纠正此代码,以便为每个员工存储正确的出勤状态?
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?
推荐答案
考虑到您的jsp是正确的,代码
Considering your jsp is correct, the code
<input type = "radio" name ='updateattendance + <%=String.valueOf(search.getEmpId())%>' value = "present" />
将创建一个属性数组,命名为updateattendance101,updateattendance102,updateattendance201,updateattendance301等
will create an array of properties named like updateattendance101, updateattendance102, updateattendance201, updateattendance301 etc
代码
if (parameterName.startsWith("updateattendance")) { return areq.getParameter(parameterName);
获得第一个匹配的属性,因此您总是获得相同的值.因此,您需要做的是使用与馈送搜索容器相同的数组(数组'EmployeeAttendanceDetails'),遍历所有对象,并使用'getEmpId'id来完全匹配该属性.
gets the first matching property, therefore you get the same value always. So what you need to do, is use the same array you used to feed the search-container (array 'EmployeeAttendanceDetails' ), iterate through all it's objects and use the 'getEmpId' id to exact match the property.
其次,我发现一些关于ServiceBuilder使用的错误做法.
Secondly, I see some bad practices on the usage of the ServiceBuilder.
- 您确定要创建一个新的Employee,而不是更新现有的Employee吗?
- 即使您要创建/更新员工条目,也应在AttendanceLocalServiceImpl中创建包装函数,而不是在客户端代码中手动编辑所有属性/增加持久性计数器等
您可以用此替换您发布的Java代码,希望您能理解
Edit : you can replace your posted java code with this, I hope you can understand to do
public void updateDailyAttendance(ActionRequest areq, ActionResponse aRes) throws Exception{
List<Employee> employeeAttendanceDetails = MISPortalActionUtil.getEmployeeData();
for (Employee emp: employeeAttendanceDetails) {
String name = "updateattendance" + Long.toString(emp.getEmpId());
String value = getAttendanceValue(areq, name);
// You don't really need to call call getAttendanceValue, except if you're going to handle the IllegalStateException.
//If this is the case, you can just call :
//String value = areq.getParameter(name);
// Do your stuff with the employee object
}
}
private String getAttendanceValue(ActionRequest areq, String paramName) {
Enumeration parameters = areq.getParameterNames();
while (parameters.hasMoreElements()) {
String parameterName = parameters.nextElement().toString();
if (parameterName.equals(paramName)) {
return areq.getParameter(parameterName);
}
}
throw new IllegalStateException("Parameter updateattendance is not found");
}
替换
name ='updateattendance + <%=String.valueOf(search.getEmpId())%>'
使用
name ='updateattendance<%=String.valueOf(search.getEmpId())%>'
这篇关于通过单选按钮将值传递给动作类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!