-
Notifications
You must be signed in to change notification settings - Fork 25
6주차 미션 / 서버 1조 이준용 #53
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from 13 commits
98b9d70
18c1f18
558ce02
afbac4e
18d56fb
978790f
024948b
ed766c8
aa19e29
9330d5f
699f385
dc2fd96
42fac50
45795f8
406a4dd
d4e1a8e
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
Large diffs are not rendered by default.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,22 +1,28 @@ | ||
| import org.apache.catalina.startup.Tomcat; | ||
| import org.springframework.boot.SpringApplication; | ||
| import org.springframework.boot.autoconfigure.SpringBootApplication; | ||
|
|
||
|
|
||
| import java.io.File; | ||
| import java.util.logging.Logger; | ||
|
|
||
| @SpringBootApplication | ||
| 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(); | ||
| SpringApplication.run(WebServerLauncher.class, args); | ||
|
|
||
| tomcat.addWebapp("", new File(webappDirLocation).getAbsolutePath()); | ||
| logger.info("configuring app with basedir: " + new File(webappDirLocation).getAbsolutePath()); | ||
|
|
||
| tomcat.start(); | ||
| tomcat.getServer().await(); | ||
| // 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(); | ||
| } | ||
coderabbitai[bot] marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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); | ||
| // } | ||
| // } | ||
| //} |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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<T> { | ||
| // | ||
| // 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<T> query(String sql, RowMapper<T> rowMapper) throws SQLException { | ||
| // List<T> 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<T> 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; | ||
| // } | ||
| // | ||
| //} |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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; | ||
| // } | ||
| //} |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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; | ||
| //} |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| //package core.jdbc; | ||
| // | ||
| //import jwp.model.User; | ||
| // | ||
| //import java.sql.ResultSet; | ||
| //import java.sql.SQLException; | ||
| // | ||
| //@FunctionalInterface | ||
| //public interface RowMapper<T> { | ||
| // T mapRow(ResultSet rs) throws SQLException; | ||
| // | ||
| //} |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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; | ||
| //} |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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:/"; | ||
coderabbitai[bot] marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
| @@ -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 "qna/form.jsp"; | |
| return "qna/form"; |
🤖 Prompt for AI Agents
In src/main/java/jwp/controller/CreateQuestionFormController.java around line
17, the controller returns a view name that includes the ".jsp" extension which
causes double-suffixing; change the returned view name to omit the ".jsp" suffix
(e.g. return "qna/form") so Spring's configured view suffix is applied
correctly.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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"; | ||
| } | ||
coderabbitai[bot] marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
MySQL 커넥터 의존성 버전을 명시하세요.
MySQL 커넥터에 버전이 명시되어 있지 않습니다. Spring Boot의 의존성 관리가 처리할 수 있지만,
mysql-connector-java는 deprecated되었으며mysql-connector-j로 대체되었습니다.다음과 같이 수정하는 것을 권장합니다:
📝 Committable suggestion
🤖 Prompt for AI Agents