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;
}
}
@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>