Notice
Recent Posts
Recent Comments
Link
«   2025/05   »
1 2 3
4 5 6 7 8 9 10
11 12 13 14 15 16 17
18 19 20 21 22 23 24
25 26 27 28 29 30 31
Tags
more
Archives
Today
Total
관리 메뉴

게으른 완벽주의자의 개발자 도전기

학생성적정보시스템 만들기 2 (학생목록페이지, taglibs(choose, when, otherwise, if 사용하기) 본문

servlet jsp

학생성적정보시스템 만들기 2 (학생목록페이지, taglibs(choose, when, otherwise, if 사용하기)

머리방울 2022. 7. 30. 14:46

 

학생정보 작성 페이지에서 등록을 누르면 자료가 저장되어야 한다.

 

1. controller servlet

 

else if(command.equals("/regStudent.st")) {
String name = request.getParameter("name");
int age = Integer.parseInt(request.getParameter("age")); 
String [] tell = request.getParameterValues("tel");
String addr = request.getParameter("addr");

String tel = tell[0] + "-" + tell[1] + "-" + tell[2];

 

(위의 자료와 점수 정보들을 다 받아야 학생 한 명의 데이터가 완성된다.

이를 저장하기 위해서는 List를 이용하여

학생1, 학생2, 학생3 이런식으로 저장하는것이 수월하다.

public class StudentController1 extends HttpServlet {

private List<StudentDTO> studentList = new ArrayList<>();

맨 위 클래스에 리스트를 생성해준다.

리스트의 자료형은 StudentDTO 안에 변수들을 이용할 것이기 때문에

자료형으로 지정한다.)

 

1. StudentDTO에서 이름과 나이 전화번호 주소가 들어가는 생성자를 만든다.

StudentDTO student = new StudentDTO(name, age, tel, addr);

 

2. 위의 정보들을 리스트 항목에 넣어준다. 

studentList.add(student);

 

3. 목록페이지로 이동하는 속성을 넣어준다.
page="studentList.st";

 

 

(★request.setAttribute("studentList", studentList);
page = "student_List.jsp"; 

이렇게 적으면 새로고침 했을때 게시글이 계속 추가되는 단점이 있다.
그래서 목록페이지 servlet으로 이동하여 목록페이지.jsp로 이동하도록 설정한다.

servlet으로 갈 때는 넘길 데이터가 없기 때문에 

isRedirect=true; 넣어 response.sendRedirect(page); 실행되도록 해준다.)


isRedirect=true;
}

 

<목록페이지 이동>

if(command.equals("/studentList.st")) {

 

학생정보 작성 데이터를 jsp로 보내어 실질적으로 목록 페이지에 추가되도록 한다. 
request.setAttribute("studentList", studentList);

page = "student_List.jsp";
}

 

 

2. 목록페이지.jsp choose when oherwise if 태그 추가하기

<table>
<tr>
<td>No</td>
<td>학생명</td>
<td>국어점수</td>
<td>영어점수</td>
<td>수학점수</td>
<td>평균</td>
<td>점수입력</td>
<td>삭제</td>
</tr>

 

문제의 조건에서 데이터 입력하지 않았을 때 목록페이지에 데이터가 없다는 표시가 뜨도록 하며,

점수 입력하지 않은 경우 "미입력"이 뜨고, 점수 입력이 되면 해당 점수가 뜨도록 한다고 했다.

또한 점수등록, 삭제 버튼도 함께 뜨도록 세팅해야한다.

 

위와 같은 상황에서 java에서는 if else if문을 사용하겠지만, jsp에서는 if는 있으나

else if가 없어서 그 대신 사용하는 태그가 choose when otherwise이다.

 

<c:choose>

<c:when test = 내가 지정한 상황 조건 >

어떤걸 실행해라

<c:/when>

<c:otherwise>  지정상황 외 조건에서 실행할 내용<c:/otherwise> 

<c:/choose>


(when에는 학생데이터 없을 때, otherwise에는 학생 데이터 있을 때 조건을 넣을 것이다. )

<c:choose>

(★ list는 length를 size() 사용했다는 점 기억하자 

같다: eq  다르다: ne)


<c:when test="${studentList.size() eq 0}">
<tr>

(총 열이 8개니까 열병합을 해준다)
<td colspan="8">데이터가 없습니다.</td>
</tr>
</c:when>

(그 외)
<c:otherwise>

foreach문을 이용하여 자료를 넣어주고자 한다.

studentList라는 리스트의 자료를 student로 부를게
<c:forEach var="student" items="${studentList }">
<tr>
<td> </td>
<td>

이름을 누르면 상세페이지로 이동하도록 만들었다. 

상세페이지에서 우리가 primary key로 정한것이 이름이니까 이름값 가져가야한다.
<input type="button" value="${student.name }" onclick="location.href='detailStu.st?name=${student.name}';">
</td>

조건에서 성적 입력이 안되어 있으면 미입력이 표기되도록 하라고 했다.

<c:if test= 내가 정한 조건>

실행할 내용 작성

<c:/if>

 

<국어점수>
<td>
<c:if test="${student.korSco eq 0 }">
미입력
</c:if>
<c:if test="${student.korSco ne 0 }">
${student.korSco }
</c:if>
</td>

<영어점수>
<td>
<c:if test="${student.engSco eq 0}">
미입력
</c:if>
<c:if test="${student.engSco ne 0}">
${student.engSco }
</c:if>
</td>

 

<수학점수>
<td>
<c:if test="${student.mathSco eq 0 }">
미입력
</c:if>
<c:if test="${student.mathSco ne 0 }">
${student.mathSco }
</c:if>
</td>

 

<평균>

<td>
<c:if test="${student.avg eq 0}">
 미입력
</c:if>
<c:if test="${student.avg ne 0 }">
${student.avg }
</c:if>
</td>

 

<버튼>
<td><input type="button" value="점수등록" onclick="location.href='regScore.st?name=${student.name}';"></td>
<td><input type="button" value="삭제" onclick="location href='deleteStu.st?name=${student.name}';"></td>

 

</tr>
</c:forEach>
</c:otherwise>
</c:choose>

</table>

<div align="center" >
<input type="button" value="학생등록" onclick="location.href='regStudentForm.st';">
</div>