[thymeleaf 추가]
스프링스타터로 thymeleaf 추가시
build.gradle에 기존 세팅한게 다 날아감
Finish누르지않고 추가되는 줄을 복사해서 손수 붙여넣어야함
Finish 누른경우 커맨드z로 되돌리고 붙여넣기
implementation 'org.springframework.boot:spring-boot-starter-thymeleaf'
프로젝트에 에러나면 다이나믹모듈 5.0으로 변경
타임리프 세팅도 해야함 thymeleaf.properties 생성해서 openwith Spring properties, enc검색해서 스프링프로퍼티스에 *.properties 추가
thymeleaf.properties
spring.thymeleaf.cache=false
spring.thymeleaf.check-template-location=true
spring.thymeleaf.enabled=true
#thymeleaf View
spring.thymeleaf.prefix=classpath:/templates
spring.thymeleaf.suffix=.jsp
spring.thymeleaf.mode=html
#controller return JSP,JSTL,thymeleaf 구별하기 위함
spring.thymeleaf.view-names=/*
application.properties에
spring.config.import=thymeleaf.properties
추가해서 임포트하기
[DB 추가]
스프링스타터로 필요한거 다 추가 (jdbc,mybatis,db커넥터) 해서 복사해서
build.gradle 의 dependencies에 추가
//DB
implementation 'org.springframework.boot:spring-boot-starter-data-jdbc'
implementation 'org.springframework.boot:spring-boot-starter-jdbc'
runtimeOnly 'com.mysql:mysql-connector-j'
implementation 'org.mybatis.spring.boot:mybatis-spring-boot-starter:3.0.4'
//test : 디버깅용 임시로 디비연결
testImplementation 'org.mybatis.spring.boot:mybatis-spring-boot-starter-test:3.0.4'
database.properties
#데이터베이스 여러개 연결시 이거 봉인풀면 망함 하나만쓸때 이렇게 쓰기
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
로컬용(개발시 사용)
spring.datasource.url=jdbc:mysql://서버ip:13306/webapi
#배포용
#spring.datasource.url=jdbc:mysql://localhost:3306/webapi
spring.datasource.username=아이디
spring.datasource.password=비번
#mybatis
mybatis.type-aliases-package=kr.co.koo.*
mybatis.mapper-locations=classpath:/mapper/*.xml
메인properties파일에
spring.config.import=thymeleaf.properties,database.properties
콤마로 이어주기
[코드]
[webfile.html]
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>파일업로드 - Server</title>
</head>
<body>
<form id="frm" method="POST" action="./webfileok.do" enctype="multipart/form-data">
단일 파일 업로드 : <input type="file" name="file1"><br>
<input type="button" value="전송" onclick="fileok()">
</form>
</body>
<script>
function fileok(){
frm.submit();
}
</script>
</html>
[controller.java]
@PostMapping("/webfileok.do")
public String webfileok(@RequestParam("file1")MultipartFile file1,
ServletRequest req
) throws Exception {
long filesize = file1.getSize(); //파일용량
// this.log.info(file1.getOriginalFilename());
String url = req.getServletContext().getRealPath("/upload/");
File f = new File(url);
this.log.info(url);
String copys = url + file1.getOriginalFilename();
try {
FileCopyUtils.copy(file1.getBytes(), new File(copys));
}catch (Exception e) {
this.log.info(e.toString());
}
//spring-boot에서는 RealPath가 src 디렉토리에 생성을 하여 경로룰 생성
//spring에서는 RealPath가 temp 디렉토리에 생성을 하여 경로를 생성
return null;
}
배포시 업로드한 파일은 src/upload에 저장됨