java - Calling a function when I click on a link -
so in jsf page have outputtext content generate managed bean.
<h:outputtext value="#{bean.show(id)}" />
then here method show()
inside managed bean bean
.
public string show(long fromuserid){ user = myejb.finduserbyid(fromuserid); string content = ""; //this method contains complex business logics, below illustration if(...){ content += ... ; //generate content here content += "<a href=\"profile.jsf?userid=" + fromuserid + "\">"; content += from.getfname() + " " + from.getlname() + "</a> }else if(...){ //code required access database content += ...; }else if(...){ content += ...; } ... return content; }
so on page, have content , link link profile page. want when click on link, i want invoke method run of business logic before redirect me profile.jsf page. guess need write javascript when click link, invoke method, return string redirect me profile.jsf. try this, not work
public string show(long fromuserid){ ... content += "<a href=\"#\" onclick=\"gotoprofile(1)\">"; content += from.getfname() + " " + from.getlname() + "</a> return content; } public string gotoprofile(long id){ //some business logic return "profile.jsf?faces-redirect=true&userid=" + id; }
help please
edit
@balusc: try suggest, due way design, not work. here why: have 3 view scoped managed bean
pages [centralfeed.jsf, profile.jsf, textbook.jsf] using same header, create template page header below.
template-header.xhtml
<h:form style="display: inline;"> <p:menubutton value="#{sessionbean.option}" > <p:menuitem value="my profile" ajax="false" action="#{sessionbean.gotoprofile}" /> <p:menuitem value="stanford university" ajax="false" action="#{sessionbean.gotocentral}" /> <p:menuitem value="universal trade" ajax="false" action="#{sessionbean.gototrade}" /> </p:menubutton> </h:form> <ui:insert name="body"/>
then 3 jsf pages use ui:composition
, ui:define
include template header. header of template page have primefaces menu button menu button bind session scoped managed bean
. when click on item of menu button, redirect 1 of 3 jsf page. want menu button reflect correctly page at. here attempt
sessionbean.java ---> session scoped managed bean
public static final string my_profile = "my profile"; public static final string central_feed = "stanford university"; public static final string trade = "universal trade"; private string option = null; @postconstruct public void init(){ me = scholarejb.finduserbyusername(getloginusername()); if(me != null){ httpservletrequest request = (httpservletrequest) facescontext.getcurrentinstance(). getexternalcontext().getrequest(); string path = request.getservletpath(); if(path.contains("profile")){ option = my_profile; }else if(path.contains("centralfeed")){ option = central_feed; }else if(path.contains("textbook")){ option = trade; }else{ option = " "; } } public string gotoprofile(){ option = my_profile; return "profile.jsf?faces-redirect=true&userid="+me.getid(); } public string gotocentral(){ option = central_feed; return "centralfeed.jsf?faces-redirect=true"; } public string gototrade(){ option = trade; return "textbook.jsf?faces-redirect=true"; }
that work if navigate using menu button, of 3 page, can navigate others, , still want menu button reflect correctly page looking at. let take @ centralfeed.jsf. link profile.jsf this
<p:commandlink value="profile" action="#{centralfeed.gotoprofile(centralfeed.me.id)}" ajax="false"/>
so in centralfeed.java, have:
public string gotoprofile(long userid){ sessionbean.setoption(sessionbean.my_profile); //sessionbean: session scoped bean reference return "profile.jsf?faces-redirect=true&userid=" + userid; }
when click link, got redirect profile.jsf, , value of menu button my_profile
. work, have problem when have lot of business logic figure out should output. try in original code above. depend on condition, generate html. link generate <a href="...">
, take me straight profile.jsf
, dont allow me change value of menu button. please
just desired job in constructor or @postconstruct
in managed bean associated profile.jsf
el. can use @managedproperty
let jsf set request parameters in managed bean.
@managedproperty(value="#{param.userid}") private long userid; @postconstruct public void init() { // business stuff based on userid. }
unrelated concrete problem, must agree thorbjørn. know you're using jsf 2.0 / facelets. create facelets include template or composite component that. plain vanilla html not belong in model, in view.
Comments
Post a Comment