<리다이렉트>
컨트롤러에서 분기할 때
// 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"/>
-->
하나하나 리다렉트 하지 않아도 인터셉트에서 처리됨
'Back-end > Spring Framework' 카테고리의 다른 글
spring lagacy project / 생성, 설정, 오류 처리, maven, mybatis | 히카리 cp (0) | 2022.05.06 |
---|---|
java spring 커넥션 풀 , jdbcTemplate (0) | 2020.08.13 |
java spring 세션 & 쿠키 (0) | 2020.08.10 |
java spring , Controller 설정팁 (0) | 2020.08.09 |
java spring , controller & dao & service 객체 (0) | 2020.08.08 |
댓글