I have <h:inputText> on form and what I need is to execute some method from backing bean on BLUR event:
public void test()
{
System.out.print("HELLO!");
}
Can you help me?
You can use <f:ajax>
<h:form>
<h:inputText value="#{managedBean.val}" >
<f:ajax event="blur" render="result" listener="#{managedBean.test}"/>
</h:inputText>
</h:form>
#ManagedBean(name = "managedBean")
public class Bean {
private String val; // getter and setter
...
public void test() {
System.out.print("HELLO!");
}
}
Alternative :
If you are using richfaces then you can use a4j:jsFunction
See Also
JSF2: Ajax in JSF – using f:ajax tag
How-to-update-a-value-displayed-in-the-page-without-refreshing
Related
I have bean "RichFacesTreeNodeBean" with property "newItemInfo", and I call modal panel with inputTextArea, which must contain actual value of that property, but it contains the first value of property all the time. Here is the part of index.xhtml:
<rich:popupPanel id="editPanel">
<h:form>
<h:inputTextarea value="#{richFacesTreeNodeBean.newItemInfo}" />
<!-- some buttons -->
</h:form>
</rich:popupPanel>
And RichFacesTreeNodeBean.java:
#ManagedBean
#ViewScoped
public class RichFacesTreeNodeBean {
private String newItemInfo;
public String getNewItemInfo() {
return newItemInfo;
}
How to fix this?
<h:form>
<a4j:outputPanel layout="block" ajaxRendered="true">
<h:inputTextarea value="#{richFacesTreeNodeBean.newItemInfo}" />
</a4j:outputPanel>
</h:form>
It works.
I have developed a navigation menu with primefaces tree component. The tree nodes have a Project object as data inside. I want to be able to load a view with the project details which loads dynamically when I click each project.
For that I have defined an ajax event select on the tree:
<h:form>
<p:tree value="#{treeBean.root}" var="node"
selection="#{treeBean.selectedNode}" id="tree" >
<p:ajax event="select" listener="#treeBean.onNodeSelect}"/>
<p:treeNode styleClass="treeNode">
<h:outputText value="#{node}" />
</p:treeNode>
</p:tree>
</h:form>
Which calls a onNodeSelectMethod in the treeManagedBean:
#ViewScoped
#Component("treeBean")
#ManagedBean
public class TreeManagedBean{
private Project selectedProject;
public Project getSelectedProject(){
return selectedProject;
}
public void setSelectedProject(Project project){
this.selectedProject = project;
}
public void onNodeSelect(NodeSelectEvent event) {
this.selectedProject = (Project)event.getTreeNode().getData();
FacesContext context = FacesContext.getCurrentInstance();
ConfigurableNavigationHandler handler = (ConfigurableNavigationHandler) context.getApplication().getNavigationHandler();
handler.performNavigation("viewProject");
}
And then I load viewProject.xhtml via faces-config.xml, and populate the property in the other managedBean:
#Component("projectView")
#ViewScoped
#ManagedBean
public class ProjectView {
#ManagedProperty("#{treeBean.selectedProject}")
private Project project;
}
The view loads without any errors, but the property is never set. The ajax call works and is setting the property in the first bean. Is this the way to do it?
I solved it just adding an identifier to the managedBeans, for that I was obliged to provide a default constructor:
#ViewScoped
#Component("treeBean")
#ManagedBean("treeBean")
public class TreeManagedBean{
public TreeManagedBean(){}
}
and
#Component("projectView")
#ViewScoped
#ManagedBean("projectView")
public class ProjectView {
#ManagedProperty("#{treeBean.selectedProject}")
private Project project;
public ProjectView(){}
}
In JSF, how can I invoke method in case of conversion failing on any of input fields? I guess I can write my own converters and do all the stuff there, but isn't there a more simple way?
You could use a PreRenderViewEvent listener, and in that method check if validation has failed. This listener method will be called every time just before the view is rendered.
E.g.
Consider the following Facelet:
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:f="http://java.sun.com/jsf/core"
>
<h:body>
<h:messages/>
<f:event listener="#{onErrorBean.onPreRenderView}" type="preRenderView" />
<h:form>
<h:inputText value="#{onErrorBean.test}" label="test" required="true" />
<h:commandButton value="Submit" action="#{onErrorBean.onSuccess}" />
</h:form>
</h:body>
</html>
And the following backing bean:
#ViewScoped
#ManagedBean
public class OnErrorBean {
private String test;
public void onPreRenderView() {
if (FacesContext.getCurrentInstance().isValidationFailed()) {
onError();
}
}
public void onSuccess() {
System.out.println("Success!");
}
public void onError() {
System.out.println("Error!");
}
public String getTest() {
return test;
}
public void setTest(String test) {
this.test = test;
}
}
If you press the button without entering a value you'll see "Error!" being printed in your console, enter a value and you'll see "Success!".
Im trying out an example with EJB Session Beans. I wanna see the differences them. My basic projects diagram is below ;
http://img109.imageshack.us/img109/8262/85220418.png
Project is like a shopping cart. When i invoke to the managed bean to get result of added Urun objects via injected EJB, result list must be last Urun object. Cause my EJB is #Stateless bean. But, when i run the application, it keeps the all new added Urun object. But it is Stateless, it must keep the last Urun object in every action. And then, when i open the application page in other web browser, it list the before added Urun object's list. But managed bean and Ejb is Stateless in my model. But it acts a Singleton bean. Where is my problem?
#Stateless
public class AlisverisSepetiEJB {
List<Urun> urunler=new ArrayList<>();
public List<Urun> getUrunler() {
return urunler;
}
public void setUrunler(List<Urun> urunler) {
this.urunler = urunler;
}
public void urunEkle(Urun urun){
urunler.add(urun);
}
}
#ManagedBean(name="bean")
#RequestScoped
public class JSFYonetimliNesne {
public JSFYonetimliNesne(){
System.out.println("Yönetimli nesne çalıştı");
}
#EJB
AlisverisSepetiEJB alisverisSepeti;
Urun urun=new Urun();
List<Urun> urunler;
public List<Urun> getUrunler() {
return alisverisSepeti.getUrunler();
}
public void setUrunler(List<Urun> urunler) {
this.urunler = urunler;
}
public Urun getUrun() {
return urun;
}
public void setUrun(Urun urun) {
this.urun = urun;
}
public void sepeteKoy(){
alisverisSepeti.urunEkle(urun);
urun=new Urun();
}
}
public class Urun {
String urunAdi;
Long fiyat;
Long gramaj;
public Long getFiyat() {
return fiyat;
}
public void setFiyat(Long fiyat) {
this.fiyat = fiyat;
}
public Long getGramaj() {
return gramaj;
}
public void setGramaj(Long gramaj) {
this.gramaj = gramaj;
}
public String getUrunAdi() {
return urunAdi;
}
public void setUrunAdi(String urunAdi) {
this.urunAdi = urunAdi;
}
}
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:f="http://java.sun.com/jsf/core">
<h:head>
<title>Alışveriş Sepeti</title>
</h:head>
<h:body>
Alışveriş Sepeti
<h:form>
<h:panelGrid columns="2">
<h:outputLabel value="Ürün adı : "/>
<h:inputText value="#{bean.urun.urunAdi}"/>
<h:outputLabel value="Ürün fiyatı : "/>
<h:inputText value="#{bean.urun.fiyat}"/>
<h:outputLabel value="ÜRün gramajı : "/>
<h:inputText value="#{bean.urun.gramaj}"/>
</h:panelGrid>
<h:commandButton action="#{bean.sepeteKoy}" value="Sepete Ekle"/>
<br><h:outputLabel value="Sepetteki Ürünler"/></br>
<h:dataTable value="#{bean.urunler}" var="item" border="1">
<h:column >
<f:facet name="header">
<h:outputLabel value="Ürün adı"/>
</f:facet>
<h:outputText value="#{item.urunAdi}"/>
</h:column>
<h:column >
<f:facet name="header">
<h:outputLabel value="Ürün fiyatı"/>
</f:facet>
<h:outputText value="#{item.fiyat}"/>
</h:column>
<h:column >
<f:facet name="header">
<h:outputLabel value="Ürün gramajı"/>
</f:facet>
<h:outputText value="#{item.gramaj}"/>
</h:column>
</h:dataTable>
</h:form>
</h:body>
</html>
It's quite possible that the same instance of AlisverisSepetiEJB is being shared between invocations (the container can do this, it's perfectly legal). Precisely because of the stateless nature of a #Stateless EJB, you should not declare instance attributes in it!
Remove the List<Urun> urunler = new ArrayList<>(); declaration and all associated methods, think of a stateless bean as a class where all its methods are static - meaning, that they can't use any instance attributes and must be completely independent of instance data; if any state needs to be maintained between calls, then it must be passed along as parameters from the point where the EJB is being invoked. The only instance variables allowed in a #Stateless EJB are attributes which are themselves stateless and/or immutable.
If you need to maintain the state of the shopping cart between invocations, you'll have to keep it elsewhere in a stateful context (say, the Session context of a web application) or mark the EJB as #Stateful.
I have a (request-scoped) list from which the user may select a "PQ" (list of links). When clicked or otherwise entered into the browser the main page for each PQ shall be displayed. Each PQ's page is of the form
http://localhost:8080/projectname/main.jsf?id=2
Here's the PQ bean first:
#Named
#ViewScoped
public class PqHome implements Serializable
{
#PersistenceContext(unitName="...")
private EntityManager em;
private Integer id;
private PQ instance;
#PostConstruct
public void init()
{
System.out.println("ID is " + id); // ID from URL param
instance = em.find(PQ.class, id);
}
public Integer getId()
{
return id;
}
public void setId(Integer id)
{
this.id = id;
}
public PQ getInstance()
{
return instance;
}
}
Here's the main.xhtml:
<ui:composition xmlns="http://www.w3.org/1999/xhtml"
...>
<ui:define name="metadata">
<f:metadata>
<f:viewParam name="id" value="#{pqHome.id}">
<f:convertNumber integerOnly="#{true}" />
</f:viewParam>
<!--f:event type="preRenderView" listener="#{pqHome.init}" /-->
</f:metadata>
</ui:define>
<ui:define name="title">
<h:outputText value="Main" />
</ui:define>
...
</ui:composition>
Any time I select or otherwise refresh the page/URL I get a NullPointerException from the EntityManager:
org.jboss.weld.exceptions.WeldException: WELD-000049 Unable to invoke [method] #PostConstruct public de.mycomp.myproj.beans.PqHome.init() on de.mycomp.myproj.beans.PqHome#4f0ea68f
at org.jboss.weld.bean.AbstractClassBean.defaultPostConstruct(AbstractClassBean.java:595)
...
Caused by: java.lang.IllegalArgumentException: id to load is required for loading
at org.hibernate.event.spi.LoadEvent.<init>(LoadEvent.java:87)
at org.hibernate.event.spi.LoadEvent.<init>(LoadEvent.java:59)
at org.hibernate.internal.SessionImpl.get(SessionImpl.java:961)
at org.hibernate.internal.SessionImpl.get(SessionImpl.java:957)
at org.hibernate.ejb.AbstractEntityManagerImpl.find(AbstractEntityManagerImpl.java:787)
at org.hibernate.ejb.AbstractEntityManagerImpl.find(AbstractEntityManagerImpl.java:762)
at org.jboss.as.jpa.container.AbstractEntityManager.find(AbstractEntityManager.java:221)
at de.mycomp.myproj.beans.PqHome.init(PqHome.java:47)
... 56 more
[Line 47 is em.find(...)]
The line
<f:event type="preRenderView" listener="#{pqHome.init}" />
doesn't make things any better. I'm pretty desparate now.
How do you get URL GET request params into an #ViewScoped bean?
Note: I bet it's not a trivial thing to do. Chances are I'm doing something wrong here conceptually, so any tips on how to improve are welcome. I felt that I needed to choose #ViewScoped because there will be more complex AJAX-based GUI on that page which I'd really like to keep accessible via URL GET params.
Thanks
There is a better way to get id from url. Just use it in #PostConstruct init() method to get "id" from url:
FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap().get("id");
You can still use ViewScoped and #PostConstruct.
The #PostConstruct is invoked directly after bean's construction and all dependency injection (such as #PersistenceContext, #EJB, #ManagedProperty, #Inject, etc..etc..).
The <f:viewParam> sets its value during the update model values phase, which is far after (post)construction of the bean. So inside the #PostConstruct the <f:viewParam> value is simply not yet been set. It'll be still null at that point.
You're close with <f:event type="preRenderView">, but you have to remove the #PostConstruct annotation.
So:
<f:viewParam name="pq" value="#{pqHome.id}">
<f:convertNumber integerOnly="#{true}" />
</f:viewParam>
<f:event type="preRenderView" listener="#{pqHome.init}" />
with
private Integer id;
public void init() {
instance = em.find(PQ.class, id);
}
Unrelated to the concrete problem, I'd suggest to use a Converter for this instead. See also Communication in JSF 2.0 - Converting and validating GET request parameters.
Also the combination #Named #ViewScoped won't work as intended. The JSF-specific #ViewScoped works in combination with JSF-specific #ManagedBean only. Your CDI-specific #Named will behave like #RequestScoped this way. Either use #ManagedBean instead of #Named or use CDI-specific #ConversationScoped instead of #ViewScoped.