1 /*
2 * $Id: ComponentTagSupport.java 474191 2006-11-13 08:30:40Z mrdon $
3 *
4 * Licensed to the Apache Software Foundation (ASF) under one
5 * or more contributor license agreements. See the NOTICE file
6 * distributed with this work for additional information
7 * regarding copyright ownership. The ASF licenses this file
8 * to you under the Apache License, Version 2.0 (the
9 * "License"); you may not use this file except in compliance
10 * with the License. You may obtain a copy of the License at
11 *
12 * http://www.apache.org/licenses/LICENSE-2.0
13 *
14 * Unless required by applicable law or agreed to in writing,
15 * software distributed under the License is distributed on an
16 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17 * KIND, either express or implied. See the License for the
18 * specific language governing permissions and limitations
19 * under the License.
20 */
21 package org.apache.struts2.views.jsp;
22
23 import javax.servlet.http.HttpServletRequest;
24 import javax.servlet.http.HttpServletResponse;
25 import javax.servlet.jsp.JspException;
26
27 import org.apache.struts2.components.Component;
28 import org.apache.struts2.dispatcher.Dispatcher;
29 import org.apache.struts2.dispatcher.mapper.ActionMapper;
30
31 import com.opensymphony.xwork2.inject.Container;
32 import com.opensymphony.xwork2.inject.Inject;
33 import com.opensymphony.xwork2.util.ValueStack;
34
35 /**
36 */
37 public abstract class ComponentTagSupport extends StrutsBodyTagSupport {
38 protected Component component;
39
40 public abstract Component getBean(ValueStack stack, HttpServletRequest req, HttpServletResponse res);
41
42 public int doEndTag() throws JspException {
43 component.end(pageContext.getOut(), getBody());
44 component = null;
45 return EVAL_PAGE;
46 }
47
48 public int doStartTag() throws JspException {
49 component = getBean(getStack(), (HttpServletRequest) pageContext.getRequest(), (HttpServletResponse) pageContext.getResponse());
50 Container container = Dispatcher.getInstance().getContainer();
51 container.inject(component);
52
53 populateParams();
54 boolean evalBody = component.start(pageContext.getOut());
55
56 if (evalBody) {
57 return component.usesBody() ? EVAL_BODY_BUFFERED : EVAL_BODY_INCLUDE;
58 } else {
59 return SKIP_BODY;
60 }
61 }
62
63 protected void populateParams() {
64 component.setId(id);
65 }
66
67 public Component getComponent() {
68 return component;
69 }
70 }