View Javadoc

1   /*
2    * $Id: Head.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.views.annotations.StrutsTagAttribute;
28  import org.apache.struts2.StrutsConstants;
29  
30  import com.opensymphony.xwork2.inject.Inject;
31  import com.opensymphony.xwork2.util.ValueStack;
32  
33  /***
34   * <!-- START SNIPPET: javadoc -->
35   *
36   * Renders parts of the HEAD section for an HTML file. This is useful as some themes require certain CSS and JavaScript
37   * includes.<p/>
38   *
39   * If, for example, your page has ajax components integrated, without having the default theme set to ajax, you might
40   * want to use the head tag with <b>theme="ajax"</b> so that the typical ajax header setup will be included in the
41   * page.<p/>
42   *
43   * The tag also includes the option to set a custom datepicker theme if needed. See calendarcss parameter for
44   * description for details.<p/>
45   *
46   * If you use the ajax theme you can turn a debug flag on by setting the debug parameter to <tt>true</tt>.
47   *
48   * <!-- END SNIPPET: javadoc -->
49   *
50   * <p/> <b>Examples</b>
51   *
52   * <pre>
53   * <!-- START SNIPPET: example1 -->
54   * &lt;head&gt;
55   *   &lt;title&gt;My page&lt;/title&gt;
56   *   &lt;s:head/&gt;
57   * &lt;/head&gt;
58   * <!-- END SNIPPET: example1 -->
59   * </pre>
60   *
61   * <pre>
62   * <!-- START SNIPPET: example2 -->
63   * &lt;head&gt;
64   *   &lt;title&gt;My page&lt;/title&gt;
65   *   &lt;s:head theme="ajax" calendarcss="calendar-green"/&gt;
66   * &lt;/head&gt;
67   * <!-- END SNIPPET: example2 -->
68   * </pre>
69   *
70   * <pre>
71   * <!-- START SNIPPET: example3 -->
72   * &lt;head&gt;
73   *   &lt;title&gt;My page&lt;/title&gt;
74   *   &lt;s:head theme="ajax" debug="true"/&gt;
75   * &lt;/head&gt;
76   * <!-- END SNIPPET: example3 -->
77   * </pre>
78   *
79   */
80  @StrutsTag(name="head", tldBodyContent="empty", tldTagClass="org.apache.struts2.views.jsp.ui.HeadTag",
81      description="Render a chunk of HEAD for your HTML file")
82  public class Head extends UIBean {
83      public static final String TEMPLATE = "head";
84  
85      private String calendarcss = "calendar-blue.css";
86      private boolean debug;
87      private String encoding;
88  
89      public Head(ValueStack stack, HttpServletRequest request, HttpServletResponse response) {
90          super(stack, request, response);
91      }
92  
93      protected String getDefaultTemplate() {
94          return TEMPLATE;
95      }
96  
97      @Inject(StrutsConstants.STRUTS_I18N_ENCODING)
98      public void setEncoding(String encoding) {
99          this.encoding = encoding;
100     }
101 
102     public void evaluateParams() {
103         super.evaluateParams();
104 
105         if (calendarcss != null) {
106             String css = findString(calendarcss);
107             if (css != null && css.trim().length() > 0) {
108                 if (css.lastIndexOf(".css") < 0) {
109                     addParameter("calendarcss", css + ".css");
110                 } else {
111                     addParameter("calendarcss", css);
112                 }
113             }
114         }
115 
116         addParameter("encoding", encoding);
117         addParameter("debug", Boolean.valueOf(debug).toString());
118     }
119 
120     public String getCalendarcss() {
121         return calendarcss;
122     }
123 
124     @StrutsTagAttribute(description="The jscalendar css theme to use", defaultValue="calendar-blue.css")
125     public void setCalendarcss(String calendarcss) {
126         //TODO remove this one
127         this.calendarcss = calendarcss;
128     }
129 
130     public boolean isDebug() {
131         return debug;
132     }
133 
134     @StrutsTagAttribute(description="Set to true to enable debugging mode for AJAX themes")
135     public void setDebug(boolean debug) {
136         this.debug = debug;
137     }
138 
139 
140 }