2013-04-09 1 views
0

각 학생의 출석을 업데이트해야하는 모듈이 있습니다. 코드는 다음과 같습니다 :한 항목에 입력 한 값이 고유 값 대신 다른 항목에 저장됩니다.

public void updateDailyAttendance(ActionRequest areq, ActionResponse aRes) throws Exception{ 

int totalEmployees = EmployeeLocalServiceUtil.getEmployeesCount(); 
String attendanceValue = getAttendanceValue(areq); 
//long attPKey = CounterLocalServiceUtil.increment(Employee.class.getName()); 
for (int i = 0; i < totalEmployees; i++) { 
// use attendanceValue to update employee entry 
//String attendanceValue = getAttendanceValue(areq); 
// parameterValue is value of radio button parameter 


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 cannot be found!!"); 
} 

내 데이터베이스가 업데이트 될 때 위의 코드 만 참석 (첫번째 직원에 입력 결석/존재하는 값으로한다 사용하는 경우 다른 직원에 대해 다른 값 (Present/absent)을 표시하더라도 다른 직원의 경우

위의 코드를 수정하여 각 직원의 라디오 버튼 값이 저장되는 방법은 무엇입니까?

+0

이것은 http://stackoverflow.com/questions/15876090/passing-values-to-action-class-from-radio-button의 중복입니다. – yannicuLar

+0

나는 또한 당신이 대답을 수락하거나 적어도 upvote하는 것이 좋습니다. 사람들은 당신에게 매우 도움이되며 그들의 대답은 정확합니다. – yannicuLar

답변

1

루프 전에 한 번만 attendanceValue이 표시됩니다. getAttendanceValue은 하나의 문자열 만 반환하므로 두 학생간에 attendanceValue은 어떻게 다를 수 있습니까? 모든 attendanceValue의이 같은 이유

String attendanceValue = getAttendanceValue(areq); 

. for 루프에서 모든 직원의 가치를 업데이트하면됩니다.

+0

답장을 보내 주셔서 감사합니다. 나는 위의 문장을 for 루프 안에서 언급하려고 시도했지만 여전히 동일한 결과를 얻는다. –

+0

getAttendanceValue에 대한 코드도 변경해야한다. 관련 문자열을 반환하거나 배열을 반환하고'for' 루프 내에서 반복 할 수 있습니다. – BobTheBuilder

+0

위 코드를 어떻게 변경해야합니까? getAttendance Value의 코드를 의미합니다. 힌트를 줄 수 있습니까? –

1

출석을위한 값을 하나만 가져오고 있기 때문입니다. 이 줄에서 "String attendanceValue = getAttendanceValue (areq);" 첫 번째 직원에 대한 출석을 얻습니다. 그러나 getAttendanceValue 방법

while (parameters.hasMoreElements()) { 
    String parameterName = parameters.nextElement().toString(); 
if (parameterName.startsWith("updateattendance")) { 
    return areq.getParameter(parameterName); 
} 
} 

에이 코드는 항상 첫 번째 레코드에 만족하고 그 값이 반환됩니다 updateattendance과의 시작되었는지 확인합니다. "updateattendance"를 고유 번호로 사용하기 위해 사용한 접미어를 확인하여 다른 직원에 대한 레코드를 저장할 수 있습니다. 그런 다음 index를 updateattendance 이후의 숫자로 ArrayList에 저장할 수 있습니다. Strig를 반환하는 대신 ArrayList를 반환 할 수 있습니다.

+0

@Cya ... updateattendance 매개 변수와 employee 번호와 같은 페이지에있는 employee의 다른 매개 변수의 고유 한 이름을 말할 수 있습니까? –

+0

답장을 보내 주셔서 감사합니다. 다음은 jsp 페이지의 스 니펫 코드입니다. 여기서 사용자 입력을 허용합니다. \t \t

+0

이 질문에 대한 다른 대답을 추가했습니다. 코드 관점에서 더 좋습니다. –

1

대신 EmpId를 Key로 사용하고 값으로 updateattendanc을 사용하는 것이 더 좋습니다.

Map<String, String> mapEmpAttendance = new HashMap<String, String>(); 
while (parameters.hasMoreElements()) { 
    String parameterName = parameters.nextElement().toString(); 
    String value = ""; 
    if (parameterName.startsWith("updateattendance")) { 
      value = areq.getParameter(parameterName); 
      map.put(parameterName.substring(16, parameterName.length), value) 
    } 
} 
return mapEmpAttendance; 
관련 문제