2025-05-02
build.gradle
)plugins {
id 'java'
id 'war'
id 'org.springframework.boot' version '3.4.5'
id 'io.spring.dependency-management' version '1.1.7'
}
group = 'com.example'
version = '0.0.1-SNAPSHOT'
java {
toolchain {
languageVersion = JavaLanguageVersion.of(21)
}
}
repositories {
mavenCentral()
}
dependencies {
implementation 'org.springframework.boot:spring-boot-starter-web'
compileOnly 'org.projectlombok:lombok'
annotationProcessor 'org.projectlombok:lombok'
testImplementation 'org.springframework.boot:spring-boot-starter-test'
testRuntimeOnly 'org.junit.platform:junit-platform-launcher'
// JSP
implementation 'org.apache.tomcat.embed:tomcat-embed-jasper'
// JSTL
implementation 'jakarta.servlet:jakarta.servlet-api'
implementation 'jakarta.servlet.jsp.jstl:jakarta.servlet.jsp.jstl-api'
implementation 'org.glassfish.web:jakarta.servlet.jsp.jstl'
// Validation
implementation 'org.hibernate.validator:hibernate-validator:8.0.2.Final'
implementation 'jakarta.validation:jakarta.validation-api:3.1.1'
// DB
runtimeOnly 'com.mysql:mysql-connector-j'
implementation 'org.springframework.boot:spring-boot-starter-jdbc'
// Connection Pool
implementation 'org.apache.commons:commons-dbcp2:2.13.0'
}
spring.application.name=demo
server.port=8090
# UTF-8 ์ธ์ฝ๋ฉ ํํฐ
spring.servlet.filter.encoding.filter-name=encodingFilter
spring.servlet.filter.encoding.filter-class=org.springframework.web.filter.CharacterEncodingFilter
spring.servlet.filter.encoding.init-param.encoding=UTF-8
spring.servlet.filter.encoding.init-param.forceEncoding=true
spring.servlet.filter.encoding.url-pattern=/*
# JSP ๋ทฐ ์ค์
spring.mvc.view.prefix=/WEB-INF/views/
spring.mvc.view.suffix=.jsp
server.servlet.jsp.init-parameters.development=true
# MySQL ์ฐ๊ฒฐ ์ ๋ณด
spring.datasource.url=jdbc:mysql://localhost:3306/testdb
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.username=root
spring.datasource.password=1234
@ExceptionHandler
(Controller ๋ด๋ถ)@Controller
@RequestMapping("/except")
@Slf4j
public class ExceptionTestController {
@ExceptionHandler(Exception.class)
public String handleAll(Exception e, Model model) {
log.error("error: " + e);
model.addAttribute("ex", e);
return "except/error";
}
@GetMapping("/page01")
public void triggerError() throws FileNotFoundException {
throw new FileNotFoundException("ํ์ผ ์์");
}
@GetMapping("/page02/{num}/{div}")
public String divide(@PathVariable int num, @PathVariable int div, Model model) {
model.addAttribute("result", num / div);
return "except/page02";
}
}
@ControllerAdvice
@ControllerAdvice
@Slf4j
public class GolbalExceptionHandler {
@ExceptionHandler(Exception.class)
public String handleAllException(Exception e, Model model) {
log.info("GlobalExceptionHandler's error : " + e);
model.addAttribute("ex", e);
return "global_error";
}
}
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" isErrorPage="true" %>
<html>
<body>
<h1>EXCEPT/ERROR PAGE</h1>
EXCEPTION : <%=exception %>
</body>
</html>
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" isErrorPage="true" %>
<html>
<body>
<h1>GLOBAL_ERROR PAGE</h1>
EXCEPTION : <%=exception %>
</body>
</html>
[์์ฒญ]
โ
[Controller ์คํ ์ค ์์ธ ๋ฐ์]
โ
[๋ก์ปฌ @ExceptionHandler ๋๋ ์ ์ญ @ControllerAdvice ์ฒ๋ฆฌ]
โ
[Model์ ์์ธ ์ ๋ณด ์ ๋ฌ]
โ
[JSP ์๋ฌ ํ์ด์ง๋ก ์๋ต ์ถ๋ ฅ]
CREATE TABLE tbl_memo (
id INT PRIMARY KEY,
content VARCHAR(255),
email VARCHAR(255),
created_at DATETIME DEFAULT CURRENT_TIMESTAMP
);
@SpringBootTest
public class DataSourceTests {
@Autowired
private DataSource dataSource;
@Test
@DisplayName("MySQL ์ฐ๋ ๋ฐ insert ํ
์คํธ")
public void testInsert() throws Exception {
Connection conn = dataSource.getConnection();
PreparedStatement pstmt = conn.prepareStatement("insert into tbl_memo values(?,?,?,now())");
pstmt.setInt(1, 111);
pstmt.setString(2, "ababab");
pstmt.setString(3, "springboot@test.com");
pstmt.executeUpdate();
}
}
@SpringBootTest
class MybatisConfigTest {
@Autowired
private SqlSessionFactory sqlSessionFactory;
@Test
@DisplayName("MyBatis SqlSessionFactory & SqlSession ์์ฑ ํ์ธ")
public void testSqlSession() throws Exception {
assertNotNull(sqlSessionFactory);
SqlSession sqlSession = sqlSessionFactory.openSession();
assertNotNull(sqlSession);
}
}
@SpringBootApplication
์์ @ComponentScan
์ด ํฌํจ๋์ด ์์ด ๋ณ๋ ์ค์บ ์ค์ ์์ด ์๋ ํ์๋๋ค.jakarta.servlet
๊ธฐ๋ฐ ๋ผ์ด๋ธ๋ฌ๋ฆฌ ์ค์ ํ์@ExceptionHandler
๋๋ @ControllerAdvice
๋ฐฉ์์ผ๋ก ์ ์ฐํ๊ฒ ๊ตฌ์ฑ ๊ฐ๋ฅDataSource
๋ฐ SqlSessionFactory
๊ฒ์ฆ ํ
์คํธ๋ก ์ํ