Interceptor로 상단메뉴 처리

JEONG SUJIN·2022년 12월 25일
0

com.demo.interceptor패키지 생성

public class MenuInterceptor implements HandlerInterceptor{

 private MenuService menuService;
 //생성자 주입
 public MenuInterceptor(MenuService menuService){
  this.menuService = menuService;
 }
 
 @Override
	public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
		
		List<BoardInfoBean> topMenuList = menuService.getMenuList();
		request.setAttribute("topMenuList", topMenuList);
		return true;
	}
 
}

ServletAppContext.java

@Override
	public void addInterceptors(InterceptorRegistry registry) {
		
		WebMvcConfigurer.super.addInterceptors(registry);
		
		MenuInterceptor menuInterceptor = new MenuInterceptor(menuService);
		
		InterceptorRegistration reg1 = registry.addInterceptor(menuInterceptor);		
		reg1.addPathPatterns("/**"); //모든 요청
	}
  <ul class="navbar-nav">
         <c:forEach var="menu" items="${topMenuList }">
         <li class="nav-item">
          <a href="${root }board/main?board_info_idx=${menu.board_info_idx }" class="nav-link">${menu.board_info_name }</a>
         </li>
      </c:forEach>
 </ul>
profile
기록하기

0개의 댓글