Access bean inside repeat in jsf [duplicate] - java

This question already has answers here:
commandButton/commandLink/ajax action/listener method not invoked or input value not set/updated
(12 answers)
Closed 9 years ago.
I have a userProfileBean which I can access outside of the repeat but when I do
<ui:repeat id="reqlistID" var="reqlist" value="#{userProfileBean.friendRequestsList}">
jmeno: #{reqlist.jmeno} <br/> prijmeni: #{reqlist.prijmeni} <br/> id: #{reqlist.id} <br/>
<p:commandButton id="allowFriendButton"
action="#{userProfileBean.allowFriend()}"
value="Přidat"
update="reqlistID">
</p:commandButton>
Then the allowFriend method doesn't get called and netbeans says unknown property. Am I missing something?
Edit: thanks for the replies. Still doesn't work. Now the state is this.
<h:form>
<p:commandButton id="allson"
action="#{userProfileBean.testt()}"
value="Přidat"
ajax="false">
</p:commandButton>
</h:form>
<c:if test="#{principalBean.p.login == userProfileBean.name}">
Žádosti přidání do přátel:<br/>
<ui:repeat id="reqlistID" var="reqlist" value="#{userProfileBean.friendRequestsList}">
jmeno: #{reqlist.jmeno} <br/> prijmeni: #{reqlist.prijmeni} <br/> id: #{reqlist.id} <br/>
<h:form>
<p:commandButton id="allowFriendButton"
action="#{userProfileBean.testt()}"
value="Přidat"
ajax="false">
</p:commandButton>
</h:form>
<br/>
</ui:repeat>
</c:if>
The first <p:commandButton> works and calls the method properly (with page reload). The second button reloads the page but does not call the method. The problem seems to be with accessing the userProfileBean from inside the repeat. But I can't google out any explanation.

I guess you're missing the form element. Try putting your buttons in a form like
<h:form>
<ui:repeat id="reqlistID" var="reqlist" value="#{userProfileBean.friendRequestsList}">
jmeno: #{reqlist.jmeno} <br/> prijmeni: #{reqlist.prijmeni} <br/> id: #{reqlist.id} <br/>
<p:commandButton id="allowFriendButton"
action="#{userProfileBean.allowFriend()}"
value="Přidat"
update="reqlistID">
</p:commandButton>
</ui:repeat>
</h:form>

Found the source of the problem, the form is getting regenerated and the bean cannot find the original button element, because it is request scoped. Tried to implement a view scope, but something failed there. So I just made the bean session scoped.

Related

Input fields not emptying after submit and redirect?

When I want to "delete a plane" from my jsf page, if successful the plane deletes, and I do a redirect to the page and show an alert telling the user the plane was succesfully deleted. However, the values the user entered in the h:inputText are still there, they dont empty.
JSF Page:
<h:form>
<h:panelGrid columns="3" class="table">
<h:outputLabel value="Plane ID" />
<h:inputText value="#{listAirplaneBB.airPlaneId}"/>
<h:commandButton value="Delete"
class="btn btn-primary"
action="#{addAirplaneCtrl.deleteAirplane}" />
</h:panelGrid>
</h:form>
Bean code:
public String deleteAirplane(){
//delete the plane
return private/user?faces-redirect=true;
}
Any ideas on why the fields dont empty after redirect?
Maybe your listAirplaneBB is a #ViewScoped bean (cant tell by your example).
In that case you have to clean any particular fields manually before redirecting as JSF keeps alive that bean while the target page has not changed.

form field in jsp not change java variable

I can't change the data information in my jsp page when I submit the form and call a java function(When the button is pressed).
This is a peace of code used in my .jsp form field:
...
<h:form>
<h:panelGrid columns="3">
<h:outputLabel for="contactName" value="#{msj.contactName}"/>
<h:inputText id="contactName" value="#{CustodiaBean.name}" required="true">
<f:validator validatorId = "TextValidator" />
</h:inputText>
<h:message for="contactName" styleClass="errorMessage" style="color:red"/>
<h:outputLabel for="contactLastName" value="#{msj.contactLastName}"/>
<h:inputText id="contactLastName" value="#{CustodiaBean.last_name}" required="true" >
<f:validator validatorId = "TextValidator" />
</h:inputText>
<h:message for="contactLastName" styleClass="errorMessage" style="color:red"/>
</h:panelGrid>
<div id="button">
<h:commandLink styleClass="hoveringLink3doubleGreen" action="#{CustodiaBean.updateAccount}">
<h:outputText id="save_button" value="#{msj.save_changes}"/>
</h:commandLink>
<h:commandLink styleClass="hoveringLink3double" action="myAccountCustodia">
<h:outputText id="cancel_button" value="#{msj.cancel}"/>
</h:commandLink>
</div>
</h:form>
...
This is the same piece of code used in other pages like login and register user.
The form fields get the current values in "CustodiaBean", but i try to change the value in the form and when I press the button to check the value(call a function in "CustodiaBean"), I always get the previous value (The new form value is not changed in my class "CustodiaBean").
I'm using a tomcat server and eclipse.
Is necesary to force the field to be bloqued and save the changes when the button is pressed??
I don't know why this is happen. Can anyone help me or give me some advice??
Thanks a lot in advance
Does h:inputText etc also has a property name? Try to see what html is generated. It may be possible that your data is not getting submitted properly. Any HTML form directly submit feilds which are non disabled with the name property of each field. IF id is present and not name, it wont be submitted correctly. Try debugging at browser level or via some firefox plugin like tamper data.
Hope it helps

p:ajax using an global HTML id to reference to an <h:messages> attribute

I currently working on an project that needs some "rebuild" but still there is no money on this - so I have to deal with that what's in front of me. I need to display an message on every change of the radioButton as you can see in this code example:
<h:form>
<h:messages id="msg"/>
<p:accordionPanel value="#{exerciseBean.getCategoriesInGrade(5)}" var="category">
<p:tab title="#{category.categoryName}">
<h:panelGrid columns="1" cellpadding="5">
<ui:repeat value="#{exerciseBean.getSingleChoiceInGrade(5, category.categoryName)}" var="exercise">
<div>#{exercise.getText()}</div>
<p:selectOneRadio value="#{exerciseBean.data}" >
<f:selectItems value="#{exercise.getChoices()}" var="c" itemLabel="#{c.id}" itemValue="#{c.id}" />
<p:ajax event="change" process="#this" listener="#{exerciseBean.validateInput(exercise.getId())}" update="msg"/>
</p:selectOneRadio>
</ui:repeat>
</h:panelGrid>
</p:tab>
</p:accordionPanel>
</h:form>
As you can see - inside this <h:form> there is directly an <h:messages> with the id msg. Now the problem: I still get the error: "cannot find component with identifier 'msg'". I now tried it with the prefix : but still the same problem. Is there a way to get PrimeFaces/JSF to reference to this "global-id"?
In my backend I send the message like this:
FacesMessage facesMessage = new FacesMessage("Right Answer!");
FacesContext.getCurrentInstance().addMessage(null, facesMessage);
thanks.
You need an id for your form.
You then need to modify the reference to msg in the ajax update attribute.
So the following code change should do it for you.
1.<h:form id="formID" >
2.<p:ajax event="change" process="#this" listener="#{exerciseBean.validateInput(exercise.getId())}" update=":formID:msg"/>

Primefaces / JSF: selectOneMenu value is not set, ajax listener not called

I am using Primefaces and JSF to develop this frontend. My issue is that one of my selectonemenus never sets its value binding, "selectedGroup", so the second dropdown is never populated. My backing bean is being called to "update" the second selectonemenu, but the listener of that ajax is not called, nor is selectedGroup ever set. This code is effectively identical to the Showcase for "Select". I even verified that the showcase code works from scratch (which i did not doubt), but fail to see how my situation is any different from that example.
Other stackoverflow questions on this topic indicate that something was left out, but none of those suggestions matched my issue.
I have two selectOneMenus, like so.
<h:form id="outerForm">
<p:panel id="outerPanel">
<p:panelGrid id="outerPanelGrid">
<h:outputLabel for="groupSelection" value="Group: "/>
<p:selectOneMenu id="groupSelection" value="#{myBean.selectedGroup}" >
<p:ajax update="commandSelection"
listener="#{myBean.handleGroupSelection}" />
<f:selectItem itemLabel="---Please Select Group---" itemValue=""/>
<f:selectItems var="group" value="#{myBean.groups}"
itemLabel="#{group.name}" itemValue="#{group.name}" />
</p:selectOneMenu>
<h:outputLabel for="commandSelection" value="Command: "/>
<p:selectOneMenu id="commandSelection" value="#{myBean.command}">
<f:selectItems value="#{myBean.commandStringsList}"/>
</p:selectOneMenu>
</p:panelGrid>
</p:panel>
</h:form>
This page is being displayed in the "center" portion of my layout template like so..
<ui:define id="content" name="content">
<p:panel id="contentPanel" style="float:left; border:none">
<ui:include src="#{anotherBean.currentView}.xhtml"/>
</p:panel>
</ui:define>
The backing bean DOES use some data classes to contain some of the data which is populated, but I thought i was doing everything correct to map it into the view. For the most part, I am using Strings, though.
Does anyone see what I am missing? At the very least, is this xhtml valid?
I should also mention that this page was working before I created and used a template. Basically, I was rendering it in a tab of a tabview using ui:include in the body of index.xhtml. Though I did not notice initially, this page stopped working sometime after I incorporated the template (poor testing on my part, I know).
<f:selectItems var="group" value="#{myBean.groups}"
itemLabel="#{group.name}" itemValue="#{group.name}" />
you can't specify selectItems this way. translation has to be bidirectional. use a converter!
<f:selectItems var="group" value="#{myBean.groups}"
itemLabel="#{group.name}" itemValue="#{group}"
converter="groupConverter"/>

Two <h:commandButton> in a <h:form>, one work and one doesn't

I'm trying to use one <h:commandButton> with ajax to check some conditions, if it's OK the other <h:commandButton> with no ajax will be display. The one with ajax work well, but the other doesn't although I have specified a return String to another page. I cannot figure out why it failed, the syntaxes are all correct.
<h:form>
<label style="font-weight: bold">Room Code: </label>#{r.code}
<label style="font-weight: bold">Room Type: </label>#{r.roomType}
<label style="font-weight: bold">Price Per Day: </label>#{r.pricePerDay}
<br/>
<h:commandButton value="Check" action="#{bookingController.checkRoom}">
<f:param name="selectedRoomID" value="#{r.id}"/>
<f:ajax execute="#form" render="informer"/>
</h:commandButton>
<h:panelGroup id="informer">
<h:outputText value="#{bookingController.message}" rendered="#{bookingController.checked}"/>
<h:commandButton value="Book now!" action="#{bookingController.doBook}" rendered="#{bookingController.goodToBook}">
<f:param name="selectedRoomID" value="#{r.id}"/>
<f:param name="customerID" value="#{customerLoginController.customerUser.customer.id}"/>
</h:commandButton>
</h:panelGroup>
</h:form>
You've a rendered attribute on the second <h:commandButton>. If it evaluates false during processing of the form submit, then the button's action won't be invoked. This will happen if the bean is request scoped and you don't preserve the same condition for the rendered attribute during the form submit request as it was during displaying the form.
There are several ways to go around this. Since you're using JSF 2.0, best is to put the bean in the view scope instead.
#ManagedBean
#ViewScoped
public class BookingController {
// ...
}
See also:
commandButton/commandLink/ajax action/listener method not invoked or input value not updated

Categories