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