how can i pass an url parameter between two xhtml pages? [duplicate] - jsf

This question already has answers here:
Does f:viewParam only pass query string in url when first page uses the same managed bean as the second page?
(2 answers)
Bookmarkability via View Parameters feature
(1 answer)
Closed 2 years ago.
i am trying to pass a parameter between two pages
in the bean i have this string with getters and setters
outcome is the function to run with the button
#Named("MeubleBean")
#ManagedBean
#SessionScoped
#RequestScoped
public class MeubleBean implements Serializable {
private String param1;
public String outcome() {
FacesContext fc = FacesContext.getCurrentInstance();
Map<String, String> params = fc.getExternalContext().getRequestParameterMap();
param1 = params.get("param1Name");
return "meubles3.jsf?faces-redirect=true&includeViewParams=true";
}
}
the code of the command button in the page meubles.xhtml :
<f:param id="param1" name="param1Name" value="param1Value"/>
</h:commandButton>
and finally the output in meubles3.xhtml :
<h:form><h:outputText value="#{meubleBean.param1}"></h:outputText>
</h:form>
i am trying the "param1value" as test value .
the output is always empty, i tried several ways none of em worked.
any solution please ???

Related

commandLink action not called [duplicate]

This question already has answers here:
commandButton/commandLink/ajax action/listener method not invoked or input value not set/updated
(12 answers)
Closed 5 years ago.
I'm using netbeans with glassfish
In my .xhtml file, a have a link which I expect to call action shopBean.instructorScheduleById()
<p:commandLink styleClass="tooltip tour-sched"
rendered="#{shopBean.instructorSchedule!=null}"
update=":dialogFormTS" action="#
{shopBean.instructorScheduleById()}"
oncomplete="dlgTS.show()" process="#this"
ajax="true">TS<h:outputText styleClass="tooltiptext"
value="#{shopBean.instructorSchedule.name}"/>
</p:commandLink>
Bean file
#ManagedBean(name = "shopBean")
#SessionScoped
public class ShopBean {
public ShopBean() {}
public void getInstructorScheduleById(){
System.out.println("hello");
}
}
I looked to my Developer tools under network i see there is an ajax request but when i looked on glassfish output, there's no word hello
Base on what i have learned here at stackoverflow, the get prefix is used for getter methods which means it must have a return value.
public void sum(){
int num1 = 1;
int num2 = 2;
System.out.println(num1 + num2);
}
// xhtml
<p:commandButton action="#{bean.sum()}" />
if you want get the value from a getter method:
pivate int sum;
public int getSum(){
return this.sum;
}
public void setSum(sum){
this.sum = sum;
}
// xhtml
<p:outputText value="#{bean.sum}" />
The quick solution for your problem is to remove the get prefix in your
bean.

Navigation using managed beans [duplicate]

This question already has answers here:
commandButton/commandLink/ajax action/listener method not invoked or input value not set/updated
(12 answers)
Closed 6 years ago.
i am working on a simple project in JSF 2.2 but i have some problem navigating between some pages. In the project i have a general template and all the views are template clients of that general template.
this is the view that i have troubles with:
<h:body>
<ui:composition template="./LayoutGeneral.xhtml">
<ui:define name="content">
<p:commandButton value="Registrar Comunidad" action="#{comunidadBean.irRegisterView}"/>
</ui:define>
</ui:composition>
</h:body>
In the action of the commandButton i call a method from the managed bean (Thar managed bean have other method that i call to change the page and they work fine, but this method doesnt):
(Managed Bean)
#ManagedBean
#SessionScoped
public class ComunidadBean {
private String idComunidad;
private String idPresidente;
private String calle;
private int numero;
private int nVecinos;
#EJB
private ComunidadDAO ejb;
public String register(){
if(ejb.realizaRegistro(this)){
return "principalView";
} else{
FacesMessage fm = new FacesMessage ("No se pudo registrar");
FacesContext.getCurrentInstance().addMessage("msg", fm);
return null;
}
}
public String irRegisterView(){
return "registroCView";
}
}
So the method "register" works fine and the page change but the method "irRegisterView" doesnt navigate to "registroCView" page.
Does someone has any idea of what it is happening?
Thanks!
I can' comment, so I write this as an answer.
I see that your bean is SessionScoped. You need to implement Serializable because a SessionScoped bean is passivating after a time.
do you have a form in LayoutGeneral.xhtml? If not, this code will never works because a commandButton needs be within a form.
why you call the method as a property? In JSF 2.2 and EL 2.3, you can call methods like this: #{comunidadBean.isRegisterView()}.
Is some exception thrown when click the button? If do, paste the stack trace.

How to print string in jsf Facelets page? [duplicate]

This question already has answers here:
javax.el.PropertyNotFoundException: The class 'xxx' does not have a readable property 'yyy'
(2 answers)
Closed 7 years ago.
(intro: I have generated Entity Clases from DB with one tabe lets say Beans, so it generated me Beans.java, BeansFacade.java, BeansController.java and AbstracFacade.java than I added JSF pages from entity classes, and just want to add something to list.xhtml)
In my BeansFacade.java I have
public String simple(){
return "output";
}
In BeansController.java I hava
public String printSimple(){
return ejbFacade.simple();
}
And than whem I try to print that
<h:outputText value="#{beansController.printSimple}"> </h:outputText>
I get an error
javax.el.PropertyNotFoundException:
The class 'fct.entity.EventsController' does not have the property 'printSimple'.
You are not able to call the method in h:outputText.
h:outputText tries to search the given variable related getter/setter
private String printSimple;
public String getPrintSimple()
{
return ejbFacade.simple();;
}
/**
* #return the simple
*/
public String getSimple()
{
return simple;
}
And than whem you can use the simple variable to get the value.
<h:outputText value="#{beansController.printSimple}"> </h:outputText>
before that call the printSimple method.

JSF. Call backing bean method on every page load [duplicate]

This question already has answers here:
Invoke JSF managed bean action on page load
(4 answers)
Closed 6 years ago.
here is my situation in short.
I have page with datatable and few buttons backed by bean. Bean should be initialized with some default properties. That properties can be changed depending on action.
I started with RequestScoped bean and #PostConstruct annotated method. but it seems that datatable works well only with View(Session)scoped. Now my setup look like this:
#ManagedBean
#ViewScoped
public class ProductsTableBean implements Serializable {
private LazyDataModel<Products> productsData;
#Inject
private ProductsFacade model;
public void onPageLoad() {
// here some defaults are set
// ...
System.err.println("onPageLoad called");
}
public void addRow() {
// andhere some defaults redefined
// ...
System.err.println("addRow called");
}
...
and snippet from jsf page:
<p:commandButton action="#{productsTableBean.addRow()}"
title="save"
update="#form" process="#form" >
</p:commandButton>
...
<f:metadata>
<f:event type="preRenderView" listener="#{productsTableBean.onPageLoad}"/>
</f:metadata>
And here is the main problem arise in calling order, i have following output:
onPageLoad called
addRow called
onPageLoad called <-- :(
But i want addRow to be the last action to be called, like this:
onPageLoad called
addRow called
Any simple solution here ?
Check this link :
http://www.mkyong.com/jsf2/jsf-2-prerenderviewevent-example/
You know that the event is call on every requests : ajax, validation fail .... You can check if it's new request like this:
public boolean isNewRequest() {
final FacesContext fc = FacesContext.getCurrentInstance();
final boolean getMethod = ((HttpServletRequest) fc.getExternalContext().getRequest()).getMethod().equals("GET");
final boolean ajaxRequest = fc.getPartialViewContext().isAjaxRequest();
final boolean validationFailed = fc.isValidationFailed();
return getMethod && !ajaxRequest && !validationFailed;
}
public void onPageLoad() {
// here some defaults are set
// ...
if (isNewRequest()) {...}
System.err.println("onPageLoad called");
}

JSF preload list for datatable in page [duplicate]

This question already has an answer here:
How and when should I load the model from database for JSF dataTable
(1 answer)
Closed 7 years ago.
I'm using EJB and JSF. I made a jsp simple page with a button "get list". When it's clicked, a managed bean method is called that sets the list, which is then displayed in the jsp with the dataTable tag.
The question is, how can I pre load this list/dataTable on page load without having to click the button?
This is the method that's called through the button action on the jsp:
public String retrieveList() {
items = facade.getAllItem();
return "";
}
this is the part of the jsp:
<h:dataTable value="#{mybean.items}" var="sup"
binding="#{mybean.dataTable}"
rowClasses="oddRow, evenRow"
styleClass="tableStyle"
headerClass="tableHeader"
columnClasses="column1, column2, column1, column1, column1, column1">
You can add a method init with #postConstruct
#PostConstruct
public void init(){
items = facade.getAllItem();
}
This will return the items only on bean creation ,
Annotate the method with #PostConstruct and get rid of return value.
#PostConstruct
public void retrieveList() {
items = facade.getAllItem();
}
This way the method will be executed immediately after construction of the bean and injection of all #EJB dependencies. In the JSF page you just have to bind to #{bean.items} the usual way.

Resources