Skip to content
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

[MVC 구현하기 3단계] 우가(윤정욱) 미션 제출합니다. #570

Merged
merged 4 commits into from
Sep 26, 2023
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ repositories {
dependencies {
implementation project(":mvc")

implementation "org.apache.tomcat.embed:tomcat-embed-core:10.1.13"
implementation "org.apache.tomcat.embed:tomcat-embed-jasper:10.1.13"
implementation "ch.qos.logback:logback-classic:1.2.12"
implementation "org.apache.commons:commons-lang3:3.13.0"
Expand Down
14 changes: 8 additions & 6 deletions app/src/main/java/com/techcourse/Application.java
Original file line number Diff line number Diff line change
@@ -1,21 +1,23 @@
package com.techcourse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;
import java.util.stream.Stream;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import webmvc.org.springframework.web.TomcatStarter;

public class Application {

private static final Logger log = LoggerFactory.getLogger(Application.class);

private static final int DEFAULT_PORT = 8080;

public static void main(final String[] args) throws Exception {
private static final String WEBAPP_DIR_LOCATION = "app/src/main/webapp/";

public static void main(final String[] args) {
final int port = defaultPortIfNull(args);
final var tomcat = new TomcatStarter(port);
log.info("configuring app with basedir: {}", TomcatStarter.WEBAPP_DIR_LOCATION);
final var tomcat = new TomcatStarter(WEBAPP_DIR_LOCATION, port);
log.info("configuring app with basedir: {}", WEBAPP_DIR_LOCATION);

tomcat.start();
stop(tomcat);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,11 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import web.org.springframework.web.WebApplicationInitializer;
import webmvc.org.springframework.web.servlet.DispatcherServlet;

/**
* Base class for {@link WebApplicationInitializer}
* implementations that register a {@link DispatcherServlet} in the servlet context.
* Base class for {@link WebApplicationInitializer} implementations that register a {@link DispatcherServlet} in the
* servlet context.
*/
public class DispatcherServletInitializer implements WebApplicationInitializer {

Expand All @@ -17,7 +18,7 @@ public class DispatcherServletInitializer implements WebApplicationInitializer {

@Override
public void onStartup(final ServletContext servletContext) {
final var dispatcherServlet = new DispatcherServlet();
final var dispatcherServlet = new DispatcherServlet(this.getClass().getPackageName());

final var registration = servletContext.addServlet(DEFAULT_SERVLET_NAME, dispatcherServlet);
if (registration == null) {
Expand Down
13 changes: 0 additions & 13 deletions app/src/main/java/com/techcourse/HandlerAdaptor.java

This file was deleted.

26 changes: 0 additions & 26 deletions app/src/main/java/com/techcourse/HandlerAdaptors.java

This file was deleted.

24 changes: 0 additions & 24 deletions app/src/main/java/com/techcourse/LegacyHandlerAdaptor.java

This file was deleted.

35 changes: 0 additions & 35 deletions app/src/main/java/com/techcourse/ManualHandlerMapping.java

This file was deleted.

30 changes: 26 additions & 4 deletions app/src/main/java/com/techcourse/controller/LoginController.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,23 @@

import com.techcourse.domain.User;
import com.techcourse.repository.InMemoryUserRepository;
import context.org.springframework.stereotype.Controller;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import webmvc.org.springframework.web.servlet.mvc.asis.Controller;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import web.org.springframework.web.bind.annotation.RequestMapping;
import web.org.springframework.web.bind.annotation.RequestMethod;
import webmvc.org.springframework.web.servlet.mvc.view.ModelAndView;
import webmvc.org.springframework.web.servlet.mvc.view.JspView;

public class LoginController implements Controller {
@Controller
public class LoginController {

private static final Logger log = LoggerFactory.getLogger(LoginController.class);

@Override
public String execute(final HttpServletRequest req, final HttpServletResponse res) throws Exception {
@RequestMapping(value = "/login", method = RequestMethod.POST)
public String login(final HttpServletRequest req, final HttpServletResponse res) {
if (UserSession.isLoggedIn(req.getSession())) {
return "redirect:/index.jsp";
}
Expand All @@ -26,6 +31,23 @@ public String execute(final HttpServletRequest req, final HttpServletResponse re
.orElse("redirect:/401.jsp");
}

@RequestMapping(value = "/login/view", method = RequestMethod.GET)
public ModelAndView loginView(final HttpServletRequest req, final HttpServletResponse res) {
return UserSession.getUserFrom(req.getSession())
.map(user -> {
log.info("logged in {}", user.getAccount());
return new ModelAndView(new JspView("redirect:/index.jsp"));
})
.orElse(new ModelAndView(new JspView("/login.jsp")));
}

@RequestMapping(value = "/logout", method = RequestMethod.POST)
public String logout(final HttpServletRequest req, final HttpServletResponse res) {
final var session = req.getSession();
session.removeAttribute(UserSession.SESSION_KEY);
return "redirect:/";
}

private String login(final HttpServletRequest request, final User user) {
if (user.checkPassword(request.getParameter("password"))) {
final var session = request.getSession();
Expand Down

This file was deleted.

15 changes: 0 additions & 15 deletions app/src/main/java/com/techcourse/controller/LogoutController.java

This file was deleted.

23 changes: 23 additions & 0 deletions app/src/main/java/com/techcourse/controller/PageController.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package com.techcourse.controller;

import context.org.springframework.stereotype.Controller;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import web.org.springframework.web.bind.annotation.RequestMapping;
import web.org.springframework.web.bind.annotation.RequestMethod;
import webmvc.org.springframework.web.servlet.mvc.view.ModelAndView;
import webmvc.org.springframework.web.servlet.mvc.view.JspView;

@Controller
public class PageController {

@RequestMapping(value = "/", method = RequestMethod.GET)
public ModelAndView index(final HttpServletRequest req, final HttpServletResponse res) {
return new ModelAndView(new JspView("/index.jsp"));
}

@RequestMapping(value = "/404", method = RequestMethod.GET)
public ModelAndView notFoundError(final HttpServletRequest req, final HttpServletResponse res) {
return new ModelAndView(new JspView("/404.jsp"));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,19 @@
import jakarta.servlet.http.HttpServletResponse;
import web.org.springframework.web.bind.annotation.RequestMapping;
import web.org.springframework.web.bind.annotation.RequestMethod;
import webmvc.org.springframework.web.servlet.ModelAndView;
import webmvc.org.springframework.web.servlet.view.JspView;
import webmvc.org.springframework.web.servlet.mvc.view.ModelAndView;
import webmvc.org.springframework.web.servlet.mvc.view.JspView;

@Controller
public class RegisterController {

@RequestMapping(value = "/register/view", method = RequestMethod.GET)
public ModelAndView show(final HttpServletRequest req, final HttpServletResponse res) throws Exception {
public ModelAndView show(final HttpServletRequest req, final HttpServletResponse res) {
return new ModelAndView(new JspView("/register.jsp"));
}

@RequestMapping(value = "/register", method = RequestMethod.POST)
public ModelAndView save(final HttpServletRequest req, final HttpServletResponse res) throws Exception {
public ModelAndView save(final HttpServletRequest req, final HttpServletResponse res) {
final var user = new User(2,
req.getParameter("account"),
req.getParameter("password"),
Expand Down
2 changes: 2 additions & 0 deletions mvc/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ repositories {
}

dependencies {
implementation "org.apache.tomcat.embed:tomcat-embed-core:10.1.13"

implementation "jakarta.servlet:jakarta.servlet-api:5.0.0"
implementation "javax.servlet.jsp:javax.servlet.jsp-api:2.3.3"
implementation "jakarta.servlet.jsp.jstl:jakarta.servlet.jsp.jstl-api:3.0.0"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,24 +1,18 @@
package com.techcourse;
package webmvc.org.springframework.web;

import java.io.File;
import org.apache.catalina.Context;
import org.apache.catalina.LifecycleException;
import org.apache.catalina.connector.Connector;
import org.apache.catalina.core.StandardContext;
import org.apache.catalina.startup.Tomcat;
import org.apache.tomcat.util.scan.StandardJarScanner;

import java.io.File;
import webmvc.org.springframework.web.servlet.mvc.UncheckedServletException;

public class TomcatStarter {

public static final String WEBAPP_DIR_LOCATION = "app/src/main/webapp/";

private final Tomcat tomcat;

public TomcatStarter(final int port) {
this(WEBAPP_DIR_LOCATION, port);
}

public TomcatStarter(final String webappDirLocation, final int port) {
this.tomcat = new Tomcat();
tomcat.setConnector(createConnector(port));
Expand Down
Loading
Loading