1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.apache.struts2.components;
19
20 import javax.servlet.http.HttpServletRequest;
21 import javax.servlet.http.HttpServletResponse;
22
23 import com.opensymphony.xwork2.util.ValueStack;
24
25 /***
26 * FormButton.
27 */
28 public abstract class FormButton extends UIBean {
29
30 static final String BUTTONTYPE_INPUT = "input";
31 static final String BUTTONTYPE_BUTTON = "button";
32 static final String BUTTONTYPE_IMAGE = "image";
33
34 protected String action;
35 protected String method;
36 protected String align;
37 protected String type;
38
39 public FormButton(ValueStack stack, HttpServletRequest request, HttpServletResponse response) {
40 super(stack, request, response);
41 }
42
43
44 public void evaluateExtraParams() {
45 super.evaluateExtraParams();
46 if (align == null) {
47 align = "right";
48 }
49
50 String submitType = BUTTONTYPE_INPUT;
51 if (type != null && (BUTTONTYPE_BUTTON.equalsIgnoreCase(type) || (supportsImageType() && BUTTONTYPE_IMAGE.equalsIgnoreCase(type))))
52 {
53 submitType = type;
54 }
55
56
57
58 addParameter("type", submitType);
59
60 if (!BUTTONTYPE_INPUT.equals(submitType) && (label == null)) {
61 addParameter("label", getParameters().get("nameValue"));
62 }
63
64 if (action != null || method != null) {
65 String name;
66
67 if (action != null) {
68 name = "action:" + findString(action);
69
70 if (method != null) {
71 name += "!" + findString(method);
72 }
73 } else {
74 name = "method:" + findString(method);
75 }
76
77 addParameter("name", name);
78 }
79
80 addParameter("align", findString(align));
81
82 }
83
84 /***
85 * Override UIBean's implementation, such that component Html id is determined
86 * in the following order :-
87 * <ol>
88 * <li>This component id attribute</li>
89 * <li>[containing_form_id]_[this_component_name]</li>
90 * <li>[containing_form_id]_[this_component_action]_[this_component_method]</li>
91 * <li>[containing_form_id]_[this_component_method]</li>
92 * <li>[this_component_name]</li>
93 * <li>[this_component_action]_[this_component_method]</li>
94 * <li>[this_component_method]</li>
95 * <li>[an increasing sequential number unique to the form starting with 0]</li>
96 * </ol>
97 */
98 protected void populateComponentHtmlId(Form form) {
99 String _tmp_id = "";
100 if (id != null) {
101
102 if (altSyntax()) {
103 _tmp_id = findString(id);
104 } else {
105 _tmp_id = id;
106 }
107 }
108 else {
109 if (form != null && form.getParameters().get("id") != null) {
110 _tmp_id = _tmp_id + form.getParameters().get("id").toString() + "_";
111 }
112 if (name != null) {
113 _tmp_id = _tmp_id + escape(name);
114 } else if (action != null || method != null){
115 if (action != null) {
116 _tmp_id = _tmp_id + escape(action);
117 }
118 if (method != null) {
119 _tmp_id = _tmp_id + "_" + escape(method);
120 }
121 } else {
122
123
124 if (form != null) {
125 _tmp_id = _tmp_id + form.getSequence();
126 }
127 }
128 }
129 addParameter("id", _tmp_id);
130 }
131
132 /***
133 * Indicate whether the concrete button supports the type "image".
134 *
135 * @return <tt>true</tt> if type image is supported.
136 */
137 protected abstract boolean supportsImageType();
138
139 /***
140 * Set action attribute.
141 *
142 * @s.tagattribute required="false" type="String"
143 */
144 public void setAction(String action) {
145 this.action = action;
146 }
147
148 /***
149 * Set method attribute.
150 *
151 * @s.tagattribute required="false" type="String"
152 */
153 public void setMethod(String method) {
154 this.method = method;
155 }
156
157 /***
158 * HTML align attribute.
159 *
160 * @s.tagattribute required="false" type="String"
161 */
162 public void setAlign(String align) {
163 this.align = align;
164 }
165
166 /***
167 * The type of submit to use. Valid values are <i>input</i>, <i>button</i> and <i>image</i>.
168 *
169 * @s.tagattribute required="false" type="String" default="input"
170 */
171 public void setType(String type) {
172 this.type = type;
173 }
174 }