View Javadoc

1   /*
2    * $Id: GenericUIBean.java 497654 2007-01-19 00:21:57Z rgielen $
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.components;
22  
23  import javax.servlet.http.HttpServletRequest;
24  import javax.servlet.http.HttpServletResponse;
25  
26  import org.apache.struts2.views.annotations.StrutsTag;
27  import org.apache.struts2.util.ContainUtil;
28  
29  import com.opensymphony.xwork2.util.ValueStack;
30  
31  /***
32   * <!-- START SNIPPET: javadoc -->
33   *
34   * Renders an custom UI widget using the specified templates. Additional objects can be passed in to the template
35   * using the param tags.<p/>
36   *
37   * <b>Freemarker:</b><p/>
38   * Objects provided can be retrieve from within the template via $parameters._paramname_.<p/>
39   *
40   * <b>Jsp:</b><p/>
41   * Objects provided can be retrieve from within the template via &lt;s:property value="%{parameters._paramname_}" /&gt;<p/>
42   *
43   *
44   * In the bottom JSP and Velocity samples, two parameters are being passed in to the component. From within the
45   * component, they can be accessed as:- <p/>
46   *
47   * <b>Freemarker:</b><p/>
48   * $parameters.get('key1') and $parameters.get('key2') or $parameters.key1 and $parameters.key2<p/>
49   *
50   * <b>Jsp:</b><p/>
51   * &lt;s:property value="%{parameters.key1}" /&gt; and &lt;s:property value="%{'parameters.key2'}" /&gt; or
52   * &lt;s:property value="%{parameters.get('key1')}" /&gt; and &lt;s:property value="%{parameters.get('key2')}" /&gt;<p/>
53   *
54   * Currently, your custom UI components can be written in Velocity, JSP, or Freemarker, and the correct rendering
55   * engine will be found based on file extension.<p/>
56   *
57   * <b>Remember:</b> the value params will always be resolved against the ValueStack so if you mean to pass a
58   * string literal to your component, make sure to wrap it in quotes i.e. value="'value1'" otherwise, the the value
59   * stack will search for an Object on the stack with a method of getValue1(). (now that i've written this, i'm not
60   * entirely sure this is the case. i should verify this manana)<p/>
61   *
62   * <!-- END SNIPPET: javadoc -->
63   *
64   * <p/> <b>Examples</b>
65   *
66   * <pre>
67   * <!-- START SNIPPET: example -->
68   * JSP
69   *     &lt;s:component template="/my/custom/component.vm"/&gt;
70   *
71   *       or
72   *
73   *     &lt;s:component template="/my/custom/component.vm"&gt;
74   *       &lt;s:param name="key1" value="value1"/&gt;
75   *       &lt;s:param name="key2" value="value2"/&gt;
76   *     &lt;/s:component&gt;
77   *
78   * Velocity
79   *     #s-component( "template=/my/custom/component.vm" )
80   *
81   *       or
82   *
83   *     #s-component( "template=/my/custom/component.vm" )
84   *       #s-param( "name=key1" "value=value1" )
85   *       #s-param( "name=key2" "value=value2" )
86   *     #end
87   *
88   * Freemarker
89   *    &lt;@s..component template="/my/custom/component.ftl" />
90   *
91   *      or
92   *
93   *    &lt;@s..component template="/my/custom/component.ftl"&gt;
94   *       &lt;@s..param name="key1" value="%{'value1'}" /&gt;
95   *       &lt;@s..param name="key2" value="%{'value2'}" /&gt;
96   *    &lt;/@s..component&gt;
97   *
98   * <!-- END SNIPPET: example -->
99   * </pre>
100  *
101  * <p/>
102  *
103  * <b>NOTE:</b>
104  * <!-- START SNIPPET: note -->
105  *
106  * If Jsp is used as the template, the jsp template itself must lie within the
107  * webapp itself and not the classpath. Unlike Freemarker or Velocity, JSP template
108  * could not be picked up from the classpath.
109  *
110  * <!-- END SNIPPET: note -->
111  *
112  */
113 @StrutsTag(name="component", tldTagClass="org.apache.struts2.views.jsp.ui.ComponentTag", description="Render a custom ui widget")
114 public class GenericUIBean extends UIBean {
115     private final static String TEMPLATE = "empty";
116 
117     public GenericUIBean(ValueStack stack, HttpServletRequest request, HttpServletResponse response) {
118         super(stack, request, response);
119     }
120 
121     public boolean contains(Object obj1, Object obj2) {
122         return ContainUtil.contains(obj1, obj2);
123     }
124 
125     protected String getDefaultTemplate() {
126         return TEMPLATE;
127     }
128 }