본문 바로가기

spring/소스코드

c라이브러리를 이용한 학사 페이징 작업

<com.example.domain>-ProfessorsVO.java

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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
 
 
public class ProfessorsVO {
    private String pcode;
    private String pname;
    private String dept;
    private Date Hiredate;
    private int salary;
    private String title;
    public String getPcode() {
        return pcode;
    }
    public void setPcode(String pcode) {
        this.pcode = pcode;
    }
    public String getPname() {
        return pname;
    }
    public void setPname(String pname) {
        this.pname = pname;
    }
    public String getDept() {
        return dept;
    }
    public void setDept(String dept) {
        this.dept = dept;
    }
    public Date getHiredate() {
        return Hiredate;
    }
    public void setHiredate(Date hiredate) {
        Hiredate = hiredate;
    }
    public int getSalary() {
        return salary;
    }
    public void setSalary(int salary) {
        this.salary = salary;
    }
    public String getTitle() {
        return title;
    }
    public void setTitle(String title) {
        this.title = title;
    }
    @Override
    public String toString() {
        return "ProfessorsVO [pcode=" + pcode + ", pname=" + pname + ", dept=" + dept + ", Hiredate=" + Hiredate
                + ", salary=" + salary + ", title=" + title + "]";
    }
    
}
 
 
http://colorscripter.com/info#e" target="_blank" style="text-decoration:none;color:white">cs

<com.example.domain>-Criteria.java

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
32
33
34
35
36
37
38
39
40
public class Criteria{
      private int page;
      private int perPageNum;
      
      public Criteria(){
        this.page = 1;
        this.perPageNum = 3;
      }
      
      public void setPage(int page){
        if (page <= 0){
          this.page = 1;
          return;
        }
        this.page = page;
      }
      public void setPerPageNum(int perPageNum){
        if(perPageNum <= 0 || perPageNum > 100){
          this.perPageNum = 10;
          return;
        }
        this.perPageNum = perPageNum;
      }
      public int getPage(){
        return page;
      }
      
      public int getPageStart(){
        return (this.page - 1* perPageNum;
      }
     
      public int getPerPageNum(){
        return this.perPageNum;
      }
      @Override
      public String toString(){
        return "Criteria [page=" + page + ", " + "perPageNum=" + perPageNum + "]";
      }
    }
 
 

<com.example.domain>-PageMaker.java

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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
public class PageMaker{
      private int totalCount;
      private int startPage;
      private int endPage;
      private boolean prev;
      private boolean next;
      private int displayPageNum = 10;
      private Criteria cri;
      
      public void setCri(Criteria cri){
        this.cri = cri;
      }
      public void setTotalCount(int totalCount){
        this.totalCount = totalCount;
        calcData();
      }
      private void calcData(){
        endPage = (int) (Math.ceil(cri.getPage() / (double) displayPageNum) * displayPageNum);
        startPage = (endPage - displayPageNum) + 1;
        int tempEndPage = (int) (Math.ceil(totalCount / (double) cri.getPerPageNum()));
        if(endPage > tempEndPage){
          endPage = tempEndPage;
        }
        prev = startPage == 1 ? false : true;
        next = endPage * cri.getPerPageNum() >= totalCount ? false : true;
      }
    public int getStartPage() {
        return startPage;
    }
    public void setStartPage(int startPage) {
        this.startPage = startPage;
    }
    public int getEndPage() {
        return endPage;
    }
    public void setEndPage(int endPage) {
        this.endPage = endPage;
    }
    public boolean isPrev() {
        return prev;
    }
    public void setPrev(boolean prev) {
        this.prev = prev;
    }
    public boolean isNext() {
        return next;
    }
    public void setNext(boolean next) {
        this.next = next;
    }
    public int getDisplayPageNum() {
        return displayPageNum;
    }
    public void setDisplayPageNum(int displayPageNum) {
        this.displayPageNum = displayPageNum;
    }
    public int getTotalCount() {
        return totalCount;
    }
    public Criteria getCri() {
        return cri;
    }
}
 
 
 

<com.example.persistence>-ProfessorsDAO.Interface

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
package com.example.persistence;
 
 
 
public interface ProfessorsDAO {
   public List<ProfessorsVO> list(Criteria cri)throws Exception;
   public int total()throws Exception;
   public void insert(ProfessorsVO vo) throws Exception;
   public ProfessorsVO read(String pcode) throws Exception;
   public void update(ProfessorsVO vo) throws Exception;
   public void delete(String pcode) throws Exception;
}
 
 
 

<com.example.persistence>-ProfessorsDAO.Impljava

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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
package com.example.persistence;
 
 
 
import org.springframework.stereotype.Repository;
 
 
@Repository
public class ProfessorsDAOImpl implements ProfessorsDAO{
    @Inject
    SqlSession session;
    private static final String namespace="ProfessorsMapper";
    
    @Override
    public List<ProfessorsVO> list(Criteria cri) throws Exception {
        return session.selectList(namespace+".list", cri);
    }
 
    @Override
    public int total() throws Exception {
        return session.selectOne(namespace+".total");
    }
 
    @Override
    public void insert(ProfessorsVO vo) throws Exception {
        session.insert(namespace + ".insert",vo);
    }
 
    @Override
    public ProfessorsVO read(String pcode) throws Exception {
        return session.selectOne(namespace+".read",pcode);
    }
 
    @Override
    public void update(ProfessorsVO vo) throws Exception {
        // TODO Auto-generated method stub
        session.update(namespace +".update",vo);
    }
 
    @Override
    public void delete(String pcode) throws Exception {
        // TODO Auto-generated method stub
        session.delete(namespace +".delete",pcode);
    }
 
 
 
}
 
 

<com.example.web>-ProfessorsController.java

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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
 
 
import org.springframework.stereotype.Controller;
 
import com.example.persistence.ProfessorsDAO;
 
@Controller
@RequestMapping("/professors/*")
public class ProfessorsController {
       @Inject
       ProfessorsDAO dao;
       
       @RequestMapping("list")
       public String list(Model model, Criteria cri) throws Exception{
           System.out.println(cri.toString());
           model.addAttribute("list"dao.list(cri));
           
           PageMaker pm=new PageMaker();
           pm.setCri(cri);
           pm.setTotalCount(dao.total());
           model.addAttribute("pm",pm);
           
           return "/professors/list";
       }
       @RequestMapping("insert")
       public String insert(){
           return "/professors/insert";
       }
       @RequestMapping(value="insert", method=RequestMethod.POST)
       public String insert(ProfessorsVO vo) throws Exception{
           dao.insert(vo);
           return "redirect:/professors/list";
       }
       @RequestMapping("read")
       public String read(String pcode, Model model) throws Exception{
           model.addAttribute("vo"dao.read(pcode));
           return "/professors/read";
       }
       @RequestMapping(value="update", method=RequestMethod.POST)
       public String update(ProfessorsVO vo) throws Exception{
           dao.update(vo);
           return "redirect:/professors/list";
       }
       @RequestMapping(value="delete", method=RequestMethod.POST)
       public String delete(String pcode) throws Exception{
           dao.delete(pcode);
           return "redirect:/professors/list";
       }
}
 
 
 

<src/main/resource>-DBTest.java

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
32
 
 
 
import com.example.persistence.ProfessorsDAO;
import com.example.persistence.StudentsDAO;
 
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = { "file:src/main/webapp/WEB-INF/spring/**/*.xml" })
 
public class DBTest {
   @Inject 
   ProfessorsDAO pdao;
  
   StudentsDAO sdao;
   @Test
   public void plist() throws Exception{
       pdao.list();
   }
   @Test
   public void sinsert() throws Exception{
       StudentsVO vo=new StudentsVO();
       sdao.insert(vo);
   }
}
 
 
 

<src/resource/test>-<mappers>-ProfessorsMapper.xml

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
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper
<mapper namespace="ProfessorsMapper">
    <select id="list" resultType="com.example.domain.ProfessorsVO">
       select * from professors
       limit #{pageStart}, #{perPageNum}
    </select>
    <select id="total" resultType="int">
        select count(*) from professors
    </select>
    <insert id="insert">
         insert into professors(pcode, pname, dept, title, salary, hiredate)
         values(#{pcode},#{pname},#{dept},#{title},#{salary},now())
    </insert>
    <select id="read" resultType="com.example.domain.ProfessorsVO">
        select * from professors where pcode=#{pcode}
    </select>
    
    <update id="update">
        update professors 
        set pname=#{pname},dept=#{dept},title=#{title},salary=#{salary}
        where pcode=#{pcode}
    </update>
    <delete id="delete">
        delete from professors 
        where pcode=#{pcode}
    </delete>
</mapper>
 
 

<src>-<main>-<webapp>-<WEB-INF>-<spring>-<view>-list.jsp

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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="f" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
   <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
   <title>교수목록</title>
   <style>
       a {text-decoration:none; color:skyblue;}
       .active{color:orange;}
   </style>
</head>
<body>
    <h1>[교수목록]</h1>
    <button onClick="location.href='insert'">교수등록</button>
      데이터:<b>${pm.totalCount}<b>
    <table border=1 width=700>
       <tr>
          <th width=100>교수번호</th>
          <th width=100>교수이름</th>
          <th width=100>교수학과</th>
          <th width=100>교수직급</th>
          <th width=100>교수급여</th>
          <th width=100>임용일자</th>
          <th width=100>수정</th>
       </tr>
       <c:forEach items="${list}" var="vo">
        <tr>
            <td>${vo.pcode}</td>
            <td>${vo.pname}</td>
            <td>${vo.dept}</td>
            <td>${vo.title}</td>
            <td><f:formatNumber value="${vo.salary}" pattern="#,###.00"/></td>
            <td><f:formatDate value="${vo.hiredate}" pattern="yyyy-MM-dd"/></td>
            <td><button onClick="location.href='read?pcode=${vo.pcode}'">수정</button></td>
        </tr>
       </c:forEach> 
    </table>
    
    <div id="pagination">
          <c:if test="${pm.prev}">
            <a href="list?page=${pm.startPage-1}"></a>
          </c:if>
          <c:forEach begin="${pm.startPage}" end="${pm.endPage}" var="i">
          <c:if test="${i==pm.cri.page}">
             [<a class="active" href="list?page=${i}">${i}</a>]
          </c:if>
          <c:if test="${i!=pm.cri.page}">
             [<a href="list?page=${i}">${i}</a>]
          </c:if>
          </c:forEach>
          <c:if test="${pm.next}">
           <a href="list?page=${pm.endPage+1}"></a>
          </c:if>
    </div>
</body>
</html>
 
 

<src>-<main>-<webapp>-<WEB-INF>-<spring>-<view>-insert.jsp

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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>교수등록</title>
</head>
<body>
     <h1>[교수등록]</h1>
     <form action="insert" method="post">
        <table border=1 width=700>
           <tr>
             <td>교수번호</td>
             <td><input type="text" name="pcode"></td>
            </tr>
            <tr> 
              <td>교수이름</td>
              <td><input type="text" name="pname"></td>
            </tr>
            <tr>
             <td>교수학과</td>
               <td>
                 <select name="dept">
                    <option>전산</option>
                    <option>건축</option>
                    <option>전자</option>
                 </select>
               </td>
             </tr>
             <tr>
             <td>교수직급</td>
             <td>
                 <input type="radio" name="title" value="정교수">정교수
                 <input type="radio" name="title" value="부교수">부교수
                 <input type="radio" name="title" value="조교수">조교수
              </td>
              </tr>
              <tr>
             <td>교수급여</td>
             <td><input type="text" name="salary"></td>
             </tr>
        </table>
        <input type="submit" value="저장" >
        <input type="reset" value="취소">
        <input type="button" value="목록" onClick="location.href='list'">
     </form>
</body>
</html>
http://colorscripter.com/info#e" target="_blank" style="color:#e5e5e5text-decoration:none">Colored by Color Scripter
http://colorscripter.com/info#e" target="_blank" style="text-decoration:none;color:white">cs

<src>-<main>-<webapp>-<WEB-INF>-<spring>-<view>-read.jsp

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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>교수현황</title>
</head>
<body>
     <h1>[교수현황]</h1>
     <form name="frm" method="post">
        <table border=1 width=700>
           <tr>
             <td>교수번호</td>
             <td><input type="text" name="pcode" value="${vo.pcode}"></td>
            </tr>
            <tr> 
              <td>교수이름</td>
              <td><input type="text" name="pname" value="${vo.pname}"></td>
            </tr>
            <tr>
             <td>교수학과</td>
               <td>
                 <select name="dept">
                    <option value="전산"<c:out value="${vo.dept=='전산'?'selected':''}"/>>전산</option>
                    <option value='건축'<c:out value="${vo.dept=='건축'?'selected':''}"/>>건축</option>
                    <option value='전자'<c:out value="${vo.dept=='전자'?'selected':''}"/>>전자</option>
                 </select>
               </td>
             </tr>
             <tr>
             <td>교수직급</td>
             <td>
                 <input type="radio" name="title" value="정교수" <c:out value="${vo.title=='정교수'?'checked':''}"/>>정교수
                 <input type="radio" name="title" value="부교수" <c:out value="${vo.title=='부교수'?'checked':''}"/>>부교수
                 <input type="radio" name="title" value="조교수" <c:out value="${vo.title=='조교수'?'checked':''}"/>>조교수
              </td>
              </tr>
              <tr>
             <td>교수급여</td>
             <td><input type="text" name="salary" value="${vo.salary}"></td>
             </tr>
        </table>
        <input type="button" value="저장" onClick="funupdate()">
        <input type="button" value="삭제" onClick="fundelete()">
        <input type="reset" value="취소">
        <input type="button" value="목록" onClick="location.href='list'">
     </form>
</body>
<script>
    function funupdate(){
        if(!confirm("수정하시겠습니까?")) return;
        frm.action="update";
        frm.submit();
    }
    
    function fundelete(){
        if(!confirm("삭제하시겠습니까?")) return;
        frm.action="delete";
        frm.submit();
    }
</script>
</html>