<Spring Bean을 이용한 세션관리>

1. web.xml 에 RequestContextListener 등록

2. Servlet-context.cml 에 bean 등록

3. Controller에서 사용

 <소스관리>

1. web.xml에 RequestContextListener 등록


<web-app>
・・・생략
    <listener>
        <listener-class>org.springframework.web.context.request.RequestContextListener</listener-class>
    </listener>
・・・생략
</web-app>



2. servlet-context.xml에 bean 등록


<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
    <!-- session bean -->
    <bean id="loginUserBean" class="sample.web.bean.common.LoginUserBean" scope="session">
        <aop:scoped-proxy/>
    </bean>
</beans>
 


 3. 컨트롤러에서 사용

@Controller
@RequetMapping(value="/hoge")
public class SampleController {    
    @Resource
    private LoginUserBean loginUserBean;
   
    @RequestMapping
    public ModelAndView hoge(WebRequest request) {
        loginUserBean.setName("admin");        
        ModelAndView mav = new ModelAndView("/fuga");
       
        // JSP내에서 사용하기 위해 Model에 등록
        model.addObject("loginUser", loginUserBean);
       
        return mav;
    }
}


 

+ Recent posts