从单选按钮将值传递给操作类

时间:2013-04-08 09:49:55

标签: jsp radio-button liferay-6

我有一个jsp表单,我必须为每个员工标记出勤率并将结果存储在数据库中。我用于标记出勤率的jsp片段如下:

<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>**

我使用以下功能来标记出勤率: public void updateDailyAttendance(ActionRequest areq,ActionResponse aRes)抛出异常{

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循环中,我用斜体。我应该如何纠正这些代码,以便为每位员工存储正确的出勤状态?

1 个答案:

答案 0 :(得分:1)

考虑到你的jsp是正确的,代码

<input type = "radio" name ='updateattendance +     <%=String.valueOf(search.getEmpId())%>' value = "present" />

将创建一个名为updateattendance101,updateattendance102,updateattendance201,updateattendance301等属性的数组

代码

if (parameterName.startsWith("updateattendance")) { return areq.getParameter(parameterName);

获取第一个匹配属性,因此您始终获得相同的值。所以你需要做的是使用你用来提供搜索容器的相同数组(数组'EmployeeAttendanceDetails'),迭代它的所有对象并使用'getEmpId'id与属性完全匹配。

其次,我看到了一些关于ServiceBuilder使用的不良做法。

  1. 您确定要创建新的员工,而不是更新现有员工吗?
  2. 即使你想创建/更新一个Employee Entry,你也应该在AttendanceLocalServiceImpl中创建一个包装函数,而不是手动编辑客户代码中的所有属性/增加持久性计数器等
  3. 编辑:你可以用这个替换你发布的java代码,希望你能理解做

    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");
    }
    

    编辑2:替换

    name ='updateattendance +     <%=String.valueOf(search.getEmpId())%>'
    

    name ='updateattendance<%=String.valueOf(search.getEmpId())%>'