jsf - dynamic view injection or routing -
i have jsf projekt , in there have different views, backed managedbeans.
what accomplish alter views while others remain are. has happen dynamically. in other words. want inject , remove views xhtml page without page refresh. have no clue how accomplish this.
even improve dynamic view injection based on urls. angularjs well. without routing great.
thanks in advance.
here illustration in pseudo code:
<nav> <h:link action="navigationbean.changeview(view1)" method="ajax">link1</h:link> <h:link action="navigationbean.changeview(view2)" method="ajax">link2</h:link> </nav> <h:viewcontainer> // view selected clicking nav links should injected here without page reload </h:viewcontainer>
what inquire improve done using facelet templating. you'll able way have page template shared content (the navigation menu in case) , create rest of views inherit it.
what can see suggested solution you're abusing post calls. #{fragmentspresenter.changeview('viewone')}
doesn't create sense because know want go when press link (to viewone
), you'll improve using plain links that.
here you've got illustration showing how handle navigation in proper way. let's suppose you've got view controller won't need in of cases:
viewcontroller.java
/** * give scope want bean depending on operations * oriented to. illustration @applicationscoped * * @author amaeztu * */ @managedbean @sessionscoped public class viewcontroller { /** * gets current view path , switches other 1 * * @return */ public string changeview() { string viewid = facescontext.getcurrentinstance().getviewroot() .getviewid(); if (viewid.equals("/view1.xhtml")) { homecoming "/view2"; } else { homecoming "/view1"; } } }
this controller's job check view coming , switch other one. it's pointless perform post request (to send form) navigate other view, while evaluate before page rendering.
here you've got how template view built:
template.xhtml
class="lang-xhtml prettyprint-override"><ui:composition xmlns="http://www.w3.org/1999/xhtml" xmlns:ui="http://java.sun.com/jsf/facelets" xmlns:f="http://java.sun.com/jsf/core" xmlns:h="http://java.sun.com/jsf/html"> <h:head /> <h:body> <h:form> <!-- utilize post requests when have determine destination dinamically @ server side --> <h:commandbutton value="switch view post" action="#{viewcontroller.changeview}" /> <br /> </h:form> <!-- plain navigation, utilize requests --> <h:link value="go view 1" outcome="/view1" /> <br /> <!-- determine @ page rendering time , evaluate other view path --> <h:link value="switch view without post" outcome="#{view.viewid eq '/view1.xhtml' ? '/view2' : '/view1'}" /> <br /> <br /> <ui:insert name="content" /> </h:body> </ui:composition>
this template page defines shared button/link set , calls content. i've implemented different kind of navigation options. using <h:link />
is, in case, straight-forward way. check sec link, here evaluate current view id when gets rendered , link go opposite 1 created. cool, isn't it?
now here implementation of kid views:
view1.xhtml
class="lang-xhtml prettyprint-override"><ui:composition xmlns="http://www.w3.org/1999/xhtml" xmlns:ui="http://java.sun.com/jsf/facelets" xmlns:f="http://java.sun.com/jsf/core" xmlns:h="http://java.sun.com/jsf/html" template="/template.xhtml"> <ui:define name="content"> <!-- here have @viewscoped bean managing content i.e. #{view1bean} --> view 1 </ui:define> </ui:composition>
view2.xhtml
class="lang-xhtml prettyprint-override"><ui:composition xmlns="http://www.w3.org/1999/xhtml" xmlns:ui="http://java.sun.com/jsf/facelets" xmlns:f="http://java.sun.com/jsf/core" xmlns:h="http://java.sun.com/jsf/html" template="/template.xhtml"> <ui:define name="content"> view 2 </ui:define> </ui:composition>
you'll able type address in browser , see them, that's what's called bookmarkable ;-)
see also:
get current page programmatically jsf facelets templating
No comments:
Post a Comment