empire-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From doeb...@apache.org
Subject svn commit: r889640 - /incubator/empire-db/site/struts2ext/forms_controls.htm
Date Fri, 11 Dec 2009 14:35:15 GMT
Author: doebele
Date: Fri Dec 11 14:35:15 2009
New Revision: 889640

URL: http://svn.apache.org/viewvc?rev=889640&view=rev
Log:
Wrong file content - replaced with correct content.

Modified:
    incubator/empire-db/site/struts2ext/forms_controls.htm

Modified: incubator/empire-db/site/struts2ext/forms_controls.htm
URL: http://svn.apache.org/viewvc/incubator/empire-db/site/struts2ext/forms_controls.htm?rev=889640&r1=889639&r2=889640&view=diff
==============================================================================
--- incubator/empire-db/site/struts2ext/forms_controls.htm (original)
+++ incubator/empire-db/site/struts2ext/forms_controls.htm Fri Dec 11 14:35:15 2009
@@ -147,107 +147,174 @@
 			
 			<div id="content">
 			<!-- #BeginEditable "content" -->
-			<h1>Input controls part 2 – Test</h1>
-			<p> One of the biggest problems in Web development is that usually the code responsible

-				for rendering data is separated from the code used for parsing and validating data. 
-				In most cases the rendering is performed in a JSP page, either by directly embedding
string 
-				representation of the value in HTML or by a special JSP tag that renders the value 
-				according to information provided with attributes on the tag. When the user submits the

-				form the values are usually pushed into a JavaBean or POJO by an interceptor which takes
the 
-				parameters from the request, looks for an appropriate setter method and then converts

-				the value to fit the setter’s value data type.</p>
-			<p> This procedure has several disadvantages that are best understood by the following
example:<br />
-				Consider a phone number which is stored in the database in a single field. On the form
however 
-				you want to display and edit the phone number in three individual text fields: the first
for the 
-				country code, the second for the city code and the last for the extension code which
are separated 
-				from each other using a dash. The first problem with traditional Web application frameworks
is, 
-				that since there is no tag for such behaviour you have to write all the HTML code for
those three 
-				separate input tags plus the label in your JSP. Even worse however is, that when the
form is 
-				submitted three individual fields are supplied that you have to assemble to a single
value again. 
-				Now for this you have several options which are all quite complicated for such a simple
task. If you 
-				don’t want to provide your own interceptor you will have to provide corresponding
setters on your JavaBean and – if you don’t want to rely on the order by which these
setters are called -  you’d 
-				have to add additional post processing code that will put them together again. Still
the assembly 
-				code must rely on the rendering code which is in the JSP and thus not in the same place.
As a third 
-				problem you’ll find the task of validation – for which you might have to write
your own validation 
-				class. At last, all this has to be done for every single phone number field you have
in your 
-				application. Imagine you have several phone number fields then handling 
-			all of them becomes quite a nasty 
-				problem.</p>
-			<p> With Empire-Struts2-Extensions the task of rendering and the task of parsing
and validating form 
-				values are now put together in one simple class: The input control. The input control
is an abstract 
-				base class for which you can easily provide your own implementation if necessary by only
implementing 
-				three simple methods: <code>renderText()</code>, <code>renderInput()</code>
and <code>getFieldValue()</code>. 
-				For the most common field types there are already implementations provided such as text,
select, password, phone, checkbox and more. 
-				The major differences to traditional processing are:</p>
+			<h1>Input controls part 1 – Forms and controls</h1>
+			<p> For database driven Web applications one of the most common tasks is displaying

+				a form for adding new and modifying existing records. To demonstrate how much safer,

+				cleaner and more efficient you can work using the Empire-Struts2-Extensions we 
+				compare it to standard Struts2.</p>
+			<p> Let’s first look at the attributes that are required to define a form input
control:</p>
 			<ol>
-				<li>The input control has access to field metadata and can thus perform rendering,
parsing and 
-					validation without additional redundant information provided in the view. However should
you ever 
-					need it, you may as well override these attributes in your JSPs.</li>
-				<li>The input control class distinguishes between read only and editable rendering
and can thus also 
-					be used for list views for example. Consider a field whose value you have 
-				to format in a 
-					particular way for display. Simply write your own input control class and assign the
control 
-					type to the corresponding column. Wherever a value for this field is displayed with
any of the Empire tags 
-					it will be formatted correctly – without any change or additional 
-				attributes in the JSPs. </li>
-				<li>For processing submitted form data the input control uses a pull rather than
the usual push 
-					approach. This allows the input control to combine two or more fields to one value.
This 
-					is e.g. used for checkboxes where an additional hidden value is used to detect whether
the 
-					checkbox has been unchecked. Since the rendering code is in the same object both the
splitting 
-					and the assembly can easily be kept consistent.</li>
+				<li>The type of input (text, selection list, checkbox)</li>
+				<li>The maximum input length for text fields</li>
+				<li>The list of possible values for selection lists</li>
+				<li>Whether or not the field is mandatory</li>
+				<li>Whether or not the field value is editable or read only</li>
 			</ol>
-			<p>The following graphic shows how the InputControl is the interface 
-			between the view and the model:</p>
-			<img style="display: block; margin: auto;" src="../images/diagrams/function_of_input_control.gif"
alt="" />
+			<p> While points 1 to 4 are usually defined through static attributes of a particular

+				field, the field’s accessibility (point 5) is more likely to depend on the context,

+				such as the record’s state or user credentials – and thus has more complex
requirements. 
+				This point is further discussed in the section <a href="#access_logic">Implementing
field level access logic</a>.</p>
+			<p> The definition of a full featured input control with standard Struts2 tags looks
somewhat like this:</p>			
+			<pre><span style="color: #0000FF; ">&lt;</span><span style="color:
#800000; ">s:textfield </span><span style="color: #FF0000; ">key</span><span
style="color: #0000FF; ">=&quot;employee.lastname&quot;</span><span style="color:
#FF0000; "> 
+             size</span><span style="color: #0000FF; ">=&quot;40&quot;</span><span
style="color: #FF0000; "> 
+             required</span><span style="color: #0000FF; ">=&quot;true&quot;</span><span
style="color: #FF0000; "> 
+             disabled</span><span style="color: #0000FF; ">=&quot;!hasAccess('employee.lastname')&quot;</span><span
style="color: #FF0000; "> </span><span style="color: #0000FF; ">/&gt;</span></pre>
 			
-			<p>The list view JSP uses the <code>&lt;e:value&gt;</code> tag
(or alternatively an <code>&lt;e:td&gt;</code> 
-			tag which extends <code>&lt;e:value&gt;</code>) to render the field&#39;s
value for 
-			display. The form view on contrary uses the <code>&lt;e:control&gt;</code>
tag to 
-			render a form input control which may be used to change the value. 
-			When the form is submitted, an action support class iterates through 
-			all fields and gives the corresponding InputControl the opportunity 
-			to pull the value(s) off the request and to perform parsing and 
-			validation. If successful the field value is written back to the 
-			record. For all three tasks access to data and metadata is 
-			necessary.</p>
-
-			<h1 id="access_logic">Writing a custom input control</h1>
-			<p> The following example shows, how easily a custom input control 
-			type 
-			can be created and applied.</p>
-			<h2> Step 1: Write your input control class. </h2>
-			<p> Create a new class, derive it from InputControl and provide an implementation

-			for the 
-			three methods <code>renderText()</code>, <code>renderInput()</code>
and <code>getFieldValue()</code>. 
-			Remember that your class must be fully stateless since one single instance is 
-			shared for all fields and threads. However all information you 
-			require 
-			is provided with the parameters. Here is an outline of what your 
-			class may look like:</p>	
-<pre><font color="#0000FF">public class </font>CustomInputControl <font
color="#0000FF">extends</font> InputControl { 
-<font color="#404040">    @Override 
-</font>    <font color="#0000FF">public</font> Object getFieldValue(String
name, RequestParamProvider request, Locale locale, DBColumn column) { … } 
-<font color="#404040">    @Override 
-</font>    <font color="#0000FF">public</font> void renderText(HtmlWriter
writer, ValueInfo vi) { … } 
-<font color="#404040">    @Override 
-</font>    <font color="#0000FF">public</font> void renderInput(HtmlWriter
writer, ControlInfo ci) { … } 
-}</pre>    							
-			<h2> Step 2: Register your class with the InputControlMananger. </h2>
-			<p> To make the Empire-Struts2-Extensions capable of using your 
-			input control class you must register it with the input control 
-			manager as follows:</p>	
-<pre><font color="#008000">// Register Control</font>
-InputControlManager.registerControl(<font color="#0000FF">&quot;custom</font><font
color="#0000FF">&quot;</font>, <font color="#0000FF">new</font> CustomInputControl());</pre>
   							
-			<h2> Step 3: Assign your input control type to database columns</h2>
-			<p> Finally, assign your control type to database columns. To do 
-			this, use the <code>setControlType()</code> function which is provided with
the 
-			DBColumn 
-			objects. For those fields, your custom input control class will now 
-			be used for rendering and form request processing.</p>	
-<pre>MyDatabase db = getDatabase();
-db.<font color="#0000FF">MYTABLE</font>.<font color="#0000FF">MYCOLUMN</font>.setControlType(<font
color="#0000FF">&quot;custom</font><font color="#0000FF">&quot;</font>);</pre>
   							
-			<p>	&nbsp;</p>
-								
+			<p> With Empire-Struts2-Extensions you’d simply write:</p>
+			<pre><span style="color: #0000FF; ">&lt;</span><span style="color:
#800000; ">e:control </span><span style="color: #FF0000; ">column</span><span
style="color: #0000FF; ">=&quot;&lt;%= EMPLOYEES.LASTNAME %&gt;&quot;</span><span
style="color: #FF0000; "> </span><span style="color: #0000FF; ">/&gt;</span></pre>
+			
+			<p> The same applies to a field that is displayed as a selection list. With traditional
Struts2 tags 
+				it may look like this:</p>
+			<pre><span style="color: #0000FF; ">&lt;</span><span style="color:
#800000; ">s:select </span><span style="color: #FF0000; ">key</span><span
style="color: #0000FF; ">=&quot;employee.gender&quot;</span><span style="color:
#FF0000; ">
+          list</span><span style="color: #0000FF; ">=&quot;genders&quot;</span><span
style="color: #FF0000; "> listKey</span><span style="color: #0000FF; ">=&quot;key&quot;</span><span
style="color: #FF0000; "> listValue</span><span style="color: #0000FF; ">=&quot;value&quot;</span><span
style="color: #FF0000; ">
+          required</span><span style="color: #0000FF; ">=&quot;true&quot;</span><span
style="color: #FF0000; ">
+          disabled</span><span style="color: #0000FF; ">=&quot;!hasAccess('employee.department')&quot;</span><span
style="color: #FF0000; "> </span><span style="color: #0000FF; ">/&gt;</span></pre>
+          
+          	<p> Again with Empire-Struts2-Extensions the code is simply:</p>
+          	<pre><span style="color: #0000FF; ">&lt;</span><span
style="color: #800000; ">e:control </span><span style="color: #FF0000; ">column</span><span
style="color: #0000FF; ">=&quot;&lt;%= EMPLOYEES.DEPARTMENT_ID %&gt;&quot;</span><span
style="color: #FF0000; "> </span><span style="color: #0000FF; ">/&gt;</span></pre>
+          	
+          	<p> All the information necessary to set all input control 
+			attributes is provided by the meta information 
+          		supplied by the record and column objects. The following code shows the corresponding
column 
+          		definition for the two fields with extended view specific metadata:</p>
+          	<pre><span style="color: #0000FF; ">public</span><span style="color:
#000000; "> </span><span style="color: #0000FF; ">static</span><span
style="color: #000000; "> </span><span style="color: #0000FF; ">class</span><span
style="color: #000000; "> Employees </span><span style="color: #0000FF; ">extends</span><span
style="color: #000000; "> DBTable
+{
+    </span><span style="color: #0000FF; ">public</span><span style="color:
#000000; "> </span><span style="color: #0000FF; ">final</span><span
style="color: #000000; "> DBTableColumn LASTNAME;
+    </span><span style="color: #0000FF; ">public</span><span style="color:
#000000; "> </span><span style="color: #0000FF; ">final</span><span
style="color: #000000; "> DBTableColumn GENDER;
+    
+    </span><span style="color: #0000FF; ">public</span><span style="color:
#000000; "> Employees(DBDatabase db)
+    {
+        </span><span style="color: #0000FF; ">super</span><span style="color:
#000000; ">(</span><span style="color: #000000; ">&quot;</span><span
style="color: #000000; ">EMPLOYEES</span><span style="color: #000000; ">&quot;</span><span
style="color: #000000; ">, db);
+ </span><span style="color: #000000; ">       </span><span style="color:
#008000; ">// Add columns
+</span><span style="color: #000000; ">        LASTNAME </span><span
style="color: #000000; ">=</span><span style="color: #000000; "> addColumn(</span><span
style="color: #000000; ">&quot;</span><span style="color: #000000; ">LASTNAME</span><span
style="color: #000000; ">&quot;</span><span style="color: #000000; ">,
DataType.TEXT, </span><span style="color: #000000; ">40</span><span style="color:
#000000; ">, </span><span style="color: #0000FF; ">true</span><span
style="color: #000000; ">);
+        GENDER   </span><span style="color: #000000; ">=</span><span
style="color: #000000; "> addColumn(</span><span style="color: #000000; ">&quot;</span><span
style="color: #000000; ">GENDER</span><span style="color: #000000; ">&quot;</span><span
style="color: #000000; ">,   DataType.TEXT,  </span><span style="color: #000000;
">1</span><span style="color: #000000; ">, </span><span style="color:
#0000FF; ">false</span><span style="color: #000000; ">);
+ </span><span style="color: #000000; ">       </span><span style="color:
#008000; ">// Set control types
+ </span><span style="color: #000000; ">       LASTNAME.setControlType(</span><span
style="color: #000000; ">&quot;</span><span style="color: #000000; ">text</span><span
style="color: #000000; ">&quot;</span><span style="color: #000000; ">);
+        GENDER.setControlType(</span><span style="color: #000000; ">&quot;</span><span
style="color: #000000; ">select</span><span style="color: #000000; ">&quot;</span><span
style="color: #000000; ">);
+        </span><span style="color: #008000; ">//</span><span style="color:
#008000; "> Set gender options</span><span style="color: #008000; ">
+</span><span style="color: #000000; ">        Options genders </span><span
style="color: #000000; ">=</span><span style="color: #000000; "> </span><span
style="color: #0000FF; ">new</span><span style="color: #000000; "> Options();
+        genders.set(</span><span style="color: #000000; ">&quot;</span><span
style="color: #000000; ">M</span><span style="color: #000000; ">&quot;</span><span
style="color: #000000; ">, </span><span style="color: #000000; ">&quot;</span><span
style="color: #000000; ">Male</span><span style="color: #000000; ">&quot;</span><span
style="color: #000000; ">);
+        genders.set(</span><span style="color: #000000; ">&quot;</span><span
style="color: #000000; ">F</span><span style="color: #000000; ">&quot;</span><span
style="color: #000000; ">, </span><span style="color: #000000; ">&quot;</span><span
style="color: #000000; ">Female</span><span style="color: #000000; ">&quot;</span><span
style="color: #000000; ">);
+        GENDER.setOptions(genders);
+    }
+}</span></pre>
+
+		<p> As you can see, we have added the control type and possible gender 
+		options here for our two columns. For simplicity we have put this 
+		together with the database schema definition, but you may as well 
+		separate it and add the data in a different class or method.</p>
+
+		<h1>Form definition – the whole story</h1>
+		<p> Summing up, with traditional Struts2 syntax the entire form to display an employee
record may look like this:</p>
+		<pre><span style="color: #0000FF; ">&lt;</span><span style="color:
#800000; ">s:form </span><span style="color: #FF0000; ">action</span><span
style="color: #0000FF; ">=&quot;employeeDetail!doSave&quot;</span><span
style="color: #0000FF; ">&gt;</span><span style="color: #000000; ">
+    </span><span style="color: #0000FF; ">&lt;</span><span style="color:
#800000; ">s:textfield </span><span style="color: #FF0000; ">key</span><span
style="color: #0000FF; ">=&quot;employee.employeeId&quot;</span><span
style="color: #FF0000; "> disabled</span><span style="color: #0000FF; ">=&quot;true&quot;</span><span
style="color: #0000FF; ">/&gt;</span><span style="color: #000000; ">
+    </span><span style="color: #0000FF; ">&lt;</span><span style="color:
#800000; ">s:textfield </span><span style="color: #FF0000; ">key</span><span
style="color: #0000FF; ">=&quot;employee.firstname&quot;</span><span style="color:
#FF0000; "> size</span><span style="color: #0000FF; ">=&quot;40&quot;</span><span
style="color: #FF0000; "> required</span><span style="color: #0000FF; ">=&quot;true&quot;</span><span
style="color: #FF0000; "> disabled</span><span style="color: #0000FF; ">=&quot;employee.retired&quot;</span><span
style="color: #FF0000; "> </span><span style="color: #0000FF; ">/&gt;</span><span
style="color: #000000; ">
+    </span><span style="color: #0000FF; ">&lt;</span><span style="color:
#800000; ">s:textfield </span><span style="color: #FF0000; ">key</span><span
style="color: #0000FF; ">=&quot;employee.lastname&quot;</span><span style="color:
#FF0000; "> size</span><span style="color: #0000FF; ">=&quot;40&quot;</span><span
style="color: #FF0000; "> required</span><span style="color: #0000FF; ">=&quot;true&quot;</span><span
style="color: #FF0000; "> disabled</span><span style="color: #0000FF; ">=&quot;employee.retired&quot;</span><span
style="color: #FF0000; "> </span><span style="color: #0000FF; ">/&gt;</span><span
style="color: #000000; ">
+    </span><span style="color: #0000FF; ">&lt;</span><span style="color:
#800000; ">s:select </span><span style="color: #FF0000; ">key</span><span
style="color: #0000FF; ">=&quot;employee.gender&quot;</span><span style="color:
#FF0000; "> list</span><span style="color: #0000FF; ">=&quot;#{'M':'Male',
'F':'Female'}&quot;</span><span style="color: #FF0000; "> disabled</span><span
style="color: #0000FF; ">=&quot;employee.retired&quot;</span><span style="color:
#FF0000; "> </span><span style="color: #0000FF; ">/&gt;</span><span
style="color: #000000; ">
+    </span><span style="color: #0000FF; ">&lt;</span><span style="color:
#800000; ">s:textfield </span><span style="color: #FF0000; ">key</span><span
style="color: #0000FF; ">=&quot;employee.dateOfBirth&quot;</span><span
style="color: #FF0000; "> size</span><span style="color: #0000FF; ">=&quot;10&quot;</span><span
style="color: #FF0000; "> disabled</span><span style="color: #0000FF; ">=&quot;employee.retired&quot;</span><span
style="color: #FF0000; "> </span><span style="color: #0000FF; ">/&gt;</span><span
style="color: #000000; ">
+    </span><span style="color: #0000FF; ">&lt;</span><span style="color:
#800000; ">tr</span><span style="color: #0000FF; ">&gt;</span><span
style="color: #000000; ">
+      </span><span style="color: #0000FF; ">&lt;</span><span style="color:
#800000; ">td </span><span style="color: #FF0000; ">class</span><span
style="color: #0000FF; ">=&quot;tdLabel&quot;</span><span style="color:
#0000FF; ">&gt;</span><span style="color: #000000; ">
+         </span><span style="color: #0000FF; ">&lt;</span><span
style="color: #800000; ">s:label </span><span style="color: #FF0000; ">value</span><span
style="color: #0000FF; ">=&quot;%{getText('employee.phoneNumber')}&quot;</span><span
style="color: #FF0000; "> theme</span><span style="color: #0000FF; ">=&quot;simple&quot;</span><span
style="color: #FF0000; "> </span><span style="color: #0000FF; ">/&gt;</span><span
style="color: #000000; ">:</span><span style="color: #0000FF; ">&lt;/</span><span
style="color: #800000; ">td</span><span style="color: #0000FF; ">&gt;</span><span
style="color: #000000; ">
+      </span><span style="color: #0000FF; ">&lt;</span><span style="color:
#800000; ">td</span><span style="color: #0000FF; ">&gt;</span><span
style="color: #000000; ">
+         </span><span style="color: #0000FF; ">&lt;</span><span
style="color: #800000; ">s:textfield </span><span style="color: #FF0000; ">key</span><span
style="color: #0000FF; ">=&quot;employee.phoneNumber_cou&quot;</span><span
style="color: #FF0000; "> size</span><span style="color: #0000FF; ">=&quot;4&quot;</span><span
style="color: #FF0000; "> 
+                      maxlength</span><span style="color: #0000FF; ">=&quot;4&quot;</span><span
style="color: #FF0000; "> disabled</span><span style="color: #0000FF; ">=&quot;employee.retired&quot;</span><span
style="color: #FF0000; "> 
+                      theme</span><span style="color: #0000FF; ">=&quot;simple&quot;</span><span
style="color: #FF0000; "> </span><span style="color: #0000FF; ">/&gt;</span><span
style="color: #000000; ">
+         </span><span style="color: #0000FF; ">&lt;</span><span
style="color: #800000; ">s:textfield </span><span style="color: #FF0000; ">key</span><span
style="color: #0000FF; ">=&quot;employee.phoneNumber_cit&quot;</span><span
style="color: #FF0000; "> size</span><span style="color: #0000FF; ">=&quot;8&quot;</span><span
style="color: #FF0000; "> 
+                      maxlength</span><span style="color: #0000FF; ">=&quot;8&quot;</span><span
style="color: #FF0000; "> disabled</span><span style="color: #0000FF; ">=&quot;employee.retired&quot;</span><span
style="color: #FF0000; "> 
+                      theme</span><span style="color: #0000FF; ">=&quot;simple&quot;</span><span
style="color: #FF0000; "> </span><span style="color: #0000FF; ">/&gt;</span><span
style="color: #000000; ">-
+         </span><span style="color: #0000FF; ">&lt;</span><span
style="color: #800000; ">s:textfield </span><span style="color: #FF0000; ">key</span><span
style="color: #0000FF; ">=&quot;employee.phoneNumber_ext&quot;</span><span
style="color: #FF0000; "> size</span><span style="color: #0000FF; ">=&quot;20&quot;</span><span
style="color: #FF0000; "> 
+                      maxlength</span><span style="color: #0000FF; ">=&quot;20&quot;</span><span
style="color: #FF0000; "> disabled</span><span style="color: #0000FF; ">=&quot;employee.retired&quot;</span><span
style="color: #FF0000; "> 
+                      theme</span><span style="color: #0000FF; ">=&quot;simple&quot;</span><span
style="color: #FF0000; "> </span><span style="color: #0000FF; ">/&gt;</span><span
style="color: #000000; ">
+        </span><span style="color: #0000FF; ">&lt;/</span><span
style="color: #800000; ">td</span><span style="color: #0000FF; ">&gt;</span><span
style="color: #000000; ">
+    </span><span style="color: #0000FF; ">&lt;/</span><span style="color:
#800000; ">tr</span><span style="color: #0000FF; ">&gt;</span><span
style="color: #000000; ">
+    </span><span style="color: #0000FF; ">&lt;</span><span style="color:
#800000; ">s:select </span><span style="color: #FF0000; ">key</span><span
style="color: #0000FF; ">=&quot;employee.department.departmentId&quot;</span><span
style="color: #FF0000; "> list</span><span style="color: #0000FF; ">=&quot;departments&quot;</span><span
style="color: #FF0000; "> 
+              listKey</span><span style="color: #0000FF; ">=&quot;departmentId&quot;</span><span
style="color: #FF0000; "> listValue</span><span style="color: #0000FF; ">=&quot;name&quot;</span><span
style="color: #FF0000; "> required</span><span style="color: #0000FF; ">=&quot;true&quot;</span><span
style="color: #FF0000; "> 
+              disabled</span><span style="color: #0000FF; ">=&quot;employee.retired&quot;</span><span
style="color: #FF0000; "> </span><span style="color: #0000FF; ">/&gt;</span><span
style="color: #000000; ">
+    </span><span style="color: #0000FF; ">&lt;</span><span style="color:
#800000; ">tr</span><span style="color: #0000FF; ">&gt;</span><span
style="color: #000000; ">
+        </span><span style="color: #0000FF; ">&lt;</span><span style="color:
#800000; ">td </span><span style="color: #FF0000; ">class</span><span
style="color: #0000FF; ">=&quot;tdLabel&quot;</span><span style="color:
#0000FF; ">&gt;</span><span style="color: #000000; ">
+            </span><span style="color: #0000FF; ">&lt;</span><span
style="color: #800000; ">s:label </span><span style="color: #FF0000; ">value</span><span
style="color: #0000FF; ">=&quot;%{getText('employee.retired')}&quot;</span><span
style="color: #FF0000; "> 
+                     theme</span><span style="color: #0000FF; ">=&quot;simple&quot;</span><span
style="color: #FF0000; "> </span><span style="color: #0000FF; ">/&gt;</span><span
style="color: #000000; "> :</span><span style="color: #0000FF; ">&lt;/</span><span
style="color: #800000; ">td</span><span style="color: #0000FF; ">&gt;</span><span
style="color: #000000; ">
+        </span><span style="color: #0000FF; ">&lt;</span><span style="color:
#800000; ">td</span><span style="color: #0000FF; ">&gt;&lt;</span><span
style="color: #800000; ">s:checkbox </span><span style="color: #FF0000; ">key</span><span
style="color: #0000FF; ">=&quot;employee.retired&quot;</span><span style="color:
#FF0000; "> theme</span><span style="color: #0000FF; ">=&quot;simple&quot;</span><span
style="color: #FF0000; "> </span><span style="color: #0000FF; ">/&gt;&lt;/</span><span
style="color: #800000; ">td</span><span style="color: #0000FF; ">&gt;</span><span
style="color: #000000; ">
+    </span><span style="color: #0000FF; ">&lt;/</span><span style="color:
#800000; ">tr</span><span style="color: #0000FF; ">&gt;</span><span
style="color: #000000; ">
+</span><span style="color: #0000FF; ">&lt;/</span><span style="color:
#800000; ">s:form</span><span style="color: #0000FF; ">&gt;</span></pre>
+ 			
+			<p> Whereas for exactly the same form with Empire-Struts2-Extensions the code simply

+			looks like this:</p>
+			<pre><span style="color: #0000FF; ">&lt;</span><span style="color:
#800000; ">e:form </span><span style="color: #FF0000; ">record</span><span
style="color: #0000FF; ">=&quot;&lt;%= action.getEmployee() %&gt;&quot;</span><span
style="color: #FF0000; "> action</span><span style="color: #0000FF; ">=&quot;!doSave&quot;</span><span
style="color: #0000FF; ">&gt;</span><span style="color: #000000; ">
+    </span><span style="color: #0000FF; ">&lt;</span><span style="color:
#800000; ">e:control </span><span style="color: #FF0000; ">column</span><span
style="color: #0000FF; ">=&quot;&lt;%= EMPLOYEES.EMPLOYEE_ID %&gt;&quot;</span><span
style="color: #FF0000; "> </span><span style="color: #0000FF; ">/&gt;</span><span
style="color: #000000; ">
+    </span><span style="color: #0000FF; ">&lt;</span><span style="color:
#800000; ">e:control </span><span style="color: #FF0000; ">column</span><span
style="color: #0000FF; ">=&quot;&lt;%= EMPLOYEES.FIRSTNAME     %&gt;&quot;</span><span
style="color: #FF0000; "> </span><span style="color: #0000FF; ">/&gt;</span><span
style="color: #000000; ">
+    </span><span style="color: #0000FF; ">&lt;</span><span style="color:
#800000; ">e:control </span><span style="color: #FF0000; ">column</span><span
style="color: #0000FF; ">=&quot;&lt;%= EMPLOYEES.LASTNAME %&gt;&quot;</span><span
style="color: #FF0000; "> </span><span style="color: #0000FF; ">/&gt;</span><span
style="color: #000000; ">
+    </span><span style="color: #0000FF; ">&lt;</span><span style="color:
#800000; ">e:control </span><span style="color: #FF0000; ">column</span><span
style="color: #0000FF; ">=&quot;&lt;%= EMPLOYEES.GENDER %&gt;&quot;</span><span
style="color: #FF0000; "> </span><span style="color: #0000FF; ">/&gt;</span><span
style="color: #000000; ">
+    </span><span style="color: #0000FF; ">&lt;</span><span style="color:
#800000; ">e:control </span><span style="color: #FF0000; ">column</span><span
style="color: #0000FF; ">=&quot;&lt;%= EMPLOYEES.DATE_OF_BIRTH %&gt;&quot;</span><span
style="color: #FF0000; "> </span><span style="color: #0000FF; ">/&gt;</span><span
style="color: #000000; ">
+    </span><span style="color: #0000FF; ">&lt;</span><span style="color:
#800000; ">e:control </span><span style="color: #FF0000; ">column</span><span
style="color: #0000FF; ">=&quot;&lt;%= EMPLOYEES.PHONE_NUMBER %&gt;&quot;</span><span
style="color: #FF0000; "> </span><span style="color: #0000FF; ">/&gt;</span><span
style="color: #000000; ">
+    </span><span style="color: #0000FF; ">&lt;</span><span style="color:
#800000; ">e:control </span><span style="color: #FF0000; ">column</span><span
style="color: #0000FF; ">=&quot;&lt;%= EMPLOYEES.DEPARTMENT_ID %&gt;&quot;</span><span
style="color: #FF0000; "> </span><span style="color: #0000FF; ">/&gt;</span><span
style="color: #000000; ">
+    </span><span style="color: #0000FF; ">&lt;</span><span style="color:
#800000; ">e:control </span><span style="color: #FF0000; ">column</span><span
style="color: #0000FF; ">=&quot;&lt;%= EMPLOYEES.RETIRED %&gt;&quot;</span><span
style="color: #FF0000; "> </span><span style="color: #0000FF; ">/&gt;</span><span
style="color: #000000; ">
+</span><span style="color: #0000FF; ">&lt;/</span><span style="color:
#800000; ">e:form</span><span style="color: #0000FF; ">&gt;</span></pre>
+
+			<h1 id="access_logic">Implementing field level access logic</h1>
+			<p> By field level access logic we understand a function that 
+			determines whether a particular field&#39;s value is editable by the user 
+			or read only. If it&#39;s read only, the <i>disabled </i>attribute of
the 
+			corresponding input control needs to be set to <i>true</i>. In the above Struts2
form example we 
+			have implemented this logic - the 
+			fact that a field should be read only if the employee is retired - in the view. 
+			This was 
+			solely for convenience since actually it does not really belong there.</p>
+			<p> Consider for example what we additionally need to check user credentials, then
all the logic 
+				in the JSP becomes even more complex and hardly maintainable. But what other options
do we have? </p>
+			<p> A more sensible approach is to call a function on our bean or action class that
returns whether 
+				or not the field is disabled – like we have done further above 
+			by calling a function named <code>hasAccess()</code>. 
+				However this function needs to know which field we want to check for accessibility and
what 
+				other option do we have as again providing the field property name as a string literal.
And 
+				what makes this even more tedious and error prone is that if we want to 
+			do it properly we must 
+				not forget to call this function for every single input field in our entire application.</p>
+			<p> This is a good example why separation of view and logic is hard 
+			to achieve and not properly addressed by most 
+				current Web application frameworks.</p>
+			<p> With Empire-Struts2-Extensions all this is so much simpler and safer. As you
can easily see, 
+				all you have to provide in your JSPs is a reference to the record and column 
+			objects. The record reference 
+				may easily be set for the entire form so for the individual controls only the column
reference 
+				needs to be provided. The same field access logic that has caused us so much trouble
with the 
+				traditional approach is now easily implemented where it belongs: in the model with the
EmployeeRecord.</p>
+				<pre><span style="color: #0000FF; ">public</span><span style="color:
#000000; "> </span><span style="color: #0000FF; ">class</span><span
style="color: #000000; "> EmployeeRecord </span><span style="color: #0000FF; ">extends</span><span
style="color: #000000; "> DBRecord
+{
+    </span><span style="color: #0000FF; ">public</span><span style="color:
#000000; "> </span><span style="color: #0000FF; ">static</span><span
style="color: #000000; "> </span><span style="color: #0000FF; ">final</span><span
style="color: #000000; "> SampleDB.Employees T </span><span style="color: #000000;
">=</span><span style="color: #000000; "> 
+        SampleDB.getInstance().T_EMPLOYEES;  
+    
+    @Override
+    </span><span style="color: #0000FF; ">public</span><span style="color:
#000000; "> </span><span style="color: #0000FF; ">boolean</span><span
style="color: #000000; "> isFieldReadOnly(DBColumn column)
+    {
+        </span><span style="color: #0000FF; ">if</span><span style="color:
#000000; "> (</span><span style="color: #0000FF; ">super</span><span
style="color: #000000; ">.isFieldReadOnly(column))
+            </span><span style="color: #0000FF; ">return</span><span
style="color: #000000; "> </span><span style="color: #0000FF; ">true</span><span
style="color: #000000; ">;
+        </span><span style="color: #008000; ">//</span><span style="color:
#008000; "> Check if Employee has retired</span><span style="color: #008000; ">
+</span><span style="color: #000000; ">        </span><span style="color:
#0000FF; ">if</span><span style="color: #000000; "> (column</span><span
style="color: #000000; ">!=</span><span style="color: #000000; ">T.C_RETIRED
</span><span style="color: #000000; ">&amp;&amp;</span><span
style="color: #000000; "> getBoolean(T.C_RETIRED))
+            </span><span style="color: #0000FF; ">return</span><span
style="color: #000000; "> </span><span style="color: #0000FF; ">true</span><span
style="color: #000000; ">;
+        </span><span style="color: #008000; ">//</span><span style="color:
#008000; "> No, field is acessible</span><span style="color: #008000; ">
+</span><span style="color: #000000; ">        </span><span style="color:
#0000FF; ">return</span><span style="color: #000000; "> </span><span
style="color: #0000FF; ">false</span><span style="color: #000000; ">;
+    }
+}</span></pre>
+
+			<p> Note that this is really all the code you need. There are 
+			absolutely no changes to the JSPs necessary. And now you may easily 
+			extend this code additionally checking user credentials if required.</p>
+			<p> With this approach separation of view and logic is so much cleaner and thus
simpler to maintain and 
+				to extend. This has all been achieved by utilizing Empire-db’s metadata capabilities.

+			Without static and context specific metadata such a procedure 
+			is hardly possible and probably the reason why you have not seen 
+			anything like this with other solutions. </p>								
 			<!-- #EndEditable --></div>
 					
 			<div id="nav">



Mime
View raw message