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 com.opensymphony.xwork2.util.ValueStack;
25 import org.apache.struts2.components.Param.UnnamedParametric;
26 import org.apache.struts2.views.annotations.StrutsTag;
27 import org.apache.struts2.views.annotations.StrutsTagAttribute;
28
29 import javax.servlet.http.HttpServletRequest;
30 import javax.servlet.http.HttpServletResponse;
31 import java.util.ArrayList;
32 import java.util.List;
33
34 /***
35 * <!-- START SNIPPET: javadoc -->
36 *
37 * Render field errors if they exists. Specific layout depends on the particular theme.
38 * The field error strings will be html escaped by default.
39 *
40 * <!-- END SNIPPET: javadoc -->
41 *
42 * <p/> <b>Examples</b>
43 *
44 * <pre>
45 * <!-- START SNIPPET: example -->
46 *
47 * <!-- example 1 -->
48 * <s:fielderror />
49 *
50 * <!-- example 2 -->
51 * <s:fielderror>
52 * <s:param>field1</s:param>
53 * <s:param>field2</s:param>
54 * </s:fielderror>
55 * <s:form .... >
56 * ....
57 * </s:form>
58 *
59 * OR
60 *
61 * <s:fielderror>
62 * <s:param value="%{'field1'}" />
63 * <s:param value="%{'field2'}" />
64 * </s:fielderror>
65 * <s:form .... >
66 * ....
67 * </s:form>
68 *
69 * OR
70 *
71 * <s:fielderror fieldName="field1" />
72 *
73 * <!-- END SNIPPET: example -->
74 * </pre>
75 *
76 *
77 * <p/> <b>Description</b><p/>
78 *
79 *
80 * <pre>
81 * <!-- START SNIPPET: description -->
82 *
83 * Example 1: display all field errors<p/>
84 * Example 2: display field errors only for 'field1' and 'field2'<p/>
85 *
86 * <!-- END SNIPPET: description -->
87 * </pre>
88 *
89 */
90 @StrutsTag(name="fielderror", tldTagClass="org.apache.struts2.views.jsp.ui.FieldErrorTag", description="Render field error (all " +
91 "or partial depending on param tag nested)if they exists")
92 public class FieldError extends UIBean implements UnnamedParametric {
93
94 private List<String> errorFieldNames = new ArrayList<String>();
95 private boolean escape = true;
96
97 public FieldError(ValueStack stack, HttpServletRequest request, HttpServletResponse response) {
98 super(stack, request, response);
99 }
100
101 private static final String TEMPLATE = "fielderror";
102
103 protected String getDefaultTemplate() {
104 return TEMPLATE;
105 }
106
107 @Override
108 protected void evaluateExtraParams() {
109 super.evaluateExtraParams();
110
111 if (errorFieldNames != null)
112 addParameter("errorFieldNames", errorFieldNames);
113
114 addParameter("escape", escape);
115 }
116
117 public void addParameter(Object value) {
118 if (value != null) {
119 errorFieldNames.add(value.toString());
120 }
121 }
122
123 public List<String> getFieldErrorFieldNames() {
124 return errorFieldNames;
125 }
126
127 @StrutsTagAttribute(description="Field name for single field attribute usage", type="String")
128 public void setFieldName(String fieldName) {
129 addParameter(fieldName);
130 }
131
132 @StrutsTagAttribute(description=" Whether to escape HTML", type="Boolean", defaultValue="true")
133 public void setEscape(boolean escape) {
134 this.escape = escape;
135 }
136 }
137