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

complete MVC-config mission #31

Open
wants to merge 2 commits into
base: 24YoonSeojin-mvc-config
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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: 1 addition & 0 deletions .java-version
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
11.0.24
8 changes: 8 additions & 0 deletions bin/main/logback-access.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<configuration>
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>%n###### HTTP Request ######%n%fullRequest%n###### HTTP Response ######%n%fullResponse%n%n</pattern>
</encoder>
</appender>
<appender-ref ref="STDOUT" />
</configuration>
Binary file added bin/main/nextstep/helloworld/HelloApplication.class
Binary file not shown.
Binary file added bin/main/nextstep/helloworld/mvc/domain/User.class
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
10 changes: 10 additions & 0 deletions bin/main/templates/sample.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<!DOCTYPE HTML>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<title>Getting Started: Serving Web Content</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
</head>
<body>
<p th:text="'Hello, ' + ${name} + '!'" />
</body>
</html>
Binary file added bin/test/JUnitTest.class
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
87 changes: 86 additions & 1 deletion docs/README.md
Original file line number Diff line number Diff line change
@@ -1 +1,86 @@
# 요기다 정리
## View Controllers

뷰 컨트롤러는 화면 구성 요소들, 뷰를 관리하며 화면과 데이터 사이의 상호작용도 관리

컨트롤러 없이 특정 view에 대한 컨트롤러를 추가하는 방법

```java
@Configuration
@EnableWebMvc
public class WebConfig implements WebMvcConfigurer{

@Override
public void addViewControllers(ViewControllerRegistry registy){
registry.addViewController("/").setViewName("home");
```

```xml
<mvc:view-controller path="/" view-name="home"/>
```

위 코드는 같은 동작을 함

“/”라는 url이 요청되면 home이라는 view로 이동하도록 만들어 줌

만약 @RequestMapping 메서드가 http 메서드나 URL에 매핑되어 있을 경우 뷰 컨트롤러는 같은 URL에서 동작할 수 없음

## Interceptors

- interceptor란?

애플리케이션의 요청과 응답을 가로채고 처리하는 기능을 제공하는 인터페이스

일반적으로 로깅, 인증, 권한 부여와 같은 공통 작업을 행하는데 사용


```java
@Configuration
@EnableWebMvc
public class WebConfig implements WebMvcConfigurer{

@Override
public void addInterceptors(InterceptorRegistry registry){
registry.adddInterceptor(new LocaleChangeInterceptor());
registry.addInterceptor(new ThemeChangeInterceptor()).addPathPattern("/**").excludePathPattern("/admin/**");
}
}
```

- Interface HandlerInterceptor → LocalChangeInterceptor

특정한 URI 호출을 가로채는 역할, 기존 컨트롤러의 로직을 수정하지 않고 제어가 가능


## Custom Argument Resolver

### Argument Resolver란?

컨트롤러 메서드의 파라미터 중 조건에 맞는 파라미터를 요청에 의해 들어온 값을 이용해 원하는 객체를 만들어 바인딩해주는 역할을 수행

### HandlerMethodArgumentResolver 인터페이스

주어진 요청에 의해 메서드 파라미터를 매개변수로 연결하는 인터페이스

### Custom Argument Resolver를 사용하는 방법

- 타깃 파라미터를 위한 어노테이션 정의

```java
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.PARAMETER)
public @interface Version{ }
```

- @Retention: 라이프 사이클을 정의
- RetentionPolicy.RUNTIME: 런타임까지 생존
- RetentionPolicy.CLASS: 클래스 파일까지 생존
- RetentionPolicy.SOURCE: 소스 코드까지 생존
- @Target: 사용자가 만든 어노테이션이 부착될 수 있는 타입 지정
- Custom HandlerMethodArgumentResolver를 정의
- supportsParameter 메서드: 해당 파라미터가 타입인지 확인, 어노테이션을 가지고있는지 확인
- resolveArgument 메서드: 실제 바인딩할 객체를 만들어 리턴
- WebMvcConfigurer를 상속한 클래스의 addArgumentResolvers를 정의

HandlerMethodArgumentResolver의 리스트를 전달맏아 새롭게 정의한 custom argumentresolver를 추가

- Contoller에서 사용 가능
Original file line number Diff line number Diff line change
@@ -1,22 +1,30 @@
package nextstep.helloworld.mvcconfig.config;

import nextstep.helloworld.mvcconfig.ui.AuthenticationPrincipal;
import nextstep.helloworld.mvcconfig.ui.AuthenticationPrincipalArgumentResolver;
import nextstep.helloworld.mvcconfig.ui.LoginInterceptor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.method.support.HandlerMethodArgumentResolver;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import java.util.List;

@Configuration
@EnableWebMvc
public class WebMvcConfiguration implements WebMvcConfigurer {

/**
* https://docs.spring.io/spring-framework/docs/current/reference/html/web.html#mvc-config-view-controller
*
* "/" 요청 시 hello.html 페이지 응답하기
*/
@Override
public void addViewControllers(ViewControllerRegistry registry) {
registry.addViewController("/").setViewName("hello");
}

/**
Expand All @@ -26,6 +34,7 @@ public void addViewControllers(ViewControllerRegistry registry) {
*/
@Override
public void addInterceptors(InterceptorRegistry registry) {
registry.addInterceptor(new LoginInterceptor()).addPathPatterns("/admin/**");
}

/**
Expand All @@ -35,5 +44,6 @@ public void addInterceptors(InterceptorRegistry registry) {
*/
@Override
public void addArgumentResolvers(List<HandlerMethodArgumentResolver> resolvers) {
resolvers.add(new AuthenticationPrincipalArgumentResolver());
}
}