JBoss.orgCommunity Documentation
Starting in version 2.1, Errai offers a system for creating applications that have multiple bookmarkable pages. This navigation system has the following features:
Declarative, statically-analyzable configuration of pages and links
Compile time referential safety (i.e. “no broken links”)
Can generate storyboard of the application’s navigation flow at compile time
Decentralized configuration
Create a new page by creating a new annotated class. No need to edit a second file.
Make navigational changes in the natural place in the code.
Integrates cleanly with Errai UI templates, but also works well with other view technologies.
Builds on Errai CDI
To use Errai UI Navigation, you must include it on the compile-time classpath. If you are using Maven for your build, add this dependency:
<dependency>
<groupId>org.jboss.errai</groupId>
<artifactId>errai-navigation</artifactId>
<version>${errai.version}</version>
</dependency>
If you are not using Maven for dependency management, add
errai-navigation-version.jar
to your classpath.
Errai Navigation has three main parts: the
@Page
annotation marks any widget as a page; the
TransitionTo<P>
interface is an injectable type that provides a link to another page; and the
Navigation
singleton offers control over the navigation system as a whole.
The Navigation singleton owns a GWT Panel, called the navigation panel . This panel always contains a widget corresponding to the the fragment ID (the part after the # symbol) in the browser's location bar. Whenever the fragment ID changes for any reason (for example, because the user pressed the back button, navigated to a bookmarked URL, or simply typed a fragment ID by hand), the widget in the navigation panel is replaced by the widget associated with that fragment ID. Likewise, when the application asks the navigation system to follow a link, the fragment ID in the browser's location bar is updated to reflect the new current page.
To declare a page, annotate any subclass of Widget with the
@Page
annotation:
@Page
public class ItemListPage extends Composite {
// Anything goes...
}
By default, the name of a page is the simple name of the class that declares it. In the above example, the
ItemListPage
will fill the navigation panel whenever the browser's location bar ends with
#ItemListPage
. If you prefer a different page name, use the
@Page
annotation's
path
attribute:
@Page(path="items")
public class ItemListPage extends Composite {
// Anything goes...
}
Each application must have exactly one starting page . This requirement is enforced at compile time. The starting page is displayed when there is no fragment ID present in the browser's location bar.
Use the
startingPage
attribute to declare the starting page, like this:
@Page(startingPage=true)
public class WelcomePage extends Composite {
// Anything goes...
}
Pages are looked up as CDI beans, so you can inject other CDI beans into fields or a constructor. Pages can also have
@PostConstruct
and
@PreDestroy
CDI methods.
The lifecycle of a Page instance is governed by CDI scope: Dependent and implict-scoped beans are instantiated each time the user navigates to them, and Singleton and ApplicationScoped beans are created only once over the lifetime of the application. If a particular page is slow to appear because its UI takes a lot of effort to build, try marking it as a singleton.
Any widget can be a page. This includes Errai UI
@Templated
classes! Simply annotate any Errai UI templated class with
@Page
, and it will become a page that can be navigated to.
To declare a link from one page to another, inject an instance of
TransitionTo<P>
, where
P
is the class of the target page.
For example, this code declares a link from the welcome page to the item list page:
@Page(startingPage=true)
public class WelcomePage extends Composite {
@Inject TransitionTo<ItemListPage> startButtonClicked;
}
You do not need to implement the
TransitionTo
interface yourself; the framework creates the appropriate instance for you.
You can inject any number of links into a page. The only restriction is that the target of the link must be a Widget type that is annotated with
@Page
.
To follow a link, simply call the
go()
method on an injected
TransitionTo
object. For example:
@Page(startingPage=true)
public class WelcomePage extends Composite {
@Inject TransitionTo<ItemListPage> startButtonClicked;
public void onStartButtonPressed(ClickEvent e) {
startButtonClicked.go();
}
}
Now that you've created several pages and injected some links between them, there is one last step to seeing the results in the user interface: you must add the navigation panel somewhere in the document. The best place to do this is during application startup, for example in the
@PostConstruct
method of your
@EntryPoint
class. You can allow Errai Navigation to control the full contents of the page, or you can opt to keep some parts of the page (headers, footers, and sidebars, for example) away from Errai Navigation.
This example code gives the Navigation system control of the full page contents:
@EntryPoint
public class Bootstrap {
@Inject
private Navigation navigation;
@PostConstruct
public void clientMain() {
RootPanel.get().add(navigation.getContentPanel());
}
}
The following example reserves space for header and footer content that is not affected by the navigation system:
@EntryPoint
public class Bootstrap {
@Inject
private Navigation navigation;
@PostConstruct
public void clientMain() {
VerticalPanel vp = new VerticalPanel();
vp.add(new HeaderWidget());
vp.add(navigation.getContentPanel());
vp.add(new FooterWidget());
RootPanel.get().add(vp);
}
}
This last example demonstrates a simple approach to defining the page structure with an Errai UI template. The final product is identical to the above example, but in this case the overall page structure is declared in an HTML template rather than being defined programmatically in procedural logic:
@Templated
@EntryPoint
public class OverallPageStrucutre extends Composite {
@Inject
private Navigation navigation;
@Inject @DataField
private HeaderWidget header;
@Inject @DataField
private SimplePanel content;
@Inject @DataField
private FooterWidget footer;
@PostConstruct
public void clientMain() {
// give over the contents of this.content to the navigation panel
content.add(navigation.getContentPanel());
// add this whole templated widget to the root panel
RootPanel.get().add(this);
}
}
Because the pages and links in an Errai Navigation application are declared structurally, the framework gets a complete picture of the app's navigation structure at compile time. This knowledge is saved out during compilation (and at page reload when in Dev Mode) to the file
.errai/navgraph.gv
. You can view the navigation graph using any tool that understands the GraphViz (also known as DOT) file format.
One popular open source tool that can display GraphViz/DOT files is GraphViz . Free downloads are available for all major operating systems.
When rendered, a navigation graph looks like this:
In the rendered graph, the pages are nodes (text surrounded by an ellipse). The starting page is drawn with a heavier stroke. The links are drawn as arrows from one page to another. The labels on these arrows come from the Java field names the TransitionTo objects were injected into.