diff --git a/build.gradle b/build.gradle index 8883efe..4856ddc 100644 --- a/build.gradle +++ b/build.gradle @@ -1,46 +1,38 @@ plugins { + id 'org.springframework.boot' version '2.7.17' // 스프링 부트 플러그인 적용 + id 'io.spring.dependency-management' version '1.0.15.RELEASE' // 의존성 관리 플러그인 id 'java' - id 'idea' } -idea { - module { - inheritOutputDirs = false - outputDir = file('./webapp/WEB-INF/classes') - } -} - -group 'org.example' -version '1.0-SNAPSHOT' +group = 'org.example' +version = '1.0-SNAPSHOT' +sourceCompatibility = '11' // 사용하는 자바 버전에 맞게 설정 (예: 11) repositories { mavenCentral() } -ext { - springVersion = "5.1.8.RELEASE" - tomcatVersion = '8.5.42' -} - dependencies { - testImplementation 'org.junit.jupiter:junit-jupiter-api:5.8.1' - testImplementation "org.springframework:spring-test:$springVersion" - testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine:5.8.1' + // 1. Spring Boot Web Starter + implementation 'org.springframework.boot:spring-boot-starter-web' + + // 2. Spring Boot Data JPA Starter + implementation 'org.springframework.boot:spring-boot-starter-data-jpa' - implementation("org.springframework:spring-jdbc:$springVersion") - implementation("org.springframework:spring-web:$springVersion") - implementation "org.reflections:reflections:0.10.2" + // 3. JSP 템플릿 엔진 사용을 위한 의존성 + implementation 'org.apache.tomcat.embed:tomcat-embed-jasper' + implementation 'javax.servlet:jstl:1.2' - implementation("org.apache.commons:commons-dbcp2:2.6.0") + // 4. 데이터베이스 드라이버 (H2, MySQL 등 필요한 것 사용) + runtimeOnly 'com.h2database:h2' + runtimeOnly 'mysql:mysql-connector-java' - runtimeOnly("com.h2database:h2:2.1.214") - runtimeOnly('mysql:mysql-connector-java:8.0.28') + // 5. Lombok (선택 사항이지만 강력히 추천) + compileOnly 'org.projectlombok:lombok' + annotationProcessor 'org.projectlombok:lombok' - implementation("org.apache.tomcat.embed:tomcat-embed-core:$tomcatVersion") - implementation("org.apache.tomcat.embed:tomcat-embed-logging-juli:8.5.2") - implementation("org.apache.tomcat.embed:tomcat-embed-jasper:$tomcatVersion") - implementation group: 'javax.servlet', name: 'jstl', version: '1.2' - implementation 'com.fasterxml.jackson.core:jackson-databind:2.12.7.1' + // 6. Spring Boot Test Starter + testImplementation 'org.springframework.boot:spring-boot-starter-test' } test { diff --git a/java-webMVC.ipr b/java-webMVC.ipr index fddddb4..250433f 100644 --- a/java-webMVC.ipr +++ b/java-webMVC.ipr @@ -1,79 +1,982 @@ - - - + + + + + + + + + + + + + + + + - + + + + + + + - - + - + + + + + + + - - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + - + + + + - - + + + + + + + + + + - - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + - + - + - + @@ -83,7 +986,7 @@ @@ -94,11 +997,11 @@ - + - + - + \ No newline at end of file diff --git a/java-webMVC.iws b/java-webMVC.iws index d5bc759..719089c 100644 --- a/java-webMVC.iws +++ b/java-webMVC.iws @@ -1,207 +1,327 @@ + + - - - - + - + - - - - + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/src/main/java/WebServerLauncher.java b/src/main/java/WebServerLauncher.java deleted file mode 100644 index d111fcf..0000000 --- a/src/main/java/WebServerLauncher.java +++ /dev/null @@ -1,22 +0,0 @@ -import org.apache.catalina.startup.Tomcat; - - -import java.io.File; -import java.util.logging.Logger; - -public class WebServerLauncher { - private static final Logger logger = Logger.getLogger(WebServerLauncher.class.getName()); - - public static void main(String[] args) throws Exception { - String webappDirLocation = "./webapp/"; - Tomcat tomcat = new Tomcat(); - tomcat.setPort(8080); - tomcat.getConnector(); - - tomcat.addWebapp("", new File(webappDirLocation).getAbsolutePath()); - logger.info("configuring app with basedir: " + new File(webappDirLocation).getAbsolutePath()); - - tomcat.start(); - tomcat.getServer().await(); - } -} diff --git a/src/main/java/core/jdbc/ConnectionManager.java b/src/main/java/core/jdbc/ConnectionManager.java index 6673010..c649515 100644 --- a/src/main/java/core/jdbc/ConnectionManager.java +++ b/src/main/java/core/jdbc/ConnectionManager.java @@ -1,34 +1,34 @@ -package core.jdbc; - -import org.apache.commons.dbcp2.BasicDataSource; - -import javax.sql.DataSource; -import java.sql.Connection; -import java.sql.SQLException; - -public class ConnectionManager { - private static final String DB_DRIVER = "org.h2.Driver"; - private static final String DB_URL = "jdbc:h2:~/jwp-basic"; - private static final String DB_USERNAME = "sa"; - private static final String DB_PW = ""; - - private static BasicDataSource ds; - public static DataSource getDataSource() { - if (ds == null) { - ds = new BasicDataSource(); - ds.setDriverClassName(DB_DRIVER); - ds.setUrl(DB_URL); - ds.setUsername(DB_USERNAME); - ds.setPassword(DB_PW); - } - return ds; - } - - public static Connection getConnection() { - try { - return getDataSource().getConnection(); - } catch (SQLException e) { - throw new IllegalStateException(e); - } - } -} +//package core.jdbc; +// +//import org.apache.commons.dbcp2.BasicDataSource; +// +//import javax.sql.DataSource; +//import java.sql.Connection; +//import java.sql.SQLException; +// +//public class ConnectionManager { +// private static final String DB_DRIVER = "org.h2.Driver"; +// private static final String DB_URL = "jdbc:h2:~/jwp-basic"; +// private static final String DB_USERNAME = "sa"; +// private static final String DB_PW = ""; +// +// private static BasicDataSource ds; +// public static DataSource getDataSource() { +// if (ds == null) { +// ds = new BasicDataSource(); +// ds.setDriverClassName(DB_DRIVER); +// ds.setUrl(DB_URL); +// ds.setUsername(DB_USERNAME); +// ds.setPassword(DB_PW); +// } +// return ds; +// } +// +// public static Connection getConnection() { // DB의 커넥션 풀 중 커넥션 하나를 받아올 수 있음 +// try { +// return getDataSource().getConnection(); +// } catch (SQLException e) { +// throw new IllegalStateException(e); +// } +// } +//} diff --git a/src/main/java/core/jdbc/JdbcTemplate.java b/src/main/java/core/jdbc/JdbcTemplate.java new file mode 100644 index 0000000..d00cfa3 --- /dev/null +++ b/src/main/java/core/jdbc/JdbcTemplate.java @@ -0,0 +1,73 @@ +//package core.jdbc; +// +// +//import java.sql.Connection; +//import java.sql.PreparedStatement; +//import java.sql.ResultSet; +//import java.sql.SQLException; +//import java.sql.Statement; +//import java.util.ArrayList; +//import java.util.List; +// +//public class JdbcTemplate { +// +// public void update(String sql, PreparedStatementSetter pstmtSetter) throws SQLException { +// try (Connection conn = ConnectionManager.getConnection(); +// PreparedStatement pstmt = conn.prepareStatement(sql);) { +// +// pstmtSetter.setParameters(pstmt); +// pstmt.executeUpdate(); +// } +// } +// +// public void update(String sql, PreparedStatementSetter pstmtSetter, KeyHolder holder) throws SQLException { +// try (Connection conn = ConnectionManager.getConnection(); +// PreparedStatement pstmt = conn.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS)) { +// +// pstmtSetter.setParameters(pstmt); +// pstmt.executeUpdate(); +// +// if (holder != null) { +// try (ResultSet rs = pstmt.getGeneratedKeys()) { +// if (rs.next()) { +// holder.setId(rs.getLong(1)); +// } +// } +// } +// } +// } +// +// +// public List query(String sql, RowMapper rowMapper) throws SQLException { +// List objects = new ArrayList<>(); +// +// try (Connection conn = ConnectionManager.getConnection(); +// PreparedStatement pstmt = conn.prepareStatement(sql); +// ResultSet rs = pstmt.executeQuery();) { +// +// while (rs.next()) { +// T object = rowMapper.mapRow(rs); +// objects.add(object); +// } +// } +// return objects; +// } +// +// public T queryForObject(String sql, PreparedStatementSetter pstmtSetter, RowMapper rowMapper) throws SQLException { +// T object = null; +// +// try(Connection conn = ConnectionManager.getConnection(); +// PreparedStatement pstmt = conn.prepareStatement(sql);) { +// pstmtSetter.setParameters(pstmt); +// +// try (ResultSet rs = pstmt.executeQuery()) { +// if (rs.next()) { +// object = rowMapper.mapRow(rs); +// } +// } +// } +// +// return object; +// } +// +//} diff --git a/src/main/java/core/jdbc/KeyHolder.java b/src/main/java/core/jdbc/KeyHolder.java new file mode 100644 index 0000000..7652569 --- /dev/null +++ b/src/main/java/core/jdbc/KeyHolder.java @@ -0,0 +1,13 @@ +//package core.jdbc; +// +//public class KeyHolder { +// private long id; +// +// public void setId(long id) { +// this.id = id; +// } +// +// public long getId() { +// return id; +// } +//} \ No newline at end of file diff --git a/src/main/java/core/jdbc/PreparedStatementSetter.java b/src/main/java/core/jdbc/PreparedStatementSetter.java new file mode 100644 index 0000000..61bacc7 --- /dev/null +++ b/src/main/java/core/jdbc/PreparedStatementSetter.java @@ -0,0 +1,9 @@ +//package core.jdbc; +// +//import java.sql.PreparedStatement; +//import java.sql.SQLException; +// +//@FunctionalInterface +//public interface PreparedStatementSetter { +// void setParameters(PreparedStatement ps) throws SQLException; +//} diff --git a/src/main/java/core/jdbc/RowMapper.java b/src/main/java/core/jdbc/RowMapper.java new file mode 100644 index 0000000..5051b79 --- /dev/null +++ b/src/main/java/core/jdbc/RowMapper.java @@ -0,0 +1,12 @@ +//package core.jdbc; +// +//import jwp.model.User; +// +//import java.sql.ResultSet; +//import java.sql.SQLException; +// +//@FunctionalInterface +//public interface RowMapper { +// T mapRow(ResultSet rs) throws SQLException; +// +//} diff --git a/src/main/java/jwp/WebServerLauncher.java b/src/main/java/jwp/WebServerLauncher.java new file mode 100644 index 0000000..3472976 --- /dev/null +++ b/src/main/java/jwp/WebServerLauncher.java @@ -0,0 +1,11 @@ +package jwp; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; + +@SpringBootApplication +public class WebServerLauncher { + public static void main(String[] args) throws Exception { + SpringApplication.run(WebServerLauncher.class, args); + } +} diff --git a/src/main/java/jwp/controller/Controller.java b/src/main/java/jwp/controller/Controller.java new file mode 100644 index 0000000..4b0a2b5 --- /dev/null +++ b/src/main/java/jwp/controller/Controller.java @@ -0,0 +1,11 @@ +//package jwp.controller; +// +//import javax.servlet.ServletException; +//import javax.servlet.http.HttpServletRequest; +//import javax.servlet.http.HttpServletResponse; +//import java.io.IOException; +//import java.sql.SQLException; +// +//public interface Controller { +// String execute(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException, SQLException; +//} \ No newline at end of file diff --git a/src/main/java/jwp/controller/CreateQuestionController.java b/src/main/java/jwp/controller/CreateQuestionController.java new file mode 100644 index 0000000..991e174 --- /dev/null +++ b/src/main/java/jwp/controller/CreateQuestionController.java @@ -0,0 +1,37 @@ +//package jwp.controller; +// +// +//import jwp.model.Question; +//import jwp.model.User; +//import jwp.service.QuestionService; +//import jwp.support.session.UserSessionUtils; +//import lombok.RequiredArgsConstructor; +//import org.springframework.stereotype.Controller; +//import org.springframework.web.bind.annotation.PostMapping; +//import org.springframework.web.bind.annotation.RequestMapping; +//import org.springframework.web.bind.annotation.RequestParam; +// +//import javax.servlet.ServletException; +//import javax.servlet.http.HttpServletRequest; +//import javax.servlet.http.HttpServletResponse; +//import javax.servlet.http.HttpSession; +//import java.io.IOException; +//import java.sql.SQLException; +// +//@Controller +//@RequestMapping("/qna") +//@RequiredArgsConstructor +//public class CreateQuestionController{ +// private final QuestionService questionService; +// @PostMapping("/create") +// public String createQuestion(@RequestParam String title, +// @RequestParam String contents, +// HttpSession session) { +// User user = UserSessionUtils.getUserFromSession(session); +// if (user == null) { +// return "redirect:/user/loginForm"; +// } +// questionService.createQuestion(user.getUserId(), title, contents); +// return "redirect:/"; +// } +//} diff --git a/src/main/java/jwp/controller/CreateQuestionFormController.java b/src/main/java/jwp/controller/CreateQuestionFormController.java new file mode 100644 index 0000000..bde186b --- /dev/null +++ b/src/main/java/jwp/controller/CreateQuestionFormController.java @@ -0,0 +1,21 @@ +//package jwp.controller; +// +//import jwp.support.session.UserSessionUtils; +//import org.springframework.stereotype.Controller; +//import org.springframework.web.bind.annotation.GetMapping; +//import org.springframework.web.bind.annotation.RequestMapping; +// +//import javax.servlet.http.HttpSession; +// +//@Controller +//@RequestMapping("/qna") +//public class CreateQuestionFormController { +// +// @GetMapping("/form") +// public String showForm(HttpSession session) { +// if (UserSessionUtils.isLogined(session)) { +// return "qna/form.jsp"; +// } +// return "redirect:/user/loginForm"; +// } +//} diff --git a/src/main/java/jwp/controller/CreateUserController.java b/src/main/java/jwp/controller/CreateUserController.java new file mode 100644 index 0000000..ea4d62e --- /dev/null +++ b/src/main/java/jwp/controller/CreateUserController.java @@ -0,0 +1,22 @@ +//package jwp.controller; +// +//import jwp.model.User; +//import jwp.service.UserService; +//import lombok.RequiredArgsConstructor; +//import org.springframework.stereotype.Controller; +//import org.springframework.web.bind.annotation.ModelAttribute; +//import org.springframework.web.bind.annotation.PostMapping; +//import org.springframework.web.bind.annotation.RequestMapping; +// +//@Controller +//@RequestMapping("/user") +//@RequiredArgsConstructor +//public class CreateUserController { +// private final UserService userService; +// +// @PostMapping("/signup") +// public String createUser(@ModelAttribute User user) { +// userService.createUser(user); +// return "redirect:/user/list"; +// } +//} diff --git a/src/main/java/jwp/controller/DispatcherServlet.java b/src/main/java/jwp/controller/DispatcherServlet.java new file mode 100644 index 0000000..5c8c772 --- /dev/null +++ b/src/main/java/jwp/controller/DispatcherServlet.java @@ -0,0 +1,62 @@ +//package jwp.controller; +// +//import jwp.controller.RequestMapping; +// +//import javax.servlet.RequestDispatcher; +//import javax.servlet.ServletException; +//import javax.servlet.annotation.WebServlet; +//import javax.servlet.http.HttpServlet; +//import javax.servlet.http.HttpServletRequest; +//import javax.servlet.http.HttpServletResponse; +//import java.io.IOException; +//import java.sql.SQLException; +// +//@WebServlet("/") +//public class DispatcherServlet extends HttpServlet { +// private static final String REDIRECT_PREFIX = "redirect:"; +// +// private RequestMapping requestMapping; +// +// @Override +// public void init() throws ServletException { +// super.init(); +// this.requestMapping = new RequestMapping(); +// this.requestMapping.init(); +// } +// +// @Override +// protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { +// String viewName = null; +// try { +// viewName = resolveViewName(req, resp); +// } catch (SQLException e) { +// throw new RuntimeException(e); +// } +// if (viewName == null) { +// return; +// } +// +// if (viewName.startsWith(REDIRECT_PREFIX)) { +// String redirectPath = viewName.substring(REDIRECT_PREFIX.length()); +// resp.sendRedirect(req.getContextPath() + redirectPath); +// return; +// } +// +// RequestDispatcher rd = req.getRequestDispatcher(viewName); +// rd.forward(req, resp); +// } +// +// private String resolveViewName(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException, SQLException { +// String requestUri = req.getRequestURI(); +// String contextPath = req.getContextPath(); +// String path = requestUri.substring(contextPath.length()); +// +// Controller controller = requestMapping.getController(path); +// if (controller == null) { +// resp.sendError(HttpServletResponse.SC_NOT_FOUND); +// return null; +// } +// +// return controller.execute(req, resp); +// } +//} \ No newline at end of file diff --git a/src/main/java/jwp/controller/ForwardController.java b/src/main/java/jwp/controller/ForwardController.java new file mode 100644 index 0000000..b09ca6f --- /dev/null +++ b/src/main/java/jwp/controller/ForwardController.java @@ -0,0 +1,23 @@ +//package jwp.controller; +// +//import org.springframework.stereotype.Controller; +//import org.springframework.web.bind.annotation.GetMapping; +// +//@Controller +//public class ForwardController { +//// private final String viewName; +// @GetMapping("/user/loginForm") +// public String loginForm() { +// return "user/login"; +// } +// +// @GetMapping("/user/loginFailed") +// public String loginFailed() { +// return "user/loginFailed"; +// } +// +// @GetMapping("/user/form") +// public String signUpForm() { +// return "user/form"; +// } +//} \ No newline at end of file diff --git a/src/main/java/jwp/controller/HomeController.java b/src/main/java/jwp/controller/HomeController.java index 751e468..ab15251 100644 --- a/src/main/java/jwp/controller/HomeController.java +++ b/src/main/java/jwp/controller/HomeController.java @@ -1,18 +1,26 @@ package jwp.controller; -import javax.servlet.RequestDispatcher; -import javax.servlet.ServletException; -import javax.servlet.annotation.WebServlet; -import javax.servlet.http.HttpServlet; -import javax.servlet.http.HttpServletRequest; -import javax.servlet.http.HttpServletResponse; -import java.io.IOException; +import jwp.service.QuestionService; +import lombok.RequiredArgsConstructor; +import org.springframework.stereotype.Controller; +import org.springframework.ui.Model; +import org.springframework.web.bind.annotation.GetMapping; -@WebServlet("/") -public class HomeController extends HttpServlet { - @Override - protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { - RequestDispatcher rd = req.getRequestDispatcher("/home.jsp"); - rd.forward(req, resp); + +@Controller +@RequiredArgsConstructor +public class HomeController { + private final QuestionService questionService; + + @GetMapping("/") + public String home(Model model) { + model.addAttribute("questions", questionService.findAll()); + return "home"; } -} +// private final QuestionService questionService; +// @GetMapping("/") +// public String home(Model model) { +// model.addAttribute("questions", questionService.findAll()); +// return "home"; +// } +} \ No newline at end of file diff --git a/src/main/java/jwp/controller/ListUserController.java b/src/main/java/jwp/controller/ListUserController.java new file mode 100644 index 0000000..c896ad7 --- /dev/null +++ b/src/main/java/jwp/controller/ListUserController.java @@ -0,0 +1,70 @@ +//package jwp.controller; +// +//import core.db.MemoryUserRepository; +//import jwp.dao.UserDao; +//import jwp.model.User; +//import javax.servlet.RequestDispatcher; +//import javax.servlet.ServletException; +//import javax.servlet.annotation.WebServlet; +//import javax.servlet.http.HttpServlet; +//import javax.servlet.http.HttpServletRequest; +//import javax.servlet.http.HttpServletResponse; +//import javax.servlet.http.HttpSession; +//import java.io.IOException; +//import java.sql.SQLException; +//import java.util.Collection; +// +////@WebServlet("/user/list") +//public class ListUserController implements Controller { +// private static final String USER_SESSION_KEY = "user"; +//// private static final MemoryUserRepository userRepository = MemoryUserRepository.getInstance(); +// private final UserDao userDao = new UserDao(); +// +// @Override +// public String execute(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { +// if (!"GET".equalsIgnoreCase(req.getMethod())) { +// return "redirect:/user/login"; +// } +// +// HttpSession session = req.getSession(false); +// if (session == null) { +// return "redirect:/user/login"; +// } +// +// User user = (User) session.getAttribute(USER_SESSION_KEY); +// if (user == null) { +// return "redirect:/user/login"; +// } +// +// Collection users = null; +// try { +// users = userDao.findAll(); +// } catch (SQLException e) { +// throw new RuntimeException(e); +// } +// req.setAttribute("users", users); +// return "/user/list.jsp"; +// } +//} +//// @Override +//// protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { +//// HttpSession session = req.getSession(false); +//// if (session == null) { +//// resp.sendRedirect(req.getContextPath() + "/user/login"); +//// return; +//// } +//// +//// User user = (User) session.getAttribute(USER_SESSION_KEY); +//// if (user == null) { +//// resp.sendRedirect(req.getContextPath() + "/user/login"); +//// return; +//// } +//// +//// MemoryUserRepository userRepository = MemoryUserRepository.getInstance(); +//// Collection users = userRepository.findAll(); +//// req.setAttribute("users", users); +//// +//// RequestDispatcher rd = req.getRequestDispatcher("/user/list.jsp"); +//// rd.forward(req, resp); +//// } +// diff --git a/src/main/java/jwp/controller/LoginController.java b/src/main/java/jwp/controller/LoginController.java new file mode 100644 index 0000000..15f97a8 --- /dev/null +++ b/src/main/java/jwp/controller/LoginController.java @@ -0,0 +1,30 @@ +//package jwp.controller; +// +//import jwp.service.UserService; +//import jwp.support.session.UserSessionUtils; +//import lombok.RequiredArgsConstructor; +//import org.springframework.stereotype.Controller; +//import org.springframework.web.bind.annotation.PostMapping; +//import org.springframework.web.bind.annotation.RequestMapping; +//import org.springframework.web.bind.annotation.RequestParam; +// +//import javax.servlet.http.HttpSession; +// +//@Controller +//@RequestMapping("/user") +//@RequiredArgsConstructor +//public class LoginController { +// private final UserService userService; +// +// @PostMapping("/login") +// public String login(@RequestParam String userId, +// @RequestParam String password, +// HttpSession session) { +// return userService.authenticate(userId, password) +// .map(user -> { +// session.setAttribute(UserSessionUtils.USER_SESSION_KEY, user); +// return "redirect:/"; +// }) +// .orElse("redirect:/user/loginFailed"); +// } +//} \ No newline at end of file diff --git a/src/main/java/jwp/controller/LogoutController.java b/src/main/java/jwp/controller/LogoutController.java new file mode 100644 index 0000000..344f8df --- /dev/null +++ b/src/main/java/jwp/controller/LogoutController.java @@ -0,0 +1,21 @@ +//package jwp.controller; +// +//import jwp.support.session.UserSessionUtils; +//import org.springframework.stereotype.Controller; +//import org.springframework.web.bind.annotation.GetMapping; +//import org.springframework.web.bind.annotation.RequestMapping; +// +//import javax.servlet.http.HttpSession; +// +//@Controller +//@RequestMapping("/user") +//public class LogoutController { +// +// @GetMapping("/logout") +// public String logout(HttpSession session) { +// if (session != null) { +// session.removeAttribute(UserSessionUtils.USER_SESSION_KEY); +// } +// return "redirect:/"; +// } +//} \ No newline at end of file diff --git a/src/main/java/jwp/controller/QuestionController.java b/src/main/java/jwp/controller/QuestionController.java new file mode 100644 index 0000000..ed53956 --- /dev/null +++ b/src/main/java/jwp/controller/QuestionController.java @@ -0,0 +1,51 @@ +package jwp.controller; + +import jwp.model.User; +import jwp.service.QuestionService; +import jwp.support.session.UserSessionUtils; +import lombok.RequiredArgsConstructor; +import org.springframework.stereotype.Controller; +import org.springframework.ui.Model; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.PostMapping; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RequestParam; + +import javax.servlet.http.HttpSession; + +@Controller +@RequestMapping("/qna") +@RequiredArgsConstructor +public class QuestionController { + private final QuestionService questionService; + + @GetMapping("/form") + public String showForm(HttpSession session) { + if (UserSessionUtils.isLogined(session)) { + return "qna/form"; + } + return "redirect:/user/loginForm"; + } + + @PostMapping("/create") + public String createQuestion(@RequestParam String title, + @RequestParam String contents, + HttpSession session) { + User user = UserSessionUtils.getUserFromSession(session); + if (user == null) { + return "redirect:/user/loginForm"; + } + questionService.createQuestion(user.getUserId(), title, contents); + return "redirect:/"; + } + + @GetMapping("/show") + public String showQuestion(@RequestParam Long questionId, Model model) { + return questionService.findById(questionId) + .map(question -> { + model.addAttribute("question", question); + return "qna/show"; + }) + .orElse("redirect:/"); + } +} diff --git a/src/main/java/jwp/controller/RequestMapping.java b/src/main/java/jwp/controller/RequestMapping.java new file mode 100644 index 0000000..32ccc87 --- /dev/null +++ b/src/main/java/jwp/controller/RequestMapping.java @@ -0,0 +1,28 @@ +//package jwp.controller; +// +//import jwp.controller.HomeController; +//import java.util.HashMap; +//import java.util.Map; +// +//public class RequestMapping { +// private final Map mappings = new HashMap<>(); +// +// public void init() { +// mappings.put("/", new HomeController()); +// mappings.put("/user/login", new LoginController()); +// mappings.put("/user/loginForm", new ForwardController("/user/login.jsp")); +// mappings.put("/user/logout", new LogoutController()); +// mappings.put("/user/updateForm", new UpdateUserFormController()); +// mappings.put("/user/update", new UpdateUserController()); +// mappings.put("/user/list", new ListUserController()); +// mappings.put("/user/loginFailed", new ForwardController("/user/loginFailed.jsp")); +// mappings.put("/qna/form", new CreateQuestionFormController()); +// mappings.put("/qna/create", new CreateQuestionController()); +// mappings.put("/qna/show", new ShowQuestionController()); +// } +// +// +// public Controller getController(String path) { +// return mappings.get(path); +// } +//} \ No newline at end of file diff --git a/src/main/java/jwp/controller/ShowQuestionController.java b/src/main/java/jwp/controller/ShowQuestionController.java new file mode 100644 index 0000000..e730211 --- /dev/null +++ b/src/main/java/jwp/controller/ShowQuestionController.java @@ -0,0 +1,26 @@ +//package jwp.controller; +// +//import jwp.service.QuestionService; +//import lombok.RequiredArgsConstructor; +//import org.springframework.stereotype.Controller; +//import org.springframework.ui.Model; +//import org.springframework.web.bind.annotation.GetMapping; +//import org.springframework.web.bind.annotation.RequestMapping; +//import org.springframework.web.bind.annotation.RequestParam; +// +//@Controller +//@RequestMapping("/qna") +//@RequiredArgsConstructor +//public class ShowQuestionController { +// private final QuestionService questionService; +// +// @GetMapping("/show") +// public String showQuestion(@RequestParam Long questionId, Model model) { +// return questionService.findById(questionId) +// .map(question -> { +// model.addAttribute("question", question); +// return "qna/show"; +// }) +// .orElse("redirect:/"); +// } +//} \ No newline at end of file diff --git a/src/main/java/jwp/controller/UpdateUserController.java b/src/main/java/jwp/controller/UpdateUserController.java new file mode 100644 index 0000000..003cffd --- /dev/null +++ b/src/main/java/jwp/controller/UpdateUserController.java @@ -0,0 +1,30 @@ +//package jwp.controller; +// +//import jwp.model.User; +//import jwp.service.UserService; +//import jwp.support.session.UserSessionUtils; +//import lombok.RequiredArgsConstructor; +//import org.springframework.stereotype.Controller; +//import org.springframework.web.bind.annotation.ModelAttribute; +//import org.springframework.web.bind.annotation.PostMapping; +//import org.springframework.web.bind.annotation.RequestMapping; +// +//import javax.servlet.http.HttpSession; +// +// +//@Controller +//@RequestMapping("/user") +//@RequiredArgsConstructor +//public class UpdateUserController { +// private final UserService userService; +// @PostMapping("/update") +// public String updateUser(@ModelAttribute User updateUser, HttpSession session) { +// User sessionUser = UserSessionUtils.getUserFromSession(session); +// if (sessionUser == null || !sessionUser.isSameUser(updateUser.getUserId())) { +// return "redirect:/"; +// } +// User persistedUser = userService.updateUser(updateUser); +// session.setAttribute(UserSessionUtils.USER_SESSION_KEY, persistedUser); +// return "redirect:/user/list"; +// } +//} \ No newline at end of file diff --git a/src/main/java/jwp/controller/UpdateUserFormController.java b/src/main/java/jwp/controller/UpdateUserFormController.java new file mode 100644 index 0000000..1049b3c --- /dev/null +++ b/src/main/java/jwp/controller/UpdateUserFormController.java @@ -0,0 +1,34 @@ +//package jwp.controller; +// +//import jwp.model.User; +//import jwp.service.UserService; +//import jwp.support.session.UserSessionUtils; +//import lombok.RequiredArgsConstructor; +//import org.springframework.stereotype.Controller; +//import org.springframework.ui.Model; +//import org.springframework.web.bind.annotation.GetMapping; +//import org.springframework.web.bind.annotation.RequestMapping; +//import org.springframework.web.bind.annotation.RequestParam; +// +//import javax.servlet.http.HttpSession; +// +//@Controller +//@RequestMapping("/user") +//@RequiredArgsConstructor +//public class UpdateUserFormController { +// private final UserService userService; +// +// @GetMapping("/updateForm") +// public String showUpdateForm(@RequestParam String userId, HttpSession session, Model model) { +// User sessionUser = UserSessionUtils.getUserFromSession(session); +// if (sessionUser == null || !sessionUser.isSameUser(userId)) { +// return "redirect:/"; +// } +// return userService.findByUserId(userId) +// .map(user -> { +// model.addAttribute("user", user); +// return "user/updateForm"; +// }) +// .orElse("redirect:/"); +// } +//} \ No newline at end of file diff --git a/src/main/java/jwp/controller/UserController.java b/src/main/java/jwp/controller/UserController.java new file mode 100644 index 0000000..c96a84e --- /dev/null +++ b/src/main/java/jwp/controller/UserController.java @@ -0,0 +1,107 @@ +package jwp.controller; + +import jwp.model.User; +import jwp.service.UserService; +import jwp.support.session.UserSessionUtils; +import lombok.RequiredArgsConstructor; +import org.springframework.stereotype.Controller; +import org.springframework.ui.Model; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.ModelAttribute; +import org.springframework.web.bind.annotation.PostMapping; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RequestParam; + +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpSession; +@Controller +@RequestMapping("/user") +@RequiredArgsConstructor +public class UserController { + private final UserService userService; + + @GetMapping("/form") + public String signUpForm() { + return "user/form"; + } + + @PostMapping("/signup") + public String createUser(@RequestParam String userId, + @RequestParam String password, + @RequestParam String name, + @RequestParam String email) { + userService.createUser(new User(userId, password, name, email)); + return "redirect:/user/list"; + } + + @GetMapping("/list") + public String listUsers(HttpSession session, Model model) { + if (!UserSessionUtils.isLogined(session)) { + return "redirect:/user/loginForm"; + } + model.addAttribute("users", userService.findAllUsers()); + return "user/list"; + } + + + @GetMapping("/updateForm") + public String showUpdateForm(@RequestParam String userId, + HttpSession session, + Model model) { + User sessionUser = UserSessionUtils.getUserFromSession(session); + if (sessionUser == null || !sessionUser.isSameUser(userId)) { + return "redirect:/"; + } + return userService.findByUserId(userId) + .map(user -> { + model.addAttribute("user", user); + return "user/updateForm"; + }) + .orElse("redirect:/"); + } + + @PostMapping("/update") + public String updateUser(@RequestParam String userId, + @RequestParam String password, + @RequestParam String name, + @RequestParam String email, + HttpSession session) { + User sessionUser = UserSessionUtils.getUserFromSession(session); + if (sessionUser == null || !sessionUser.isSameUser(userId)) { + return "redirect:/"; + } + User persistedUser = userService.updateUser(new User(userId, password, name, email)); + session.setAttribute(UserSessionUtils.USER_SESSION_KEY, persistedUser); + return "redirect:/user/list"; + } + + @GetMapping("/loginForm") + public String loginForm() { + return "user/login"; + } + + @GetMapping("/loginFailed") + public String loginFailed() { + return "user/loginFailed"; + } + + @PostMapping("/login") + public String login(@RequestParam String userId, + @RequestParam String password, + HttpSession session) { + return userService.authenticate(userId, password) + .map(user -> { + session.setAttribute(UserSessionUtils.USER_SESSION_KEY, user); + return "redirect:/"; + }) + .orElse("redirect:/user/loginFailed"); + } + + @GetMapping("/logout") + public String logout(HttpSession session) { + if (session != null) { + session.removeAttribute(UserSessionUtils.USER_SESSION_KEY); + } + return "redirect:/"; + } +} \ No newline at end of file diff --git a/src/main/java/jwp/dao/QuestionDao.java b/src/main/java/jwp/dao/QuestionDao.java new file mode 100644 index 0000000..3ec821c --- /dev/null +++ b/src/main/java/jwp/dao/QuestionDao.java @@ -0,0 +1,62 @@ +//package jwp.dao; +// +//import core.jdbc.JdbcTemplate; +//import core.jdbc.KeyHolder; +//import core.jdbc.PreparedStatementSetter; +//import core.jdbc.RowMapper; +//import jwp.model.Question; +// +//import java.sql.SQLException; +//import java.util.List; +// +//public class QuestionDao { +// private final JdbcTemplate jdbcTemplate = new JdbcTemplate<>(); +// private final RowMapper questionRowMapper = rs -> { +// Integer countOfAnswer = rs.getInt("countOfAnswer"); +// if (rs.wasNull()) { +// countOfAnswer = null; +// } +// return new Question( +// rs.getLong("questionId"), +// rs.getString("writer"), +// rs.getString("title"), +// rs.getString("contents"), +// rs.getTimestamp("createdDate"), +// countOfAnswer +// ); +// }; +// +// public List findAll() throws SQLException { +// String sql = "SELECT questionId, writer, title, contents, createdDate, countOfAnswer FROM QUESTIONS ORDER BY createdDate DESC"; +// return jdbcTemplate.query(sql, questionRowMapper); +// } +// +// public Question findByQuestionId(long questionId) throws SQLException { +// String sql = "SELECT questionId, writer, title, contents, createdDate, countOfAnswer FROM QUESTIONS WHERE questionId = ?"; +// PreparedStatementSetter setter = pstmt -> pstmt.setLong(1, questionId); +// return jdbcTemplate.queryForObject(sql, setter, questionRowMapper); +// } +// +// public Question insert(Question question) throws SQLException { +// String sql = "INSERT INTO QUESTIONS (writer, title, contents, createdDate, countOfAnswer) VALUES (?, ?, ?, CURRENT_TIMESTAMP(), ?)"; +// PreparedStatementSetter setter = pstmt -> { +// pstmt.setString(1, question.getWriter()); +// pstmt.setString(2, question.getTitle()); +// pstmt.setString(3, question.getContents()); +// Integer countOfAnswer = question.getCountOfAnswer(); +// if (countOfAnswer == null) { +// pstmt.setInt(4, 0); +// } else { +// pstmt.setInt(4, countOfAnswer); +// } +// }; +// +// KeyHolder keyHolder = new KeyHolder(); +// jdbcTemplate.update(sql, setter, keyHolder); +// long questionId = keyHolder.getId(); +// if (questionId == 0L) { +// return null; +// } +// return findByQuestionId(questionId); +// } +//} \ No newline at end of file diff --git a/src/main/java/jwp/dao/UserDao.java b/src/main/java/jwp/dao/UserDao.java new file mode 100644 index 0000000..3f19954 --- /dev/null +++ b/src/main/java/jwp/dao/UserDao.java @@ -0,0 +1,83 @@ +//package jwp.dao; +// +////import core.jdbc.JdbcTemplate; +////import core.jdbc.PreparedStatementSetter; +////import core.jdbc.RowMapper; +//import core.jdbc.JdbcTemplate; +//import jwp.model.User; +//import lombok.RequiredArgsConstructor; +//import org.springframework.stereotype.Repository; +//import org.springframework.transaction.annotation.Transactional; +// +// +//import javax.persistence.EntityManager; +//import java.sql.SQLException; +//import java.util.List; +// +//@Repository +//@RequiredArgsConstructor +//public class UserDao { +// +// private final EntityManager em; +// +// @Transactional +// public void insert(User user) throws SQLException { +// em.persist(user); +//// String sql = "INSERT INTO USERS VALUES(?, ?, ?, ?)"; +//// PreparedStatementSetter pss = pstmt -> { +//// pstmt.setString(1, user.getUserId()); +//// pstmt.setString(2, user.getPassword()); +//// pstmt.setString(3, user.getName()); +//// pstmt.setString(4, user.getEmail()); +//// }; +//// jdbcTemplate.update(sql, pss); +// } +// +// public void update(User user) throws SQLException { +// em.merge(user); +//// String sql = "UPDATE USERS SET password =?, name =?, email =? WHERE userId =?"; +//// PreparedStatementSetter pstmtSetter = pstmt -> { +//// pstmt.setString(1, user.getPassword()); +//// pstmt.setString(2, user.getName()); +//// pstmt.setString(3, user.getEmail()); +//// pstmt.setString(4, user.getUserId()); +//// }; +//// jdbcTemplate.update(sql, pstmtSetter); +// } +// +//// public void delete(User user) throws SQLException { +//// String sql = "DELETE FROM USERS WHERE userId =?"; +//// PreparedStatementSetter pss = pstmt -> { +//// pstmt.setString(1, user.getUserId()); +//// }; +//// jdbcTemplate.update(sql, pss); +//// } +// +// // TODO findAll, findByUserId +// public List findAll() throws SQLException { +// em.createQuery("select u from User u", User.class).getResultList(); +//// String sql = "SELECT * FROM USERS"; +//// RowMapper rowMapper = rs -> new User(rs.getString("userId"), +//// rs.getString("password"), +//// rs.getString("name"), +//// rs.getString("email")); +//// return jdbcTemplate.query(sql, rowMapper); +// } +// +// public User findByUserId(String userId) throws SQLException { +// em.find(User.class, userId); +// +//// String sql = "SELECT * FROM USERS WHERE userId=?"; +//// +//// PreparedStatementSetter pstmtSetter = pstmt -> { +//// pstmt.setString(1, userId); +//// }; +//// +//// RowMapper rowMapper = rs -> new User(rs.getString("userId"), +//// rs.getString("password"), +//// rs.getString("name"), +//// rs.getString("email") +//// ); +//// return jdbcTemplate.queryForObject(sql, pstmtSetter, rowMapper); +// } +//} diff --git a/src/main/java/jwp/model/Question.java b/src/main/java/jwp/model/Question.java new file mode 100644 index 0000000..217fa48 --- /dev/null +++ b/src/main/java/jwp/model/Question.java @@ -0,0 +1,91 @@ +package jwp.model; +import lombok.AccessLevel; +import lombok.NoArgsConstructor; + +import javax.persistence.Column; +import javax.persistence.Entity; +import javax.persistence.GeneratedValue; +import javax.persistence.GenerationType; +import javax.persistence.Id; +import javax.persistence.PrePersist; +import javax.persistence.Table; +import java.time.LocalDateTime; + +@Entity +@Table(name = "QUESTIONS") +@NoArgsConstructor(access = AccessLevel.PROTECTED) +public class Question { +// private final Long questionId; +// private final String writer; +// private final String title; +// private final String contents; +// private final Timestamp createdDate; +// private final Integer countOfAnswer; + + @Id + @GeneratedValue(strategy = GenerationType.IDENTITY) + @Column(name = "questionId") + private Long questionId; + + @Column(name = "writer", nullable = false, length = 30) + private String writer; + + @Column(name = "title", nullable = false, length = 50) + private String title; + + @Column(name = "contents", nullable = false, length = 5000) + private String contents; + + @Column(name = "createdDate", nullable = false) + private LocalDateTime createdDate; + + @Column(name = "countOfAnswer") + private Integer countOfAnswer; + + public Question(String writer, String title, String contents) { + this(null, writer, title, contents, null, null); + } + + public Question(Long questionId, String writer, String title, String contents, LocalDateTime createdDate, Integer countOfAnswer) { + this.questionId = questionId; + this.writer = writer; + this.title = title; + this.contents = contents; + this.createdDate = createdDate; + this.countOfAnswer = countOfAnswer; + } + + @PrePersist + protected void onCreate() { + if (createdDate == null) { + createdDate = LocalDateTime.now(); + } + if (countOfAnswer == null) { + countOfAnswer = 0; + } + } + + public Long getQuestionId() { + return questionId; + } + + public String getWriter() { + return writer; + } + + public String getTitle() { + return title; + } + + public String getContents() { + return contents; + } + + public LocalDateTime getCreatedDate() { + return createdDate; + } + + public Integer getCountOfAnswer() { + return countOfAnswer; + } +} \ No newline at end of file diff --git a/src/main/java/jwp/model/User.java b/src/main/java/jwp/model/User.java index 635bf27..ef7cd13 100644 --- a/src/main/java/jwp/model/User.java +++ b/src/main/java/jwp/model/User.java @@ -1,6 +1,17 @@ package jwp.model; +import lombok.AccessLevel; +import lombok.NoArgsConstructor; + +import javax.persistence.Entity; +import javax.persistence.Id; +import javax.persistence.Table; + +@Entity +@Table(name = "USERS") +@NoArgsConstructor(access = AccessLevel.PROTECTED) public class User { + @Id private String userId; private String password; private String name; diff --git a/src/main/java/jwp/repository/QuestionRepository.java b/src/main/java/jwp/repository/QuestionRepository.java new file mode 100644 index 0000000..d1fa6cf --- /dev/null +++ b/src/main/java/jwp/repository/QuestionRepository.java @@ -0,0 +1,7 @@ +package jwp.repository; + +import jwp.model.Question; +import org.springframework.data.jpa.repository.JpaRepository; + +public interface QuestionRepository extends JpaRepository { +} \ No newline at end of file diff --git a/src/main/java/jwp/repository/UserRepository.java b/src/main/java/jwp/repository/UserRepository.java new file mode 100644 index 0000000..1fae9e7 --- /dev/null +++ b/src/main/java/jwp/repository/UserRepository.java @@ -0,0 +1,7 @@ +package jwp.repository; + +import jwp.model.User; +import org.springframework.data.jpa.repository.JpaRepository; + +public interface UserRepository extends JpaRepository { +} \ No newline at end of file diff --git a/src/main/java/jwp/service/QuestionService.java b/src/main/java/jwp/service/QuestionService.java new file mode 100644 index 0000000..230d911 --- /dev/null +++ b/src/main/java/jwp/service/QuestionService.java @@ -0,0 +1,32 @@ +package jwp.service; + +import jwp.model.Question; +import jwp.repository.QuestionRepository; +import lombok.RequiredArgsConstructor; +import org.springframework.data.domain.Sort; +import org.springframework.stereotype.Service; +import org.springframework.transaction.annotation.Transactional; + +import java.util.List; +import java.util.Optional; + +@Service +@RequiredArgsConstructor +@Transactional(readOnly = true) +public class QuestionService { + private final QuestionRepository questionRepository; + + public List findAll() { + return questionRepository.findAll(Sort.by(Sort.Direction.DESC, "createdDate")); + } + + public Optional findById(Long id) { + return questionRepository.findById(id); + } + + @Transactional + public Question createQuestion(String writer, String title, String contents) { + Question question = new Question(writer, title, contents); + return questionRepository.save(question); + } +} \ No newline at end of file diff --git a/src/main/java/jwp/service/UserService.java b/src/main/java/jwp/service/UserService.java new file mode 100644 index 0000000..8c42047 --- /dev/null +++ b/src/main/java/jwp/service/UserService.java @@ -0,0 +1,43 @@ +package jwp.service; + +import jwp.model.User; +import jwp.repository.UserRepository; +import lombok.RequiredArgsConstructor; +import org.springframework.stereotype.Service; +import org.springframework.transaction.annotation.Transactional; + +import java.util.List; +import java.util.Optional; + +@Service +@RequiredArgsConstructor +@Transactional(readOnly = true) +public class UserService { + private final UserRepository userRepository; + + @Transactional + public User createUser(User user) { + return userRepository.save(user); + } + + public List findAllUsers() { + return userRepository.findAll(); + } + + public Optional findByUserId(String userId) { + return userRepository.findById(userId); + } + + @Transactional + public User updateUser(User updateUser) { + User persistedUser = userRepository.findById(updateUser.getUserId()) + .orElseThrow(() -> new IllegalArgumentException("User not found: " + updateUser.getUserId())); + persistedUser.update(updateUser); + return persistedUser; + } + + public Optional authenticate(String userId, String password) { + return findByUserId(userId) + .filter(user -> user.matchPassword(password)); + } +} \ No newline at end of file diff --git a/src/main/java/jwp/support/context/ContextLoaderListener.java b/src/main/java/jwp/support/context/ContextLoaderListener.java index e48b948..76b88f9 100644 --- a/src/main/java/jwp/support/context/ContextLoaderListener.java +++ b/src/main/java/jwp/support/context/ContextLoaderListener.java @@ -1,25 +1,27 @@ -package jwp.support.context; - -import javax.servlet.ServletContextEvent; -import javax.servlet.ServletContextListener; -import javax.servlet.annotation.WebListener; -import java.util.logging.Logger; - -@WebListener -public class ContextLoaderListener implements ServletContextListener { - private static final Logger logger = Logger.getLogger(ContextLoaderListener.class.getName()); - - @Override - public void contextInitialized(ServletContextEvent sce) { -// ResourceDatabasePopulator populator = new ResourceDatabasePopulator(); -// populator.addScript(new ClassPathResource("jwp.sql")); -// ConnectionManager.getDataSource(); -// DatabasePopulatorUtils.execute(populator, ConnectionManager.getDataSource()); - - logger.info("Completed Load ServletContext!"); - } - - @Override - public void contextDestroyed(ServletContextEvent sce) { - } -} \ No newline at end of file +//package jwp.controller; +// +//import jwp.service.UserService; +//import jwp.support.session.UserSessionUtils; +//import lombok.RequiredArgsConstructor; +//import org.springframework.stereotype.Controller; +//import org.springframework.ui.Model; +//import org.springframework.web.bind.annotation.GetMapping; +//import org.springframework.web.bind.annotation.RequestMapping; +// +//import javax.servlet.http.HttpSession; +// +//@Controller +//@RequestMapping("/user") +//@RequiredArgsConstructor +//public class UserController { +// private final UserService userService; +// +// @GetMapping("/list") +// public String listUsers(HttpSession session, Model model) { +// if (!UserSessionUtils.isLogined(session)) { +// return "redirect:/user/loginForm"; +// } +// model.addAttribute("users", userService.findAllUsers()); +// return "user/list"; +// } +//} \ No newline at end of file diff --git a/src/main/java/jwp/support/session/UserSessionUtils.java b/src/main/java/jwp/support/session/UserSessionUtils.java new file mode 100644 index 0000000..3604876 --- /dev/null +++ b/src/main/java/jwp/support/session/UserSessionUtils.java @@ -0,0 +1,27 @@ +package jwp.support.session; + +import jwp.model.User; + +import javax.servlet.http.HttpSession; + +public final class UserSessionUtils { + public static final String USER_SESSION_KEY = "user"; + + private UserSessionUtils() { + } + + public static boolean isLogined(HttpSession session) { + return getUserFromSession(session) != null; + } + + public static User getUserFromSession(HttpSession session) { + if (session == null) { + return null; + } + Object value = session.getAttribute(USER_SESSION_KEY); + if (value instanceof User) { + return (User) value; + } + return null; + } +} \ No newline at end of file diff --git a/src/main/resources/application.properties b/src/main/resources/application.properties new file mode 100644 index 0000000..98d27ca --- /dev/null +++ b/src/main/resources/application.properties @@ -0,0 +1,14 @@ +server.port=8080 +# DATABASE PLATFORM +spring.jpa.database-platform=org.hibernate.dialect.H2Dialect + +# DATASOURCE (DB CONNECTION) +spring.datasource.driver-class-name=org.h2.Driver +spring.datasource.url=jdbc:h2:~/jwp-basic +spring.datasource.username=sa +spring.datasource.password= +spring.sql.init.mode=always +spring.jpa.hibernate.naming.physical-strategy=org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl + +spring.mvc.view.prefix=/ +spring.mvc.view.suffix=.jsp \ No newline at end of file diff --git a/webapp/WEB-INF/.gitignore b/src/main/webapp/WEB-INF/.gitignore similarity index 100% rename from webapp/WEB-INF/.gitignore rename to src/main/webapp/WEB-INF/.gitignore diff --git a/webapp/css/styles.css b/src/main/webapp/css/styles.css similarity index 100% rename from webapp/css/styles.css rename to src/main/webapp/css/styles.css diff --git a/webapp/css/styles2.css b/src/main/webapp/css/styles2.css similarity index 100% rename from webapp/css/styles2.css rename to src/main/webapp/css/styles2.css diff --git a/webapp/favicon.ico b/src/main/webapp/favicon.ico similarity index 100% rename from webapp/favicon.ico rename to src/main/webapp/favicon.ico diff --git a/src/main/webapp/home.jsp b/src/main/webapp/home.jsp new file mode 100644 index 0000000..b1c7a0b --- /dev/null +++ b/src/main/webapp/home.jsp @@ -0,0 +1,118 @@ +<%@ page contentType="text/html;charset=UTF-8" language="java" %> +<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> + + + +<%@ include file="/include/header.jspf" %> + +<%@ include file="/include/navigation.jspf" %> + +
+

Q&A

+
+ +
+
+
+ +
+
+<%-- 질문하기--%> + 질문하기 +
+
+
+
+ + + + + \ No newline at end of file diff --git a/webapp/img/KUIT.png b/src/main/webapp/img/KUIT.png similarity index 100% rename from webapp/img/KUIT.png rename to src/main/webapp/img/KUIT.png diff --git a/webapp/img/picture.jpeg b/src/main/webapp/img/picture.jpeg similarity index 100% rename from webapp/img/picture.jpeg rename to src/main/webapp/img/picture.jpeg diff --git a/webapp/include/header.jspf b/src/main/webapp/include/header.jspf similarity index 100% rename from webapp/include/header.jspf rename to src/main/webapp/include/header.jspf diff --git a/webapp/include/navigation.jspf b/src/main/webapp/include/navigation.jspf similarity index 92% rename from webapp/include/navigation.jspf rename to src/main/webapp/include/navigation.jspf index ed221f9..162999f 100644 --- a/webapp/include/navigation.jspf +++ b/src/main/webapp/include/navigation.jspf @@ -30,8 +30,10 @@ diff --git a/webapp/js/scripts.js b/src/main/webapp/js/scripts.js similarity index 100% rename from webapp/js/scripts.js rename to src/main/webapp/js/scripts.js diff --git a/src/main/webapp/qna/form.jsp b/src/main/webapp/qna/form.jsp new file mode 100644 index 0000000..2677faf --- /dev/null +++ b/src/main/webapp/qna/form.jsp @@ -0,0 +1,62 @@ +<%@ page contentType="text/html;charset=UTF-8" language="java" %> +<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> + + + +<%@ include file="/include/header.jspf" %> + +<%@ include file="/include/navigation.jspf" %> + +
+ + <%--
--%> + + <%--
--%> + <%--
--%> + <%-- --%> + <%-- --%> + <%--
--%> + <%--
--%> + <%-- --%> + <%-- --%> + <%--
--%> + <%--
--%> + <%-- --%> + <%-- --%> + <%--
--%> + <%--
--%> + <%-- --%> + <%-- --%> + <%--
--%> + <%--
--%> + <%--
--%> + <%-- --%> + <%--
--%> + <%--
--%> +
+
+
+
+ + +
+
+ + +
+
+ + +
+ +
+
+
+
+
+ + + + + \ No newline at end of file diff --git a/src/main/webapp/qna/show.jsp b/src/main/webapp/qna/show.jsp new file mode 100644 index 0000000..83bc994 --- /dev/null +++ b/src/main/webapp/qna/show.jsp @@ -0,0 +1,187 @@ +<%@ page contentType="text/html;charset=UTF-8" language="java" %> +<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> +<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %> + + + +<%@ include file="/include/header.jspf" %> + +<%@ include file="/include/navigation.jspf" %> + +
+
+ <%--

객체지향을 가장 잘 다룬 책이 뭐가 있나요?

--%> +

${question.title}

+
+
+
+
+
+ +
+
+ + <%-- --%> + + +<%-- 2024-09-29 22:11--%> + ${question.createdDate} + +
+
+
+ <%--

저는 지금 '객체지향의 사실과 오해' 라는 책을 읽고 있는데 정말 좋아요

--%> + <%--

이해가 쏙쏙 됩니다!

--%> +

${question.contents}

+
+
+ +
+
+ +
+
+ <%--

2개의 의견

--%> + + + + +

${answerCount}개의 의견

+
+ <%--
--%> + <%--
--%> + <%--
--%> + <%-- --%> + <%--
--%> + <%--
--%> + <%-- --%> + <%-- --%> + <%-- 2024-09-29 22:15--%> + <%-- --%> + <%--
--%> + <%--
--%> + <%--
--%> + <%--

오브젝트여

--%> + <%--
--%> + <%--
--%> + <%--
    --%> + <%--
  • --%> + <%-- --%> + <%-- 수정--%> + <%--
  • --%> + <%--
  • --%> + <%-- --%> + <%--
    --%> + <%-- --%> + <%-- --%> + <%--
    --%> + <%--
  • --%> + <%--
--%> + <%--
--%> + <%--
--%> + <%--
--%> + <%--
--%> + <%--
--%> + <%-- --%> + <%--
--%> + <%--
--%> + <%-- --%> + <%-- --%> + <%-- 2023-09-30 20:20--%> + <%-- --%> + <%--
--%> + <%--
--%> + <%--
--%> + <%--

스프링 입문을 위한 자바 객체 지향의 원리와 이해 추천합니다.

--%> + <%--

(근데 저도 아직 안 읽었어요..)

--%> + <%--
--%> + <%--
--%> + <%--
    --%> + <%--
  • --%> + <%-- --%> + <%-- 수정--%> + <%--
  • --%> + <%--
  • --%> + <%-- --%> + <%--
    --%> + <%-- --%> + <%-- --%> + <%--
    --%> + <%--
  • --%> + <%--
--%> + <%--
--%> + <%--
--%> + + +
+
+
+ +
+
+ + + ${answer.createdDate} + +
+
+
+

${answer.contents}

+
+
+
    +
  • + + 수정 +
  • +
  • + +
    + + +
    +
  • +
+
+
+
+
+
+
+ +
+ +
+ +
+
+
+
+
+
+ + + + + + \ No newline at end of file diff --git a/webapp/user/form.jsp b/src/main/webapp/user/form.jsp similarity index 100% rename from webapp/user/form.jsp rename to src/main/webapp/user/form.jsp diff --git a/webapp/user/list.jsp b/src/main/webapp/user/list.jsp similarity index 100% rename from webapp/user/list.jsp rename to src/main/webapp/user/list.jsp diff --git a/webapp/user/login.jsp b/src/main/webapp/user/login.jsp similarity index 100% rename from webapp/user/login.jsp rename to src/main/webapp/user/login.jsp diff --git a/webapp/user/loginFailed.jsp b/src/main/webapp/user/loginFailed.jsp similarity index 100% rename from webapp/user/loginFailed.jsp rename to src/main/webapp/user/loginFailed.jsp diff --git a/webapp/user/updateForm.jsp b/src/main/webapp/user/updateForm.jsp similarity index 100% rename from webapp/user/updateForm.jsp rename to src/main/webapp/user/updateForm.jsp diff --git a/webapp/home.jsp b/webapp/home.jsp deleted file mode 100644 index ac8aec8..0000000 --- a/webapp/home.jsp +++ /dev/null @@ -1,80 +0,0 @@ -<%@ page contentType="text/html;charset=UTF-8" language="java" %> -<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> - - - -<%@ include file="/include/header.jspf" %> - -<%@ include file="/include/navigation.jspf" %> - -
-

Q&A

-
- -
-
-
- -
- -
-
-
- - - - - \ No newline at end of file diff --git a/webapp/qna/form.jsp b/webapp/qna/form.jsp deleted file mode 100644 index c44ab1b..0000000 --- a/webapp/qna/form.jsp +++ /dev/null @@ -1,41 +0,0 @@ -<%@ page contentType="text/html;charset=UTF-8" language="java" %> -<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%> - - - -<%@ include file="/include/header.jspf"%> - -<%@ include file="/include/navigation.jspf" %> - -
- -
- -
-
- - -
-
- - -
-
- - -
-
- - -
-
-
- -
-
-
- - - - - \ No newline at end of file diff --git a/webapp/qna/show.jsp b/webapp/qna/show.jsp deleted file mode 100644 index 7e89769..0000000 --- a/webapp/qna/show.jsp +++ /dev/null @@ -1,137 +0,0 @@ -<%@ page contentType="text/html;charset=UTF-8" language="java" %> -<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%> - - - -<%@ include file="/include/header.jspf"%> - -<%@ include file="/include/navigation.jspf" %> - -
-
-

객체지향을 가장 잘 다룬 책이 뭐가 있나요?

-
-
-
-
-
- -
-
- - - - 2024-09-29 22:11 - -
-
-
-

저는 지금 '객체지향의 사실과 오해' 라는 책을 읽고 있는데 정말 좋아요

-

이해가 쏙쏙 됩니다!

-
-
- -
-
- -
-
-

2개의 의견

-
-
-
-
- -
-
- - - 2024-09-29 22:15 - -
-
-
-

오브젝트여

-
-
-
    -
  • - - 수정 -
  • -
  • - -
    - - -
    -
  • -
-
-
-
-
-
- -
-
- - - 2023-09-30 20:20 - -
-
-
-

스프링 입문을 위한 자바 객체 지향의 원리와 이해 추천합니다.

-

(근데 저도 아직 안 읽었어요..)

-
-
-
    -
  • - - 수정 -
  • -
  • - -
    - - -
    -
  • -
-
-
-
-
- -
- -
- -
-
-
-
-
-
- - - - - - \ No newline at end of file