Mentawai Web Framework

VOFilter and OVFilter

The purpose of the org.mentawai.filter.VOFilter (Value-Object Filter) is to populate a object with the values from the action input. This can be useful for form data so instead of providing your actions with a bunch of attributes like firstName, lastName, address, email, etc. you can give it a populated User object. Note that an User is just a POJO (plain old java object).

The way the VOFilter works is that it tries to inject every attribute from the action input into the action through reflection. It will also try to inject values directly into the fields, public or private, if you tell it to do so. So the rule is: first try setter methods (setUsername, setFirstName, etc.), then, if enabled, try to inject into the field (username, firstName) even if the field is private.

Things you should note about the VOFilter:

  • You may have to convert some fields in the action input (see Conversion) before the VOFilter can successfuly inject them into the value object. However, for simple conversions of String to integer, double, boolean e short date, the VOFilter will try to automatically convert the value by looking on the type of the field by reflection, in other words, for simple conversions you don't need to use a converter because the VOFilter will do the conversion automatically for you.

  • The VOFilter does not throw any exception. It tries its best to inject every field inside the action input, but if it cannot find a setter method or a field to do so it will just ignore that field. Remember that not everything inside your action input has to belong to the object your are populating. For example, if you have a java.sql.Connection (see Connection Filter) in your action input, you don't want to throw an exception if the VOFilter cannot inject the connection in your User object.

  • After your object is populated, the VOFilter will place it in the action input using a provided key (Ex: "myObject") or the class name (Ex: "org.myproject.User") if no key was provided.

  • The VOFilter does not remove any attribute from the action input. The values injected in the object remain also in the action input.

  • If you are populating multiple objects and they share the same name for some of their properties, you can use a prefix by using the constructor that takes a prefix as one of its parameters. If you define a prefix, the VOFilter will only look for prefix.property in the action input, but when inserting in the object it will remove the prefix and use only the property name. This a handy way to indicate which property belongs to each object when you different VOFilters populating different objects in the same request.

  • You may disable the automatically conversion and/or the field injection if you prefer for any reason. Just use the appropriate version of the VOFilter constructor. Refer to the API Documentation for more details.

import org.mentawai.core.*;
import org.mentawai.filter.*;

public class ApplicationManager extends org.mentawai.core.ApplicationManager {
	
    public void loadActions() {
		
        ActionConfig ac = new ActionConfig("/Hello", HelloVOFilter.class);
        ac.addConsequence(SUCCESS, fwd("/show.jsp"));
        ac.addConsequence(ERROR, fwd("/user.jsp"));
        addActionConfig(ac);
        
        ac.addFilter(new VOFilter(User.class)); // populate a User object and place it in the action input with "org.myprof.User" key
        
        // or
        
        ac.addFilter(new VOFilter(User.class, "user")); // populate a User object and place it in the action input with "user" key
        
        // or
        
        ac.addFilter(new VOFilter(User.class, "user", true)); // populate a User object and place it in the action input with "user" key
                                                              // also try to inject directly into the fields, public or private
    }
}

					


OVFilter

The org.mentawai.filter.OVFilter (Object-Value filter) is the opposite of the VOFilter. It looks for an object in the action output and tries to extract all its fields and place them into the action output. This can be very useful for example when you want to display all properties of an object inside a html form (see HTML Tags to understand how Mentawai can do this for your automatically, without a single line of code). With the OVFilter, you would load an User from the database and just place it in the action output. The filter will do the dirty work of extracting all properties and placing them in the action output.

Things you should note about the OVFilter:

  • Unlike the VOFilter, it will not get data from a private or public field. Your object should provide getters (getUsername(), getFirstName(), etc.) for the OVFilter to be able to extract them.

  • OVFilter will also call getters from superclasses, in other words, it will access every public getter of the class and superclasses.

  • You must provide in the OVFilter constructor the key with it the filter will look for the object in the action output.

import org.mentawai.core.*;
import org.mentawai.filter.*;

public class ApplicationManager extends org.mentawai.core.ApplicationManager {
	
    public void loadActions() {
		
        ActionConfig ac = new ActionConfig("/Hello", HelloOVFilter.class);
        ac.addConsequence(SUCCESS, fwd("/show.jsp"));
        ac.addConsequence(ERROR, fwd("/user.jsp"));
        addActionConfig(ac);
        
        ac.addFilter(new OVFilter("user")); // look for an object with the "user" key in the action output and extract its properties
                                            // placing them in the action output.
    }
}				


InjectionFilter and the OutputFilter

As you have the VOFilter and OVFilter, Mentawai also provides the InjectionFilter and OutputFilter. They behave the same way except that an InjectionFilter tries to inject the input values into the Action (not into a value-object) and the OutputFilter will try to get all properties from the action (not from a value-object).

The InjectionFilter and OutputFilter can be used if you want to forget about the input and output contexts, in other words, all values from the input will be placed in the action and all properties of the action will be placed in the output.

You should note that the Injection/Output filter play an important role when it comes to model-driven actions. With that approach (used by the MyBooks Demo Application), the InjectionFilter will try to inject directly in the model provided by the action and the OutputFilter will try to extract the properties directly from that same model.