2025๋ 3์ 27์ผ
๋ณธ ํ๋ก์ ํธ๋ Java๋ก ์์ฑ๋ ์น ๋ฐฑ์๋ ์์คํ ๊ตฌ์กฐ๋ก,
FrontController ํจํด์ ๊ธฐ๋ฐ์ผ๋ก ๋ชจ๋ ์์ฒญ์ ํ๋์ ์ง์ ์ ์์ ๋ฐ์ ์ฒ๋ฆฌํ๊ณ ,
๊ธฐ๋ฅ๋ณ SubController
๋ก ๋ถ๊ธฐํ์ฌ ์ฒ๋ฆฌํ๋ ๊ตฌ์กฐ์ด๋ค.
[View ๋๋ ํ
์คํธ ์ฝ๋]
โ
[FrontController]
โ (endPoint ๊ธฐ์ค ๋ถ๊ธฐ)
[SubController.execute()]
โ
[Service (๋น์ฆ๋์ค ๋ก์ง)]
โ
[Dao (DB ์ ๊ทผ)]
โ
[MySQL - bookDB]
Map<String, Object> params = new HashMap<>();
params.put("endPoint", "/user"); // ๋๋ "/book"
params.put("serviceNo", 1); // ์๋น์ค ๋ฒํธ
params.put("userid", "user01");
params.put("username", "ํ๊ธธ๋");
params.put("password", "1234");
public class FrontController {
// ๊ฐ ์์ฒญ endPoint์ ๋ฐ๋ผ SubController๋ฅผ ์ ์ฅํ๋ Map
private Map<String, SubController> map = new HashMap<>();
private static FrontController instance; // ์ฑ๊ธํค ์ธ์คํด์ค
private FrontController() {
init(); // ์ปจํธ๋กค๋ฌ ์ด๊ธฐํ
}
// ์ฑ๊ธํค ๊ฐ์ฒด ์์ฑ ๋ฐ ๋ฐํ
public static FrontController getInstance() {
if (instance == null) instance = new FrontController();
return instance;
}
// endPoint ๋ณ SubController ๋ฑ๋ก
private void init() {
map.put("/user", new UserController());
map.put("/book", new BookController());
}
// ์คํ ๋ฉ์๋: ์์ฒญ์ ๋ฐ๋ผ ์ ์ ํ SubController ์คํ
public Map<String, Object> execute(Map<String, Object> params) {
String endPoint = (String) params.get("endPoint"); // "/user" or "/book"
SubController controller = map.get(endPoint); // ํด๋น ์ปจํธ๋กค๋ฌ ์ฐพ๊ธฐ
return controller.execute(params); // ์คํ
}
}
// ๊ณตํต ์ธํฐํ์ด์ค
public interface SubController {
// ๋ชจ๋ ํ์ ์ปจํธ๋กค๋ฌ๋ ์ด ๋ฉ์๋๋ฅผ ๊ตฌํํด์ผ ํจ
Map<String, Object> execute(Map<String, Object> params);
}
// ์ฌ์ฉ์ ๋ฐ์ดํฐ์ ์ ํจ์ฑ ๊ฒ์ฌ๋ฅผ ๋ด๋น
private boolean isValid(UserDto userDto) {
// userid๊ฐ ์ต์ 5์ ์ด์์ธ์ง ํ์ธ
if (userDto.getUserid() == null || userDto.getUserid().length() <= 4) {
response.put("error", "userid์ ๊ธธ์ด๋ ์ต์ 5์ ์ด์์ด์ด์ผ ํฉ๋๋ค.");
return false;
}
// userid๊ฐ ์ซ์๋ก ์์ํ๋์ง ํ์ธ
if (userDto.getUserid().matches("^[0-9].*")) {
response.put("error", "userid์ ์ฒซ ๋ฌธ์๋ก ์ซ์๊ฐ ๋ค์ด์ฌ ์ ์์ต๋๋ค.");
return false;
}
return true; // ํต๊ณผ ์ true ๋ฐํ
}
// ๋์ ์ฝ๋ ์ ํจ์ฑ ๊ฒ์ฌ
private boolean isValid(BookDto bookDto) {
// bookCode ์ต์ ๊ธธ์ด 8์
if (bookDto.getBookCode() == null || bookDto.getBookCode().length() < 8) {
response.put("error", "BookCode๋ ์ต์ 8์ ์ด์์ด์ด์ผ ํฉ๋๋ค.");
return false;
}
// ์ต๋ ๊ธธ์ด 255์ ์ ํ
if (bookDto.getBookCode().length() > 255) {
response.put("error", "BookCode๋ 255์๋ฅผ ์ด๊ณผํ ์ ์์ต๋๋ค.");
return false;
}
return true;
}
jdbc:mysql://localhost:3306/bookDB
root
1234
// ์ฌ์ฉ์ ๋ฑ๋ก
public int insert(UserDto userDto) throws SQLException {
try {
// SQL ์ค๋น
pstmt = conn.prepareStatement("insert into tbl_user values(?,?,?,?)");
// ํ๋ผ๋ฏธํฐ ๋ฐ์ธ๋ฉ
pstmt.setString(1, userDto.getUserid());
pstmt.setString(2, userDto.getUsername());
pstmt.setString(3, userDto.getPassword());
pstmt.setString(4, "ROLE_USER"); // ๊ธฐ๋ณธ ์ฌ์ฉ์ ๊ถํ
// ์คํ ๋ฐ ๊ฒฐ๊ณผ ๋ฐํ
return pstmt.executeUpdate();
} catch (SQLException e) {
// ์์ธ ๋ฐ์ ์ ๋ฉ์์ง์ ํจ๊ป throw
throw new SQLException("USERDAO's INSERT SQL EXCEPTION!!");
} finally {
// ๋ฆฌ์์ค ์ ๋ฆฌ
try { pstmt.close(); } catch (Exception e2) {}
}
}
// ๋์ ๋ฑ๋ก
public int insert(BookDto bookDto) throws SQLException {
try {
// SQL ์ค๋น
pstmt = conn.prepareStatement("insert into tbl_book values(?,?,?,?)");
// ํ๋ผ๋ฏธํฐ ์ค์
pstmt.setString(1, bookDto.getBookCode());
pstmt.setString(2, bookDto.getBookName());
pstmt.setString(3, bookDto.getPublisher());
pstmt.setString(4, bookDto.getIsbn());
// ์คํ
return pstmt.executeUpdate();
} catch (SQLException e) {
// ์์ธ ์ฒ๋ฆฌ
throw new SQLException("BOOKDAO's INSERT SQL EXCEPTION!!");
} finally {
// ๋ฆฌ์์ค ์ ๋ฆฌ
try { pstmt.close(); } catch (Exception e2) {}
}
}