Skip to content

Commit

Permalink
feat: change file
Browse files Browse the repository at this point in the history
  • Loading branch information
namhyeop committed Jul 24, 2022
1 parent 0936043 commit f071ed1
Show file tree
Hide file tree
Showing 27 changed files with 495 additions and 75 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ protected void service(HttpServletRequest request, HttpServletResponse response)
w.write(" <title>Title</title>");
w.write("</head>");
w.write("<body>");
w.write("<a href=\"/index.html\">메인</a>");
w.write("<a href=\"/index.jsp\">메인</a>");
w.write("<table>");
w.write(" <thead>");
w.write(" <th>id</th>");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ protected void service(HttpServletRequest request, HttpServletResponse response)
" <li>username="+member.getUsername()+"</li>\n" +
" <li>age="+member.getAge()+"</li>\n" +
"</ul>\n" +
"<a href=\"/index.html\">메인</a>\n" +
"<a href=\"/index.jsp\">메인</a>\n" +
"</body>\n" +
"</html>");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<title>Title</title>
</head>
<body>
<a href="/index.html">메인</a>
<a href="/index.jsp">메인</a>
<table>
<thead>
<th>id</th>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,6 @@
<%-- 이런것도 가능함, 그러나 더 불편--%>
<%-- <li<%=((member)request.getAttribute("member")).getAge()%></li>--%>
</ul>
<a href="/index.html">메인</a>
<a href="/index.jsp">메인</a>
</body>
</html>
2 changes: 1 addition & 1 deletion 3.spring-MVC1/1.servlet/src/main/webapp/jsp/members.jsp
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
<title>Title</title>
</head>
<body>
<a href="/index.html"> 메인</a>
<a href="/index.jsp"> 메인</a>
<table>
<thead>
<th>id</th>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,6 @@
<li>username=<%=member.getAge()%></li>
<li>age=<%=member.getAge()%></li>
</ul>
<a href="/index.html">메인</a>
<a href="/index.jsp">메인</a>
</body>
</html>
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import hello.itemservice.domain.item.Item;
import hello.itemservice.domain.item.ItemRepository;
import lombok.Data;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;
Expand All @@ -10,11 +11,13 @@
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.annotation.PostConstruct;
import java.io.Serializable;
import java.util.List;
import java.util.Map;

@Slf4j
@Controller
@RequestMapping("/basic/items")
//@RequestMapping("/basic/items")
@RequiredArgsConstructor
public class BasicItemController {
private final ItemRepository itemRepository;
Expand Down Expand Up @@ -142,4 +145,46 @@ public void init(){
itemRepository.save(new Item("itemA", 10000, 10));
itemRepository.save(new Item("itemB", 20000, 20));
}

// @ResponseBody
// @PostMapping("/orderpro123")
// public void test(@RequestBody TestDto testDto){
// System.out.println("testDto = " + testDto);
// List<Long> numbers = testDto.getNumbers();
// for (Long number : numbers) {
// System.out.println("number = " + number);
// }
// }

@ResponseBody
@GetMapping("/orderpro124")
public void test2(@RequestBody Map<String, List<String>> testDto){
System.out.println("testDto = " + testDto);
List<String> id = testDto.get("id");
List<String> password = testDto.get("password");
List<String> strings = testDto.get("email");
for (String string : strings) {
System.out.println("string = " + string);
}
// List<String> numbers = testDto.getNumbers();
// for (Object o : numbers) {
// System.out.println("o = " + o);
// }
}

@ResponseBody
@GetMapping("/orderpro125")
public void test2(@RequestParam TestDto testDto){
System.out.println("testDto = " + testDto);
}

@Data
static class TestDto implements Serializable {
String numbers;
}

@Data
static class TestDto2{
String abc;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Component;

/**
* WebServerFactoryCustomizer를 사용하게 되면 Spring의 기본 옵션인 BasciErrorController에서 처리하는 옵션대로 진행이 안된다.
* 에러 경로를 커스터 마이징하고 싶을때 사용하자
*/
//@Component
//errorPage를 등록하는 과정
public class WebServerCustomizer implements WebServerFactoryCustomizer<ConfigurableWebServerFactory> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
@RestControllerAdvice
public class ExControllerAdvice {
//IllegalArgumentException 발생시 예외 처리
//중요. 정상 흐름이 반환되기 때문에 이런 방식으로 응답할거면 응답 코드가 정상 200이 반환됨, 응답 코드도 400을 요청하고 싶으면 @ResponseStatus를 설정해야함
//200이 반환되는 이뉴는 ModelAndView로 객체를 반환하기때문에 정상 프로세스로 종료되기 때문이다.
@ResponseStatus(HttpStatus.BAD_REQUEST)
@ExceptionHandler(IllegalArgumentException.class)
public ErrorResult illegalExHandle(IllegalArgumentException e){
Expand All @@ -27,6 +29,8 @@ public ErrorResult illegalExHandle(IllegalArgumentException e){
//위와 다르게 동적으로 응답메시지를 바꿔서 반환이 가능함. 조건문 사용이 가능하기 때문에
//ExceptionHandler의 ()매개변수는 메소드의 매개변수로 생략이 가능함. 위의 illegalExhandle 메소드랑 비교해서 보면된다.
@ExceptionHandler
//위와 아래는 같다. 매개변수의 자료형이 기본값으로 설정되어 있다.
// @ExceptionHandler(UserException.class)
public ResponseEntity<ErrorResult> userExHandle(UserException e){
log.error("[exceptionHandle] ex", e);
ErrorResult errorResult = new ErrorResult("USER-EX", e.getMessage());
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
server.error.whitelabel.enabled=false
server.error.include-exception=true
server.error.include-message=always
#server.error.include-message=always
#server.error.include-stacktrace=always
#server.error.include-binding-errors=always
60 changes: 43 additions & 17 deletions 4.spring-MVC2/login/src/main/java/hello/login/WebConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

import hello.login.web.argumentresolver.LoginMemberArgumentResolver;
import hello.login.web.filter.LogFilter;
import hello.login.web.filter.LogFilter1;
import hello.login.web.filter.LogFilter2;
import hello.login.web.filter.LoginCheckFilter;
import hello.login.web.interceptor.LogInterceptor;
import hello.login.web.interceptor.LoginCheckInterceptor;
Expand All @@ -13,6 +15,7 @@
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import javax.servlet.DispatcherType;
import javax.servlet.Filter;
import javax.servlet.FilterRegistration;
import java.util.List;
Expand All @@ -25,32 +28,55 @@ public void addArgumentResolvers(List<HandlerMethodArgumentResolver> resolvers)
resolvers.add(new LoginMemberArgumentResolver());
}

@Override
public void addInterceptors(InterceptorRegistry registry){
//더할인터셉터, 순서, 응답패턴, 제외패턴순
registry.addInterceptor(new LogInterceptor())
.order(1)
.addPathPatterns("/**")
.excludePathPatterns("/css/**", "/*.ico", "/error");

registry.addInterceptor(new LoginCheckInterceptor())
.order(2)
.addPathPatterns("/**")
.excludePathPatterns("/", "/members/add", "/login", "/logout",
"/css/**", "/*.ico", "/error");
}
// @Override
// public void addInterceptors(InterceptorRegistry registry){
// //더할인터셉터, 순서, 응답패턴, 제외패턴순
// registry.addInterceptor(new LogInterceptor())
// .order(1)
// .addPathPatterns("/**")
// .excludePathPatterns("/css/**", "/*.ico", "/error");
//
// registry.addInterceptor(new LoginCheckInterceptor())
// .order(2)
// .addPathPatterns("/**")
// .excludePathPatterns("/", "/members/add", "/login", "/logout",
// "/css/**", "/*.ico", "/error");
// }

// @Bean
public FilterRegistrationBean logFilter(){
@Bean
public FilterRegistrationBean logFilter0(){
FilterRegistrationBean<Filter> filterRegistrationBean = new FilterRegistrationBean<>();
filterRegistrationBean.setFilter(new LogFilter());
filterRegistrationBean.setOrder(1);
// filterRegistrationBean.setDispatcherTypes(DispatcherType.REQUEST);
//모든 URL에서 작동하게 설정
filterRegistrationBean.addUrlPatterns("/*");
return filterRegistrationBean;
}

@Bean
public FilterRegistrationBean logFilter1(){
FilterRegistrationBean<Filter> filterRegistrationBean = new FilterRegistrationBean<>();
filterRegistrationBean.setFilter(new LogFilter1());
filterRegistrationBean.setOrder(2);
// filterRegistrationBean.setDispatcherTypes(DispatcherType.REQUEST, DispatcherType.ERROR);
//모든 URL에서 작동하게 설정
filterRegistrationBean.addUrlPatterns("/*");
return filterRegistrationBean;
}

@Bean
public FilterRegistrationBean logFilter2(){
FilterRegistrationBean<Filter> filterRegistrationBean = new FilterRegistrationBean<>();
filterRegistrationBean.setFilter(new LogFilter2());
filterRegistrationBean.setOrder(3);
// filterRegistrationBean.setDispatcherTypes(DispatcherType.REQUEST, DispatcherType.ERROR);
//모든 URL에서 작동하게 설정
filterRegistrationBean.addUrlPatterns("/*");
return filterRegistrationBean;
}

//@Bean
// @Bean
public FilterRegistrationBean loginCheckFilter(){
FilterRegistrationBean<Filter> filterRegistrationBean = new FilterRegistrationBean<>();
filterRegistrationBean.setFilter(new LoginCheckFilter());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
public class LoginMemberArgumentResolver implements HandlerMethodArgumentResolver {

//내부에 캐쉬가 있어서 최초 1회만 실행된다.
@Override
public boolean supportsParameter(MethodParameter parameter) {
log.info("supportsParameter 실행");
//Login 어노테이션이 붙어 있는지 확인
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public void init(FilterConfig filterConfig) throws ServletException {

@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
log.info("log filter doFilter");
log.info("log filter doFilter-0");
//ServletRequest는 HttpServletRequest의 부모인데 ServletRequest의 기능은 너무 적기 때문에 HttpServletRequest로 캐스팅 해줘야한다.
HttpServletRequest httpRequest = (HttpServletRequest) request;
String requestURI = httpRequest.getRequestURI();
Expand All @@ -26,18 +26,20 @@ public void doFilter(ServletRequest request, ServletResponse response, FilterCha
String uuid = UUID.randomUUID().toString();

try{
log.info("REQUEST [{}][{}]", uuid, requestURI);
log.info("REQUEST-0 [{}][{}]", uuid, requestURI);
//chain은 filter과정이 더 남아있으면 더 남은 과정으로 이동하고 아닐경우 servlet과정을 진행한다. 현재 스프링을 사용하므로 정확히는 DispatchServlet으로 넘어가서 mapping과정을 진행한다.
chain.doFilter(request, response);
}catch (Exception e){
throw e;
}finally {
log.info("RESPONSE [{}][{}]", uuid, requestURI);
}
finally {
log.info("RESPONSE-0 [{}][{}]", uuid, requestURI);
}
log.info("Survie Response-0! [{}][{}]", uuid, requestURI);
}

@Override
public void destroy() {
System.out.println("log filter destroy");
System.out.println("log filter destroy-0");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package hello.login.web.filter;

import lombok.extern.slf4j.Slf4j;

import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.UUID;

@Slf4j
public class LogFilter1 implements Filter {

@Override
public void init(FilterConfig filterConfig) throws ServletException {
log.info("log filter init-1");
}

@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
log.info("log filter doFilter1");
//ServletRequest는 HttpServletRequest의 부모인데 ServletRequest의 기능은 너무 적기 때문에 HttpServletRequest로 캐스팅 해줘야한다.
HttpServletRequest httpRequest = (HttpServletRequest) request;
String requestURI = httpRequest.getRequestURI();


String uuid = UUID.randomUUID().toString();

try{
log.info("REQUEST-1[{}][{}]", uuid, requestURI);
//chain은 filter과정이 더 남아있으면 더 남은 과정으로 이동하고 아닐경우 servlet과정을 진행한다. 현재 스프링을 사용하므로 정확히는 DispatchServlet으로 넘어가서 mapping과정을 진행한다.
chain.doFilter(request, response);
}catch (Exception e){
throw e;
}
finally {
log.info("RESPONSE1 [{}][{}]", uuid, requestURI);
}
log.info("Survie Response-1! [{}][{}]", uuid, requestURI);
}

@Override
public void destroy() {
System.out.println("log filter destroy-1");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package hello.login.web.filter;

import lombok.extern.slf4j.Slf4j;

import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.UUID;

@Slf4j
public class LogFilter2 implements Filter {

@Override
public void init(FilterConfig filterConfig) throws ServletException {
log.info("log filter init-2");
}

@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
log.info("log filter doFilter2");
//ServletRequest는 HttpServletRequest의 부모인데 ServletRequest의 기능은 너무 적기 때문에 HttpServletRequest로 캐스팅 해줘야한다.
HttpServletRequest httpRequest = (HttpServletRequest) request;
String requestURI = httpRequest.getRequestURI();


String uuid = UUID.randomUUID().toString();

try{
log.info("REQUEST-2[{}][{}]", uuid, requestURI);
//chain은 filter과정이 더 남아있으면 더 남은 과정으로 이동하고 아닐경우 servlet과정을 진행한다. 현재 스프링을 사용하므로 정확히는 DispatchServlet으로 넘어가서 mapping과정을 진행한다.
chain.doFilter(request, response);
}catch (Exception e){
throw e;
}
finally {
log.info("RESPONSE2 [{}][{}]", uuid, requestURI);
}
log.info("Survie Response-2! [{}][{}]", uuid, requestURI);
}

@Override
public void destroy() {
System.out.println("log filter destroy-2");
}
}
Loading

0 comments on commit f071ed1

Please sign in to comment.