Configuring Actions to View Mapping

Overview

Based on Action execution results, Controller will ask ActionMap to return the view file name for display. A View instance is created, and Controller will pull the template file, request parameters, action execution results together to the View instance, and display it to the user.

The Action Map

Basically, the action map is an array of key-value pairs. It has several notations with different uses, as shown below:

Notation Used by Meaning Example
<actionName>.<result> ActionMap The file name of the template to be displayed after the Action’s execute() is executed. ListUsersAction.success
<actionName>.<command>.<result> ActionMap The file name of the template to be displayed after the specified method on the Action is executed.

If no such key is specifed for the specifed <actionName> and <command>, ActionMap will look for the key <actionName>.<result> instead.
EditUserAction.update.success
<actionName>.<result>.contentType Controller The MIME content type of the template to be displayed after the Action’s execute() is executed.

If this key is not set, the default content type (text/html + application’s default character encoding) is used.
ListUsersAction.success.contentType
<actionName>.<command>.<result>.contentType Controller The MIME content type of the template to be displayed after the specified method on the Action is executed.

If this key is not set, the default content type (text/html + application’s default character encoding) is used, disregard the content type set at the action itself.
EditUserAction.update.success.contentType
<actionName>.class ActionFactory The class to instantiate for the specified <actionName>, to allow action aliasing and reusing Actions at different places.

This type of action map key is optional.
ListAllUsersAction.class


For the name of the result view, if you want to redirect user to the specified page in the action-view mapping, you need to append the key [REDIRECT] before the URL to be redirected. That is,

Logout.google.success=[REDIRECT]http://www.google.com

Controller will identify it and redirect user to the specified URL.

In addition, you may also pass the request parameters and action execution result values during redirection. To do so, use the expressions ${result.<fieldName>} and ${request.<fieldName>} in the URL. For example,

Registration.success=[REDIRECT]Login.html?registration=${result.registrationNumber}&userID=${request.userID}

Controller will redirect the user to

Login.html?registration=928462521&userID=new_user_8834

given registrationNumber after Action execution is 928462521 and userID in request parameter is new_user_8834.

If you want to redirect back to the page the user came from (i.e. the HTTP_REFERER), place a [REFERER] right after the [REDIRECT] tag. This is useful for Actions like changing display language.

ChangeLocaleAction.success=[REDIRECT][REFERER]

Action Map Configuration Methods

Depending the available libraries on the server, scope of overriding configuration on Apache HTTPD installation allowed by the server/admin, developer’s preferences, performance, etc. Mivec Framework provides several implementations for configuring Actions to View mappings. The class for initializing the action map is defined in configuration file, <webapp-root>/config/MivecFrameworkSettings.php, with the configuration parameter “mivec.settings.actionMapInitializer”.

Below we will list the available implementations:

PropertiesActionMapInitializer

The most simple form on ActionMapInitializer. It reads the file <webapp-root>/config/views.properties.

The file is a typical php.ini type file, similar to Java properties files.

LoginAction.input=views/login_form.php
AdminLoginAction.class=LoginAction
AdminLoginAction.notAllowed=views/local_users_only.php
UserRegistrationAction.input=views/new_user.php
UserRegistrationAction.confirm.success=views/confirm_new_user.php
UserRegistrationAction.create.success=views/create_new_user_success.php

PHPArrayActionMapInitializer

This ActionMapInitializer is good for places where you cannot protect your files using .htaccess. It reads the file <webapp-root>/config/ActionMap.php.

Inside this file there’s a variable defined, $mapping. This variable will be returned by PHPArrayActionMapInitializer - please do not change the name of the variable; otherwise it will break.

Other than this, just add the key and values using typical array value assignment statements:

$mapping["LoginAction.input"] = "views/login_form.php";
$mapping["AdminLoginAction.class"] = "LoginAction";
$mapping["AdminLoginAction.notAllowed"] = "views/local_users_only.php";
$mapping["UserRegistrationAction.input"] = "views/new_user.php";
$mapping["UserRegistrationAction.confirm.success"] = "views/confirm_new_user.php";
$mapping["UserRegistrationAction.create.success"] = "views/create_new_user_success.php";

As it is simply a PHP array, you may use your PHP skills to combine arrays set from other files, thereby spliting the “site map” into separated files.

PHP4DOMXMLActionMapInitializer, DOMITXMLActionMapInitializer

These implementations are just the same, just with different backends for XML parsing. They read the file <webapp-root>/config/action.xml.

For PHP4DOMXMLActionMapInitializer, you need to enable DOM XML support extension in your PHP 4 installation. For DOMITXMLActionMapInitializer, you need to download DOMIT! XML library from here, and define DOMIT_DIR inside MivecServlet.php.

An example for the file action.xml is as follows:

<?xml version="1.0" encoding="UTF-8" ?>
<actions>
	<include file="config/admin_module.xml" />
 
	<action name="LoginAction">
		<result name="input">views/login_form.php</result>
	</action>
	<action name="AdminLoginAction" class="LoginAction">
		<result name="notAllowed">views/local_users_only.php</result>
	</action>
	<action name="UserRegistrationAction">
		<result name="input">result/view4.php</result>
		<command name="confirm">
			<result name="success">views/confirm_new_user.php</result>
		</command>
		<command name="create">
			<result name="success">views/create_new_user_success.php</result>
		</command>
	</action>
</actions>

If you need to combine multiple action-view mapping XML files, you may use the <include> tag, as shown above.

For each <action> tag, you may define the optional class attribute, which allows a certain action name to use another existing Action implementation - see the meaning of <actionName>.class notation, above.

The <result> tags under <action> tags are the views for the return value after the execute() method.

The <command> tags defines the alternate commands’ view mapping.


« Prev: Validating User Input Next: Internationalizing Application with gettext »

 
  devguide/mapping_actions.txt · Last modified: 2005/03/05 14:41
 
Recent changes RSS feed Powered by PHP Valid XHTML 1.0 Valid CSS Driven by DokuWiki

SourceForge.net Logo