본문 바로가기
Back-end/Spring Framework

java spring 리다이렉트, 인터셉트

by javapp 자바앱 2020. 8. 12.
728x90

<리다이렉트>

컨트롤러에서 분기할 때

       // Remove

       @RequestMapping("/removeForm")

       public ModelAndView removeForm(HttpServletRequest request) {

            

             ModelAndView mav = new ModelAndView();

            

             HttpSession session =  request.getSession();

             Member member = (Member) session.getAttribute("member");

            

             if(member == null) {

                    mav.setViewName("redirect:/");          //메인페이지로 분기

             }else {

                    mav.addObject("member", member);

                    mav.setViewName("/member/removeForm");

             }

             return mav;

       }

 

 

 

<인터셉트>

분기할 리다이렉트가 많을 때

일괄적으로 관리

 

 

 

public class MemberLoginInterceptor extends HandlerInterceptorAdapter {

 

       //서블릿  이후 호출

       @Override

       public boolean preHandle(HttpServletRequest request,

             HttpServletResponse response, Object handler) throws Exception {

            

             HttpSession session = request.getSession(false);

             if(session != null) {

                    Object obj = session.getAttribute("member");

                    if(obj != null)

                           return true;

             }

            

             response.sendRedirect(request.getContextPath() + "/"); //루트페이지로

             return false;

       }

}

 

 

<servlet-context>

       <interceptors>

             <interceptor>

                    <mapping path="/member/modifyForm"/>

                    <mapping path="/member/removeForm"/>

                    <beans:bean class="com.bs.lec21.member.MemberLoginInterceptor"/>

             </interceptor>

       </interceptors>

 

 

2.

                    <!--

                    <mapping path="/member/**"/>

                    <exclude-mapping path="/member/joinForm"/>

                    <exclude-mapping path="/member/join"/>

                    <exclude-mapping path="/member/loginForm"/>

                    <exclude-mapping path="/member/login"/>

                    <exclude-mapping path="/member/logout"/>

                    <exclude-mapping path="/member/modify"/>

                    <exclude-mapping path="/member/remove"/>

                    -->

 

 

하나하나 리다렉트 하지 않아도 인터셉트에서 처리됨

댓글