xhtml file I use a viewParam:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:p="http://primefaces.org/ui"
xmlns:f="http://java.sun.com/jsf/core">
<f:metadata>
<f:viewParam name="actionId" value="#{editActionView.actionId}" required="true" />
</f:metadata>
<h:body>
The backing bean looks as follows:
EditActionView.java
#ManagedBean
#ViewScoped
public class EditActionView {
private long actionId;
#PostConstruct
void init() {
System.out.println("actionId: " + getActionId());
}
public long getActionId() {
return actionId;
}
public void setActionId(long actionId) {
this.actionId = actionId;
}
When I now call my application:
http://localhost:8080/aip/editAction.jsf?actionId=37
actionId is always 0. Where is my fault?
The ManagedBean is constructed before the setter is called. Therefore the System.out.println(...) in the #PostConstruct method prints the default value for the actionId of type long which is 0.
The setter is then called in the UPDATE_MODEL_VALUES phase. You can check this by putting another System.out.println(...) in the setter method, which should print the correct value.
Related
This question already has answers here:
Creating master-detail pages for entities, how to link them and which bean scope to choose
(2 answers)
Closed 6 years ago.
I am using JSF 2.0 and have a simple TestButton.xhtml like below:
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<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"
xmlns:a4j="http://richfaces.org/a4j"
xmlns:rich="http://richfaces.org/rich">
<h:body>
<h:form>
<h:inputText value="#{employee.empName}"/><br/>
<h:commandButton value="Derive" action="EmployeeTest2" >
<f:param name="Name" value="#{employee.empName}"/>
</h:commandButton>
</h:form>
</h:body>
</ui:composition>
And a Managed bean which is view scoped.
#ManagedBean(name="employee", eager=true)
#ViewScoped
public class Employee implements Serializable{
String empName;
String selectedEmployeeName;
public String getSelectedEmployeeName() {
return selectedEmployeeName;
}
public void setSelectedEmployeeName(String selectedEmployeeName) {
this.selectedEmployeeName = selectedEmployeeName;
}
public Employee() {
super();
}
public String getEmpName() {
return empName;
}
public void setEmpName(String empName) {
this.empName = empName;
}
public void manipulate(){
System.out.println("Manipulate called...");
System.out.println(selectedEmployeeName);
this.selectedEmployeeName = this.selectedEmployeeName + " Jr.";
}
Now on click of button, i navigate to EmployeeTest2.xhtml which has following code :
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<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"
xmlns:a4j="http://richfaces.org/a4j"
xmlns:rich="http://richfaces.org/rich">
<f:view>
<f:metadata>
<f:viewParam name="Name" value="#{employee.selectedEmployeeName}"/>
<f:event type="preRenderView" listener="#{employee.manipulate}"/>
</f:metadata>
</f:view>
<h:body>
<h:inputText value="#{employee.selectedEmployeeName}"/>
</h:body>
</ui:composition>
i get correctly navigated to EmployeeTest2.xhtml but my parameter value is coming as null. I can see that manipulate method is getting called(sysouts get printed) but selectedEmployeeName is null.
Please advise as to what am i missing.
I already referred to below thread and did same but of no help.
https://stackoverflow.com/questions/20880027/passing-parameters-to-a-view-scoped bean-in-jsf
Update 1: Changed
<h:inputText value="Chris"/>
to
<h:inputText value="#{employee.empName}"/>
Put a breakpoint at setter method for selectedEmployee. Setter is not getting called which means param passing is not working.
Update 2 Please see updated testButton.xhtml
?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<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"
xmlns:a4j="http://richfaces.org/a4j"
xmlns:rich="http://richfaces.org/rich">
<h:body>
<h:form>
<h:inputText name = "Name" value="#{employee.empName}"/><br/>
<h:commandButton value="Submit" action="EmployeeTest2" >
<f:param name="Name" value="#{employee.empName}"/>
</h:commandButton>
</h:form>
</h:body>
</ui:composition>
The code seems correct, in my opinion you are sending null to the next page because the value of the employee.empName is actually null.
I would debug the getEmpName() method when the TestButton.xhtml is loaded and then additionally check, after you click the button and move on to the next page whether the parameter is present in the url.
UPDATE
Ok the param is actually the value of the input. In that case add name to its definition. You form should look like this:
<h:inputText name="Name" value="#{employee.empName}"/><br/>
<h:commandButton value="Derive" action="EmployeeTest2" />
UPDATE 2
This is a workaround but you can also try this:
#PostConstruct
public void postConstruct(){
Map<String,String> params = FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap();
setSelectedEmployeeName(params.get("Name"));
}
I need to use <p:poll /> in some other work. So I was trying out the PrimeFaces ShowCase code:-
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:rich="http://richfaces.org/rich"
xmlns:p="http://primefaces.prime.com.tr/ui"
xmlns:a4j="http://richfaces.org/a4j">
<h:form>
<h:outputText id="txt_count" value="#{counterView.number}" />
<p:poll interval="3" listener="#{counterView.increment()}" update="txt_count" />
</h:form>
</html>
And the backing bean is as below:-
package com.poll;
import java.io.Serializable;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
#ManagedBean (name="counterView")
#ViewScoped
public class CounterView implements Serializable {
/**
*
*/
private static final long serialVersionUID = 1L;
private int number;
public int getNumber() {
return number;
}
public void increment() {
number++;
System.out.println(number);
}
}
It works as such: in browser number shows as 0 and doesn't change. In console I can see it printing as 1 once and then nothing.
What is wrong here? I am using PrimeFaces 3.4.2 on JSF 2.1
The p:poll tag does work for me with the following content, and I am on Primefaces 5.2.
NOTE: <h:head/> tag is needed and without that it does load primefaces related js files that are needed. I believe that is the reason why the refresh is not working in your case.
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:p="http://primefaces.org/ui">
<h:head/>
<h:body>
<h:form>
<h:outputText id="txt_count" value="#{counterView.number}" />
<p:poll interval="3" listener="#{counterView.increment}" update="txt_count" />
</h:form>
</h:body>
</html>
And the managed bean:
#ManagedBean(name="counterView")
#ViewScoped
public class CounterView implements Serializable {
private int number = 100;
public int getNumber() {
return number;
}
public void increment() {
System.out.println("Incrementing....");
number++;
}
}
Can you try with these and compare if it works.
UPDATE: Final Solution
To summarize the solution, along the <h:head/> tag, upgrading to newer version (5.x) of Primefaces helped resolve the issue, as indicated in the comments section.
I'm unable to pass a simple parameter from one Facelet to another, and set a bean property...here's my code:
The calling page, main.xhtml (only relevant code):
<h:link outcome="index" value="disconnect" >
<f:param name="logout" value="true" />
</h:link>
The final page, index.xhtml:
<?xml version='1.0' encoding='UTF-8' ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:f="http://xmlns.jcp.org/jsf/core">
<f:metadata>
<f:viewParam name="logout" value="#{indexBean.logout}"/>
<f:event type="preRenderView" listener="#{indexBean.redirect}" />
</f:metadata>
</html>
And the IndexBean (only relevant code):
#Named
#RequestScoped
public class IndexBean {
#Inject
private Logger logger;
private boolean logout;
public IndexBean() {this.logout = false;}
public void setLogout(boolean logout) {
logger.log(Level.DEBUG, "logout changed");
this.logout = logout;
}
public boolean isLogout() {return logout;}
public void redirect() throws IOException {
if(logout) {
//Never get in here
} else {
//Always here
}
}
}
I'm getting the URL parameter right on the link (http://localhost/index.xhtml?logout=true), but the setLogout method is never called.
I've even tried to change the logout type to String, change getter and setter properly and see what happens, but the setter is never called...
Any idea?
Thanks in advance!!
I am finding view params will not be passed from a page where the backing bean is different on the target page. How can I pass the product param from test1.xhtml to test2.xhtml?
test.xhtml
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:p="http://primefaces.org/ui" xmlns:o="http://omnifaces.org/ui">
<body>
<f:metadata>
<o:viewParam name="product" value="#{holder.value}"
converter="#{productConverter}"
converterMessage="Bad request. Unknown product." required="true"
requiredMessage="Bad request. Please use a link from within the system." />
</f:metadata>
<h:link outcome="/test2.xhtml" includeViewParams="true">link</h:link>
</body>
</html>
test2.xhtml
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:p="http://primefaces.org/ui" xmlns:o="http://omnifaces.org/ui">
<h:head></h:head>
<body>
<f:metadata>
<o:viewParam name="product" value="#{holder2.value}"
converter="#{productConverter}"
converterMessage="Bad request. Unknown product." required="true"
requiredMessage="Bad request. Please use a link from within the system." />
</f:metadata>
<h:link outcome="/test.xhtml" includeViewParams="true">link</h:link>
</body>
</html>
Holder.java
#ManagedBean
#ViewScoped
public class Holder<T> implements Serializable {
private T value;
public T getValue() {
return value;
}
public void setValue(T value) {
this.value = value;
}
}
Holder2.java
#ManagedBean
#ViewScoped
public class Holder2<T> implements Serializable {
private T value;
public T getValue() {
return value;
}
public void setValue(T value) {
this.value = value;
}
}
Product.java
public class Product {
private String name;
public Product(String name) {
super();
this.name = name;
}
public String getName() {
return name;
}
}
Product Converter
public class ProductConverter implements Converter {
private Map<String,Product> productMap=new HashMap<String, Product>();
public ProductConverter(List<Product> products) {
for (Product product:products){
productMap.put(product.getName().toLowerCase(), product);
}
}
#Override
public Object getAsObject(FacesContext context, UIComponent component, String value) {
return productMap.get(value.toLowerCase());
}
#Override
public String getAsString(FacesContext context, UIComponent component, Object value) {
if (!(value instanceof Product) ) {
return null;
}
return ((Product) value).getName().toLowerCase();
}
}
<h:link outcome="/test2.xhtml?includeViewParams=true">link</h:link>
This is not the right way to let JSF include view parameters in a <h:link>. Supplying it as a query string parameter is only valid in action attribute of UICommand components.
You need the includeViewParams attribute of the <h:link> instead.
<h:link outcome="/test2.xhtml" includeViewParams="true">link</h:link>
#ViewScoped is only valid for postbacks - if you change the URL, the bean dies.
To pass parameters between facelets, use <ui:param>
I think its a good practice to have an index page (in my case index.xhtml).
I want to pass some action on index page (for example in struts:<c:redirect url="list.do" /> and I go to struts action class without any links and buttons) I know if I want to use navigation I should use commandLink-s or buttons). I can write <h:commandButton> with onclick javascript function, but I don't feel this is the best option.
I'm totally new to JSF (using JSF 2.0) and I need your advice. What are the best practices for redirecting from index page to an action in controller?
///new version
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:f="http://java.sun.com/jsf/core">
<f:view>
<ui:insert name="metadata"/>
<f:viewParam name="action" value="listItems.xtml"/>
<f:event type="preRenderView" listener="#{yourBean.methodInManagedBean}" />
<h:body></h:body>
</f:view>
</html>
public class ForwardBean {
private String action;
// getter, setter
public void navigate(PhaseEvent event) {
FacesContext facesContext = FacesContext.getCurrentInstance();
String outcome = action;
facesContext.getApplication().getNavigationHandler().handleNavigation(facesContext, null, outcome);
}
}
You can use JSF preRenderView event to redirect to another page in following manner,
In your index.xhtml file
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:f="http://java.sun.com/jsf/core">
<f:view>
<ui:insert name="metadata"/>
<f:event type="preRenderView" listener="#{yourBean.methodInManagedBean}" />
<h:body></h:body>
</f:view>
</html>
In managed bean,
1st way is
public class yourClass{
FacesContext fc = FacesContext.getCurrentInstance();
ConfigurableNavigationHandler nav = (ConfigurableNavigationHandler)fc.getApplication().getNavigationHandler();
public void methodInManagedBean() throws IOException {
nav.performNavigation("list.do");//add your URL here, instead of list.do
}
}
or you can use 2nd way
public class yourClass{
public void methodInManagedBean() throws IOException {
FacesContext.getCurrentInstance().getExternalContext().redirect("list.do");//add your URL here, instead of list.do
}
}