How to conditionally render links in a Web page

From Wiki

Jump to: navigation, search

WebRatio provides different ways in order to conditionally render links in a Web page. You have to choose between the three possibilities considering your specific goal.

  • You can check the Protected property in the Properties View for area, pages and alternative pages. This property means that the specific module is available only for users belonging to specific groups. You can use the User model in the data model to set access rights to all protected modules. In the Properties View there is also the Visibility Policy property which allows you to choose the rendering of the links having the specific protected module as target. The available options are:
    1. Hide Incoming Links. The links are not rendered if the user hasn't the access rights to access it.
    2. Show Always Incoming Links. The links are always visible, but, if the user hasn't the access rights to access it, he is automatically redirect to a default login page.
    3. Disable Incoming Links. The links are always visible but, if the user hasn't the access rights to access it, they are disabled and the user cannot click on them.
  • You can hide and show links considering the user choices during its interaction with the Web application. Usually this means using switch units and alternative pages in the Web model. You can model in each alternative page the units and the links you want to render for each available choice. Then the switch units consider the user choice and show a different alternative page. The switch units can also consider the user group and show a different alternative depending on the current group. In this way the user sees only one alternative page and the other are hidden to him and so their units and links. On the right you can see a sample model.
  • You can hide and show links considering the value of attributes or other variables at runtime. In this case you have to write a layout template (cell, unit, and so on) in which you write the code with the JSTL conditions deciding whether a specific link is visible or not. Let's see a simple example. Suppose to have the following Web model and suppose you want to render the link going to the modify unit only if the order has not been processed yet.

The Order entity has a "processed" boolean attribute you can use to check whether the order has been processed or not. You have to add this boolean attribute to the display attribute of the unit. Then you can place the data unit in the grid and create a layout template like the following in which you have to:

  1. retrieve the attribute/attributes that are part of your condition (line 3)
  2. retrieve the link that has to be conditionally rendered (line 4)
  3. retrieve the other links which have to be rendered without condition (line 5)
  4. print the unit attributes (line 12-21)
  5. print the links (line 29)
  6. write the JSTL condition and print the link (32-35)

1:  #?delimiters [%, %], [%=, %]
2:  [% 
3:	def processed = unit.selectSingleNode("layout:Attribute[@name='processed']")
4:	def processLink = unit.selectSingleNode("layout:Link[@name='process']")
5:	def seeOrder = unit.selectSingleNode("layout:Link[@name='seeOrder']")
6:  %]
7:
8:  <div class="plain <wr:StyleClass/>">
9:	<div class="plain DataUnit">
10:	    <table>	      
11:	      <!-- ATTRIBUTES -->
12:	      <wr:Iterate var="attr" context="unit" select="layout:Attribute">
13:	        <tr>
14:	          <th valign="top" class="<wr:StyleClass/> header">
15:	             <wr:Label/>
16:	          </th>
17:	          <td valign="top" class="<wr:StyleClass/> value">
18:	             <wr:Value/>
19:	           </td>
20:	        </tr>
21:	      </wr:Iterate>
22:	
23:	      <!-- LINKS -->
24:	        <tr>
25:	          <td colspan="2">
26:	            <table>
27:	              <tr>
28:                    <td>
29:	                   <wr:Link class="link" context="seeOrder"/>
30:	                </td>
31:                    <td>
32:                    	<c:if test="${<wr:UnitId/>.data.[%= getFieldName(processed) %]}">
33:	                   <wr:Link class="link" context="seeOrder"/>
34:	                 </c:if>
35:	                </td>		                
36:	              </tr>
37:	            </table>
38:	          </td>
39:	        </tr>
40:	    </table>
41:	  </div>
42:   </div>

NOTE: It's under developing for the WebRatio 5.0 an extended feature that allows to specify directly in the Web model visibility conditions on unit attributes,links and fields.

Personal tools