ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • Spring:resolver
    JAVA 2021. 7. 26. 22:54

    1. Spring resolver

    웹 네트워크 구조: UI 를 매개로 소통중

    Spring resolver 참고(파일용 resolver 따로 만들었었고, 사용자가 원하는 기능을 가진 resolver를 만들 수 있다.

    Spring resolver 는 이름에 '[n]' 가 들어있는 걸 보고 배열로 인식해 넣어준다

    ( 받는 객체 쪽에서는 List 로 준비해둬야 한다. )


    바이트(B)byte -> 킬로바이트(KB) -> 메가바이트(MB) -> 기가바이트(GB)
    바이트에서 1024 나누면 킬로바이트, 킬로바이트에서 1024 나누면 메가바이트... 네이버에 byte 만 쳐도 계산기 나옴.

    int 형은 1기가바이트밖에 안된다.

    https://goldfishhead.tistory.com/2 


    2. Component

    현재 우리 시스템

    MVC + 3 Tier Layer + Component

    Component : 공통모듈 , DAO 같은 애들. ( Control 은 요청에 따라 사용할 때가 있고 없을 수 있으나, DAO 아무나 원할 때 사용가능)

     


    3. java DB기반 class 관계

    단방향, 양방향 관계

    DB 에서는 Board 의 PK 를 Attach 가 FK 로 가져온 관계이다.

    java 에서는 다시 한번 위의 단방향, 양방향 관계를 잘 따져야 한다.

    참조하는 쪽으로 지목하여 변수가 있다.(화살표방향으로 생각)

    Board 객체로 바로 attach 객체를 연결해 보고 싶다면 Board 가 Attach 를 참조해야하고
    Attach 객체에서도 바로 연결해 보고 싶다면 Board 를 참조해도 된다. (DB와 다르다! DB에서는 서로참조하지 않는다.)

    현재 java 코드에서는 서로 참조, DB 에서는 Attach가 Board를 참조하고 있다.
    java의 서로참조는 Board 가 form 에서 파일을 받아 가지고있는(참조하는) 멤버변수 Attach 에게 값을 주고 꺼낼 수 있는 방식이다.
    (즉, Board 객체 하나로 Attach 까지 다 등록하고 DB에서 가져올때도 Board 를 이용한다. (Attach 는 단순히 값 저장공간 같다.))
    UML 클래스 다이어그램
    관계
    Directed Association : 단방향
    Associtaion : 양방향

    1 : 하나만 있으면 됨
    0..* : 없거나 여러개(List)

    >> Attach.java

    public class Attach {
    	private int no;
    	private String name;
    	private long size;
    	private int board_no;
    }
    // get,set 생략

    >> Board.java

    public class Board {
    
    	// 일반속성 = 원시속성
    	private int no;
    	private String title;
    	private String contents;
    	private Date wdate;
    	private long views;
    	private Member writer;
    	private List<MultipartFile> attachFiles;
    	private List<Attach> attachs;
    // 중략..
    	public List<MultipartFile> getAttachFiles() {
    		return attachFiles;
    	}
    
    	public void setAttachFiles(List<MultipartFile> attachFiles) {
    		this.attachFiles = attachFiles;
    	}
    
    	public List<Attach> getAttachs() {
    		return attachs;
    	}
    
    	public void setAttachs(List<Attach> attachs) {
    		this.attachs = attachs;
    	}
    }
    private List<MultipartFile> attachFiles : 일단 Spring 의 resolver로 부터 매핑을 위한 객체가 있어야 하므로 File이라면 꼭 넣어야 한다.
    업로드 시에 이 멤버변수에 값이 들어간다.
    업로드 매칭 - Spring resolver 가 매칭
    private List<Attach> attachs : 윗 값을 여기에 저장하고 이 파일을 출력(혹은 남들이 업로드하도록 출력)시에는 여기서 값을 꺼낸다.
    DB 매칭

    4. MultipartFile 을 통해 파일과 정보를 set,get

     

    MultipartFile
    getBytes() : 파일을 byte[] 로 가져올 수 있다. (후에 outputStream 으로 출력가능)
    getOriginalFilename() : 파일의 이름을 가져올 수 있다.
    getSize() : 파일의 사이즈를 가져올 수 있다.

    >> Board.java 의 File 관련 get,set 메서드 고치기

    	public List<Attach> getAttachs() {//내부의 attachFiles 의 값을 가져와 set하고 get까지 하는 역할
    		if(attachFiles!=null && this.attachs==null) {
    			this.attachs = new ArrayList<Attach>();
    			for(MultipartFile multipartFile: attachFiles) {//배열로 된걸 하나씩 꺼내기
    				Attach attach = new Attach();
    				//multipartFile 을 통해 파일의 이름을 가져올 수 있다.
    				attach.setName(multipartFile.getOriginalFilename());
    				attach.setSize(multipartFile.getSize());
    				attachs.add(attach);
    			}
    		}
    		return attachs;
    	}
    
    	public void setAttachs(List<Attach> attachs) {//DB에 있는 값을 받는 역할
    		this.attachs = attachs;
    	}

    >> 게시물등록창.jsp

    <body>
    	
    	<form action="board" method="post" enctype="multipart/form-data">
    		제목
    		<input type="text" name="title" />
    		<br> 내용
    		<textarea name="title"></textarea>
    		<br> 첨부
    		<br>
    		<ul id="attachlist"></ul>
    		<input type="submit" value="등록하기"/>
    	</form>
    		// form 밑에 해야 form 이 submit 되지 않는다.
    		<button onclick="첨부요소추가하다()">첨부추가</button>
    </body>
    
    <script type="text/javascript">
    	function 첨부요소추가하다(e) {
    // 중략..
    		
    		if(첨부리스트.childNodes != null){
    			// 이 부분 따로 변수로 만들지 않고 name에 바로 넣으면 오류난다.
    			var i = 첨부리스트.childNodes.length - 1;
    			// li 가 아니라 input.file 에다가 name 넣어야 함(근데 왜 li는 name속성이 undefinded뜨고 안되지..?)
    			var fileInput = 첨부리스트.childNodes[i].childNodes[0];
    			fileInput.name = "attachFiles[" + i +  "]";
    		}
    	}
    </script>

    'JAVA' 카테고리의 다른 글

    Spring: 댓글(iframe.ver)  (0) 2021.07.26
    Spring:댓글  (0) 2021.07.26
    board,member(spring.ver1)  (0) 2021.07.26
    Spring:Mapping,Repository,Autowired(DI),multipartFile  (0) 2021.07.26
    Spring, Tomcat Servers의xml파일정리  (0) 2021.07.26

    댓글

Designed by Tistory.