View Javadoc
1   /**
2    * Copyright By Grandsoft Company Limited.  
3    * 2014年4月28日 上午8:42:45
4    */
5   package gboat2.base.view.components;
6   
7   import gboat2.base.bridge.util.BundleUtil;
8   import gboat2.base.plugin.struts.dispatcher.GboatDispatcherResult;
9   
10  import java.io.Writer;
11  
12  import javax.servlet.http.HttpServletRequest;
13  import javax.servlet.http.HttpServletResponse;
14  
15  import org.apache.struts2.ServletActionContext;
16  import org.apache.struts2.views.annotations.StrutsTag;
17  import org.osgi.framework.Bundle;
18  
19  import com.opensymphony.xwork2.ActionContext;
20  import com.opensymphony.xwork2.ActionInvocation;
21  import com.opensymphony.xwork2.Result;
22  import com.opensymphony.xwork2.util.ValueStack;
23  import com.opensymphony.xwork2.util.logging.Logger;
24  import com.opensymphony.xwork2.util.logging.LoggerFactory;
25  
26  /**
27   * <p>Include a servlet's output (result of servlet or a JSP page).</p>
28   * <p>Note: Any additional params supplied to the included page are <b>not</b>
29   * accessible within the rendered page through the &lt;g2:property...&gt; tag
30   * since no valuestack will be created. You can, however, access them in a
31   * servlet via the HttpServletRequest object or from a JSP page via
32   * a scriptlet.</p>
33   *
34   * <ul>
35   *      <li>value* (String) - jsp page to be included</li>
36   * </ul>
37   *
38   * <p/> <b>Examples</b>
39   * <pre>
40   * &lt;-- One: do an include myJsp.jsp page --&gt;
41   * &lt;g2:include value="myJsp.jsp" /&gt;
42   *
43   * &lt;-- Two: do an include to myJsp.jsp page with parameters param1=value1 and param2=value2 --&gt;
44   * &lt;g2:include value="myJsp.jsp"&gt;
45   *    &lt;g2:param name="param1" value="value2" /&gt;
46   *    &lt;g2:param name="param2" value="value2" /&gt;
47   * &lt;/g2:include&gt;
48   *
49   * &lt;-- Three: do an include to myJsp.jsp page with parameters param1=value1 and param2=value2 --&gt;
50   * &lt;g2:include value="myJsp.jsp"&gt;
51   *    &lt;g2:param name="param1"&gt;value1&lt;/g2:param&gt;
52   *    &lt;g2:param name="param2"&gt;value2&lt;/g2:param&gt;
53   * &lt;/g2:include&gt;
54   * </pre>
55   *
56   * @author <a href="mailto:[email protected]">何明旺</a>
57   * @since 3.0
58   * @date 2014年4月28日
59   */
60  @StrutsTag(
61          name="include",
62          tldTagClass="gboat2.base.view.jsp.IncludeTag",
63          description="Include a servlet's output (result of servlet or a JSP page)")
64  public class Include extends org.apache.struts2.components.Include {
65  
66      private static final Logger LOG = LoggerFactory.getLogger(Include.class);
67  
68      protected HttpServletRequest request;
69  
70      public Include(ValueStack stack, HttpServletRequest req, HttpServletResponse res) {
71          super(stack, req, res);
72          this.request = req;
73      }
74  
75      public boolean end(Writer writer, String body) {
76          String page = findString(value, "value", "You must specify the URL to include. Example: /foo.jsp");
77          page = getContextRelativePath(request, page);
78          
79          ActionContext actionContext = ServletActionContext.getContext();
80          ActionInvocation invocation = actionContext.getActionInvocation();
81          Bundle bundle = BundleUtil.getBundleForRequestJsp(page, invocation);
82          
83          
84          if(bundle != null && !page.startsWith("/" + bundle.getSymbolicName() + "/")) {
85              page = "/" + bundle.getSymbolicName() + page;
86          }
87          
88          if(page.indexOf("glodon.gbmp.index") > 0){
89          	page = "/"  + page;
90          }
91          try {
92              Result result = invocation.getResult();
93              if(result instanceof GboatDispatcherResult) {
94                  // 如果返回结果为 GboatDispatcherResult, 需要将 JSP 从 Bundle 中复制到 webapp 目录下
95                  page = GboatDispatcherResult.extractBundleJsp(page);
96              }
97          } catch (Exception e) {
98              LOG.error("获取请求 [#0] 的 Result 失败。", e, invocation.toString());
99          }
100         setValue(page);
101         return super.end(writer, body);
102     }
103 
104 }