View Javadoc

1   /*
2    * $Id: Submit.java 651946 2008-04-27 13:41:38Z apetrelli $
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  
22  package org.apache.struts2.components;
23  
24  import java.io.Writer;
25  
26  import javax.servlet.http.HttpServletRequest;
27  import javax.servlet.http.HttpServletResponse;
28  
29  import org.apache.struts2.views.annotations.StrutsTag;
30  import org.apache.struts2.views.annotations.StrutsTagAttribute;
31  
32  import com.opensymphony.xwork2.util.ValueStack;
33  import com.opensymphony.xwork2.util.logging.Logger;
34  import com.opensymphony.xwork2.util.logging.LoggerFactory;
35  
36  /***
37   * <!-- START SNIPPET: javadoc -->
38   * Render a submit button. The submit tag is used together with the form tag to provide asynchronous form submissions.
39   * The submit can have three different types of rendering:
40   * <ul>
41   * <li>input: renders as html &lt;input type="submit"...&gt;</li>
42   * <li>image: renders as html &lt;input type="image"...&gt;</li>
43   * <li>button: renders as html &lt;button type="submit"...&gt;</li>
44   * </ul>
45   * Please note that the button type has advantages by adding the possibility to seperate the submitted value from the
46   * text shown on the button face, but has issues with Microsoft Internet Explorer at least up to 6.0
47   * <!-- END SNIPPET: javadoc -->
48   */
49  @StrutsTag(
50      name="submit",
51      tldTagClass="org.apache.struts2.views.jsp.ui.SubmitTag",
52      description="Render a submit button",
53      allowDynamicAttributes=true)
54  public class Submit extends FormButton {
55      
56      private static final Logger LOG = LoggerFactory.getLogger(Submit.class);
57      final public static String TEMPLATE = "submit";
58      protected String src;
59  
60      public Submit(ValueStack stack, HttpServletRequest request, HttpServletResponse response) {
61          super(stack, request, response);
62      }
63  
64      protected String getDefaultTemplate() {
65          return TEMPLATE;
66      }
67  
68      public void evaluateParams() {
69          if ((key == null) && (value == null)) {
70              value = "Submit";
71          }
72  
73          if (((key != null)) && (value == null)) {
74              this.value = "%{getText('"+key +"')}";
75          }
76  
77          super.evaluateParams();
78      }
79      
80      public void evaluateExtraParams() {
81          super.evaluateExtraParams();
82          
83          if (src != null)
84              addParameter("src", findString(src));
85      }
86  
87      /***
88       * Indicate whether the concrete button supports the type "image".
89       *
90       * @return <tt>true</tt> to indicate type image is supported.
91       */
92      protected boolean supportsImageType() {
93          return true;
94      }
95      
96      @StrutsTagAttribute(description="Supply an image src for <i>image</i> type submit button. Will have no effect for types <i>input</i> and <i>button</i>.")
97      public void setSrc(String src) {
98          this.src = src;
99      }
100     
101     /***
102      * Overrides to be able to render body in a template rather than always before the template
103      */
104     public boolean end(Writer writer, String body) {
105         evaluateParams();
106         try {
107             addParameter("body", body);
108             
109             mergeTemplate(writer, buildTemplateName(template, getDefaultTemplate()));
110         } catch (Exception e) {
111             LOG.error("error when rendering", e);
112         }
113         finally {
114             popComponentStack();
115         }
116 
117         return false;
118     }
119 }