1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23 package org.apache.struts.tiles.taglib;
24
25 import java.io.IOException;
26
27 import javax.servlet.http.HttpServletRequest;
28 import javax.servlet.jsp.JspException;
29 import javax.servlet.jsp.PageContext;
30 import javax.servlet.jsp.tagext.TagSupport;
31
32 import org.apache.struts.tiles.ComponentContext;
33
34
35
36
37
38
39 public class GetAttributeTag extends TagSupport implements ComponentConstants {
40
41 private String attribute = null;
42
43 private String role = null;
44
45
46
47
48 private boolean isErrorIgnored = false;
49
50
51
52
53 public GetAttributeTag() {
54 super();
55 }
56
57
58
59
60 public void release() {
61
62 super.release();
63 attribute = null;
64 role = null;
65 isErrorIgnored = false;
66 }
67
68
69
70
71
72 public void setAttribute(String attribute){
73 this.attribute = attribute;
74 }
75
76
77
78
79
80 public String getAttribute()
81 {
82 return attribute;
83 }
84
85
86
87
88
89
90 public void setName(String value)
91 {
92 this.attribute = value;
93 }
94
95
96
97
98
99
100 public String getName()
101 {
102 return attribute;
103 }
104
105
106
107
108
109
110 public void setIgnore(boolean ignore)
111 {
112 this.isErrorIgnored = ignore;
113 }
114
115
116
117
118
119
120 public boolean getIgnore()
121 {
122 return isErrorIgnored;
123 }
124
125
126
127
128
129 public void setRole(String role) {
130 this.role = role;
131 }
132
133
134
135
136
137 public String getRole()
138 {
139 return role;
140 }
141
142
143
144
145
146 public int doEndTag() throws JspException {
147
148
149 if(role != null && !((HttpServletRequest)pageContext.getRequest()).isUserInRole(role) )
150 {
151 return EVAL_PAGE;
152 }
153
154
155 ComponentContext compContext = (ComponentContext)pageContext.getAttribute( ComponentConstants.COMPONENT_CONTEXT, PageContext.REQUEST_SCOPE);
156
157 if( compContext == null )
158 throw new JspException ( "Error - tag.getAsString : component context is not defined. Check tag syntax" );
159
160 Object value = compContext.getAttribute(attribute);
161 if( value == null)
162 {
163 if(isErrorIgnored == false )
164 throw new JspException ( "Error - tag.getAsString : attribute '"+ attribute + "' not found in context. Check tag syntax" );
165 else
166 return EVAL_PAGE;
167 }
168
169
170 try
171 {
172 pageContext.getOut().print( value );
173 }
174 catch( IOException ex )
175 {
176 ex.printStackTrace();
177 throw new JspException ( "Error - tag.getProperty : IOException ", ex);
178 }
179
180 return EVAL_PAGE;
181 }
182 }