Skip to content
Steve Hannah edited this page Jul 13, 2021 · 2 revisions

<import>

Synopsis

Add packages and classes to the namespace for this view. This also embeds import statements inside the resulting view java class so that your Java expressions can use them.

Usage

Place zero or more <import> tags as child elements of the root tag of the view. The contents should be one or more java import statements.

<import>
import com.example.myapp.MyClass;
import com.example.myapp.models.*;
import static com.example.myapp.MyClass.myStaticFunc;
</import>

Once you have imported a class or package, these classes can be referenced by their simple name throughout the view. This includes the use of XML tags for creating imported Component classes.

For example, if you have defined your own component class at com.example.myapp.components.MyCustomComponent, then to use this component in a view you would need to import it:

<import>
import com.example.myapp.components.MyCustomComponent;
</import>

<!-- Create instance of MyCustomComponent -->
<myCustomComponent .../>

Scope of Imports

Even though the <import> tag is a child of the root view, the scope of the imports are the entire view. E.g. the rad-controller attribute of the root tag, which specifies a ViewController to use for the view, can use the simple name instead of the fully-qualified name of the view controller class if it is imported in an <import> directive.

E.g. Both of the following examples will work, assuming you have defined a ViewController at com.example.myapp.controllers.MyViewController:

Specifying the view controller using its fully-qualified classname in the rad-controller attribute.
<?xml version="1.0"?>
<y rad-controller="com.example.myapp.controllers.MyViewController">
    ...
</y>
Specifying the view controller using its simple classname in the rad-controller attribute works because the MyViewController class is imported in the <import> tag.
<?xml version="1.0"?>
<y rad-controller="MyViewController">
    <import>
    import com.example.myapp.controllers.MyViewController;
    </import>
</y>

The following is also valid:

<?xml version="1.0"?>
<y rad-controller="MyViewController">
    <import>
    import com.example.myapp.controllers.*;
    </import>
</y>

See Also

Clone this wiki locally