Dev/frontend

jstl custom tag 에서 spring bean 주입하기

Luigi.yoon 2017. 10. 25. 13:34

1. RequestContextAwareTag 상속받기

1
2
3
4
5
6
7
8
9
10
11
 
public class XXXTag extends RequestContextAwareTag {
    @Override
    protected int doStartTagInternal() throws Exception {
        // WebApplicationContext 를 얻는다.
        WebApplicationContext ctx = getRequestContext().getWebApplicationContext();
        // bean 주입
 
        return SKIP_BODY;
    }
cs





2. request 사용하기



1
2
3
4
5
6
7
8
9
10
11
 
 public class XXXTag extends SimpleTagSupport {
    @Override
    public void doTag() throws JspException, IOException {
        PageContext context = (PageContext) getJspContext();
        HttpServletRequest request = (HttpServletRequest) context.getRequest();
        ApplicationContext applicationContext = RequestContextUtils.findWebApplicationContext(request);
        // bean 주입 
    }
 
}
cs