diff options
author | Henri Muurimaa <henri.muurimaa@gmail.com> | 2017-09-11 11:57:15 +0300 |
---|---|---|
committer | Henri Sara <henri.sara@gmail.com> | 2017-09-11 11:57:15 +0300 |
commit | fb207248d5567a2661d5729d5149c7c8920a2efa (patch) | |
tree | 197085fb374e85ccc7eeb7d7998a7bcba9b09c43 /documentation/articles/UsingParametersWithViews.asciidoc | |
parent | e83f012cf5f1388dcab9be427575a655769f75e9 (diff) | |
download | vaadin-framework-fb207248d5567a2661d5729d5149c7c8920a2efa.tar.gz vaadin-framework-fb207248d5567a2661d5729d5149c7c8920a2efa.zip |
Migrate wiki articles to Vaadin documentation (#9912)
* Vaadin Tutorial For Swing Developers
* Setting And Reading Session Attributes
* Enabling Server Push
* Cleaning Up Resources In A UI
* Sending Email From Java Applications
* Using Parameters With Views
* Optimizing Sluggish UI
* Configuring Push For Your Enviroment
* Setting And Reading Cookies
* Using Polling
* Creating An Application That Preserves State On Refresh
* Finding The Current UI And Page And Vaadin Session
* Sending Events From The Client To The Server Using RPC
* Handling Logout
* Remember To Set The Locale
* Scalable Web Applications
* MVC Basics In ITMill Toolkit
* Access Control For Views
* Customizing The Startup Page In An Application
Diffstat (limited to 'documentation/articles/UsingParametersWithViews.asciidoc')
-rw-r--r-- | documentation/articles/UsingParametersWithViews.asciidoc | 112 |
1 files changed, 112 insertions, 0 deletions
diff --git a/documentation/articles/UsingParametersWithViews.asciidoc b/documentation/articles/UsingParametersWithViews.asciidoc new file mode 100644 index 0000000000..dfa29dabde --- /dev/null +++ b/documentation/articles/UsingParametersWithViews.asciidoc @@ -0,0 +1,112 @@ +[[using-parameters-with-views]] +Using parameters with Views +--------------------------- + +When the Navigator API is in use, one can pass "parameters" to Views in +the URI fragment. + +The remainder of the fragment that is left after the (longest) view name +matched is removed, is considered to be "fragment parameters". These are +passed to the View in question, which can then handle the parameter(s). +Basically: `#viewname/parameters`. + +Continuing from the basic navigation example, let's make a View that +displays a message passed as a fragment parameter: + +[source,java] +.... +import com.vaadin.navigator.View; +import com.vaadin.ui.Label; +import com.vaadin.ui.Panel; + +public class MessageView extends Panel implements View { + public static final String NAME = "message"; + + public MessageView() { + super(new VerticalLayout()); + setCaption("Messages"); + } + + @Override + public void enter(ViewChangeEvent event) { + if(event.getParameters() != null){ + // split at "/", add each part as a label + String[] msgs = event.getParameters().split("/"); + for (String msg : msgs) { + ((Layout)getContent()).addComponent(new Label(msg)); + } + } + } +} +.... + +Let's register `MessageView` along with the other Views: + +[source,java] +.... +import com.vaadin.navigator.Navigator; +import com.vaadin.navigator.Navigator.SimpleViewDisplay; +import com.vaadin.server.Page; +import com.vaadin.server.WrappedRequest; +import com.vaadin.ui.UI; + +public class NavigationtestUI extends UI { + + @Override + public void init(VaadinRequest request) { + // Create Navigator, make it control the ViewDisplay + Navigator navigator = new Navigator(this, this); + + // Add some Views + navigator.addView(MainView.NAME, new MainView()); // no fragment + + // #count will be a new instance each time we navigate to it, counts: + navigator.addView(CountView.NAME, CountView.class); + + // #message adds a label with whatever it receives as a parameter + navigator.addView(MessageView.NAME, new MessageView()); + } +} +.... + +Finally, we'll add two labels to the MainView so we don't have to type +in the browsers address-bar to try it out: + +[source,java] +.... +import com.vaadin.navigator.View; +import com.vaadin.server.ExternalResource; +import com.vaadin.ui.Link; +import com.vaadin.ui.Panel; + +public class MainView extends Panel implements View { + + public static final String NAME = ""; + + public MainView() { + + VerticalLayout layout = new VerticalLayout(); + + Link lnk = new Link("Count", new ExternalResource("#!" + CountView.NAME)); + layout.addComponent(lnk); + + lnk = new Link("Message: Hello", new ExternalResource("#!" + + MessageView.NAME + "/Hello")); + layout.addComponent(lnk); + + lnk = new Link("Message: Bye", new ExternalResource("#!" + + MessageView.NAME + "/Bye/Goodbye")); + layout.addComponent(lnk); + setContent(layout); + } + + @Override + public void enter(ViewChangeEvent event) { + + } +} +.... + +Simple! Let's just conclude by noting that it's usually a good idea to +make sure the parameters are URI encoded, or the browser might +disapprove. |