View Javadoc

1   /*
2    * $Id: FormButton.java 768855 2009-04-27 02:09:35Z wesw $
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 javax.servlet.http.HttpServletRequest;
25  import javax.servlet.http.HttpServletResponse;
26  
27  import org.apache.struts2.views.annotations.StrutsTagAttribute;
28  import org.apache.struts2.dispatcher.mapper.ActionMapper;
29  import org.apache.struts2.dispatcher.mapper.ActionMapping;
30  
31  import com.opensymphony.xwork2.util.ValueStack;
32  import com.opensymphony.xwork2.inject.Inject;
33  
34  /***
35   * FormButton.
36   */
37  public abstract class FormButton extends ClosingUIBean {
38  
39      static final String BUTTONTYPE_INPUT = "input";
40      static final String BUTTONTYPE_BUTTON = "button";
41      static final String BUTTONTYPE_IMAGE = "image";
42  
43      protected String action;
44      protected String method;
45      protected String align;
46      protected String type;
47  
48      public FormButton(ValueStack stack, HttpServletRequest request, HttpServletResponse response) {
49          super(stack, request, response);
50      }
51  
52      //public void evaluateParams() {
53      public void evaluateExtraParams() {
54          super.evaluateExtraParams();
55          if (align == null) {
56              align = "right";
57          }
58  
59          String submitType = BUTTONTYPE_INPUT;
60          if (type != null && (BUTTONTYPE_BUTTON.equalsIgnoreCase(type) || (supportsImageType() && BUTTONTYPE_IMAGE.equalsIgnoreCase(type))))
61          {
62              submitType = type;
63          }
64  
65          //super.evaluateParams();
66  
67          addParameter("type", submitType);
68  
69          if (!BUTTONTYPE_INPUT.equals(submitType) && (label == null)) {
70              addParameter("label", getParameters().get("nameValue"));
71          }
72  
73          if (action != null || method != null) {
74              String name;
75  
76              if (action != null) {
77                  ActionMapping mapping = new ActionMapping();
78                  mapping.setName(findString(action));
79                  if (method != null) {
80                      mapping.setMethod(findString(method));
81                  }
82                  mapping.setExtension("");
83                  name = "action:" + actionMapper.getUriFromActionMapping(mapping);
84              } else {
85                  name = "method:" + findString(method);
86              }
87  
88              addParameter("name", name);
89          }
90  
91          addParameter("align", findString(align));
92  
93      }
94  
95      /***
96       * Override UIBean's implementation, such that component Html id is determined
97       * in the following order :-
98       * <ol>
99       *   <li>This component id attribute</li>
100      *   <li>[containing_form_id]_[this_component_name]</li>
101      *   <li>[containing_form_id]_[this_component_action]_[this_component_method]</li>
102      *   <li>[containing_form_id]_[this_component_method]</li>
103      *   <li>[this_component_name]</li>
104      *   <li>[this_component_action]_[this_component_method]</li>
105      *   <li>[this_component_method]</li>
106      *   <li>[an increasing sequential number unique to the form starting with 0]</li>
107      * </ol>
108      */
109     protected void populateComponentHtmlId(Form form) {
110         String _tmp_id = "";
111         if (id != null) {
112             // this check is needed for backwards compatibility with 2.1.x
113         	_tmp_id = findStringIfAltSyntax(id);
114         }
115         else {
116             if (form != null && form.getParameters().get("id") != null) {
117                 _tmp_id = _tmp_id + form.getParameters().get("id").toString() + "_";
118             }
119             if (name != null) {
120                 _tmp_id = _tmp_id + escape(name);
121             } else if (action != null || method != null){
122                 if (action != null) {
123                     _tmp_id = _tmp_id + escape(action);
124                 }
125                 if (method != null) {
126                     _tmp_id = _tmp_id + "_" + escape(method);
127                 }
128             } else {
129                 // if form is null, this component is used, without a form, i guess
130                 // there's not much we could do then.
131                 if (form != null) {
132                     _tmp_id = _tmp_id + form.getSequence();
133                 }
134             }
135         }
136         addParameter("id", _tmp_id);
137     }
138 
139     /***
140      * Indicate whether the concrete button supports the type "image".
141      *
142      * @return <tt>true</tt> if type image is supported.
143      */
144     protected abstract boolean supportsImageType();
145 
146     @Inject
147     public void setActionMapper(ActionMapper mapper) {
148         this.actionMapper = mapper;
149     }
150 
151     @StrutsTagAttribute(description="Set action attribute.")
152     public void setAction(String action) {
153         this.action = action;
154     }
155 
156     @StrutsTagAttribute(description="Set method attribute.")
157     public void setMethod(String method) {
158         this.method = method;
159     }
160 
161     @StrutsTagAttribute(description="HTML align attribute.")
162     public void setAlign(String align) {
163         this.align = align;
164     }
165 
166     @StrutsTagAttribute(description="The type of submit to use. Valid values are <i>input</i>, " +
167                 "<i>button</i> and <i>image</i>.", defaultValue="input")
168     public void setType(String type) {
169         this.type = type;
170     }
171 }