JSF condition in xhtml with AJAX alternative to JSTL - ajax

What is alternative to JSTIL in JSF. I would like to have a condition in xhtlm in AJAX.
<p:commandButton id="button" value="Send">
<f:ajax event="click" listener="#{messageControler.wniosekWyslany()}"/>
<f:ajax event="click" listener="#{messageControler.wniosekNieWyslany()}"/>
</p:commandButton>
First ajax should be action when boolean = true, secound false. Me bean return boolean value, so it is good to base on that but I do not know how. I teste JSTL but in JSF it does not works correctly.

This is businesslogic and should not be done in the view. Create one ajax listener and do the if/then in the bean
xhtml:
<p:commandButton id="button" value="Send">
<f:ajax event="click" listener="#{messageControler.doSend()}"/>
</p:commandButton>
bean:
public void doSend() {
if (myBoolean) {
wniosekWyslany();
else {
wniosekNieWyslany();
}
}

Related

Access all ajax UIComponents passed by execute attribute in backing bean?

The following code is a simple form used with ajax component:
<h:form id="loginform">
<h:inputText id="username">
<f:ajax event="blur" execute="loginform:username loginform:loginbutton" ... />
</h:inputText>
<h:inputText id="password">
<f:ajax event="blur" execute="loginform:password loginform:loginbutton" ... />
</h:inputText>
<h:commandButton id="loginbutton" value="login" action="#{indexBean.authentication()}" />
</h:form>
I understand that execute attribute is a space-separated List of IDs for components that should be included in the Ajax request.
I can access the source component at backing bean with AjaxBehaviorEvent.getComponent() method, but how to access the rest of components?
It's available by PartialViewContext#getExecuteIds().
FacesContext context = FacesContext.getCurrentInstance();
Collection<String> executeIds = context.getPartialViewContext().getExecuteIds();
// ...
You can then use UIViewRoot#findComponent() to find component by client ID.
for (String executeId : executeIds) {
UIComponent component = context.getViewRoot().findComponent(executeId);
// ...
}

JSF Ajax CommandButton different level

I have an ajax request which outputs me a dataTable and form each row i also want to execute a button.Sadly no event gets invoked and i think it is because the second button is on a different level than the first search button.
JSF looks something like that:
<h:from>
<h:inputText id="search"
value="#{profileController.searchName}"/>
<h:commandButton value="Search by name"
action="#{profileController.searchProfileWithName(profileController.searchName)}">
<f:ajax execute="search"
render="output">
</f:ajax>
</h:commandButton>
<h:dataTable id="output"
value="#{profileController.searchResultList}"
var="p">
<h:column>
<f:facet name="header">Name</f:facet>
<h:outputText value="#{p.name}"/>
</h:column>
<h:column>
<f:facet name="header">Action</f:facet>
<h:commandButton value="Invite"
action="#{trainingController.inviteProfile(p)}">
<f:ajax/>
</h:commandButton>
</h:column>
</h:dataTable
</h:form>
JSF Managed Bean (requestScoped):
public String searchProfileWithName(String name) {
searchResultList = profileBean.findProfilesWithName(name);
return null;
}
ProfileBean (stateless):
public List<Profile> findProfilesWithName(String name) {
Query query = em.createNamedQuery("findProfilesWithName");
query.setParameter("name", "%" + name.replace(" ", "%") + "%");
return query.getResultList();
}
Anyone ideas in this?
The answer to this problem is the datasource. this is the link to the problem:
h:commandButton not working inside h:dataTable

Primefaces CommandButton not working inside DataTable

I have a primefaces commandButton outside of a data table which functions properly. Inside a dataTable, I have the exact same commandButton, but when the one inside the dataTable is clicked, it doesn't call the actionListener:
<h:body>
<h:form>
<p:messages autoUpdate="true" showDetail="true" closable="true"/>
<p:accordionPanel >
<p:tab title="Visualize data">
<p:commandButton value="Add Domain" actionListener="#{TripleStoreController.addDomain('Domain')}"
update="DomainSkillTable"/>
<p:dataTable
id="DomainSkillTable"
var="result"
value="#{TripleStoreController.domainResults}"
rowKey="#{result}">
<p:column>
#{result}
</p:column>
<p:column>
<p:commandButton value="Add Domain" actionListener="#{TripleStoreController.addDomain('Domain')}"
update="DomainSkillTable"/>
</p:column>
</p:dataTable>
</p:tab>
</p:accordionPanel>
</h:form>
</h:body>
it should be work.
there is no error, i had just copy and and make back bean for the same method TripleStoreController.addDomain('Domain') in my java class the method signature like public void addDomain(String s) and method worked.
even i got the value pass by command button as 'Domain' in my back bean.
in my case, i just made my bean from #RequestScoped to #ViewScoped and it solved my problem.

Invoke aciton method with input value as parameter

I have this view:
<h:form id="carlin">
<h:outputText id="carlinInput" value="#{userBean.model.varAjax}"/>
<a class="btn" data-toggle="modal" href="#myModal" >Launch Modal</a>
</h:form>
And I have a modal dialog that shows another form:
<h:form>
<h:inputText value="#{userBean.model.varAjax}"/>
<h:commandLink action="#{userBean.processPage1()}" value="Ok">
<f:ajax render=":carlin:carlinInput" />
</h:commandLink>
</h:form>
I need to set the value typed on the <h:inputText> and pass it as a parameter to my <h:commandLink action="#{userBean.processPage1()}" value="OK">`
Here is my processPage1() method:
public void processPage1(String zip) {
this.model.varAjax = zip;
}
I have tried this:
<h:commandLink action="#{userBean.processPage1(userBean.model.varAjax)}" value="OK">
But it doesn't work. If I pass a hardcoded value, it works:
<h:commandLink action="#{userBean.processPage1('teste')}" value="OK">
But I need to pass what the user typed on that inputText to my action method. How can I achieve this?
This is not the right approach. You need to specify the client IDs of the input components which you need to process/execute by the execute attribute of <f:ajax>.
<h:form>
<h:inputText id="varAjax" value="#{userBean.model.varAjax}"/>
<h:commandLink action="#{userBean.processPage1}" value="Ok">
<f:ajax execute="varAjax" render=":carlin:carlinInput" />
</h:commandLink>
</h:form>
This way JSF will set the model value with the submitted value.
public void processPage1() {
System.out.println(model.getVarAjax()); // Look, JSF has already set it.
}
An alternative is to use execute="#form" which will process the entire form and this is what you usually need in standard ajaxified buttons.
<f:ajax execute="#form" render=":carlin:carlinInput" />
Note that componenent libraries like PrimeFaces and RichFaces have already made the #form the default execute attribute, so you don't need to explicitly specify it in their command components. In standard <f:ajax> it namely defaults to #this in command components which is indeed unintuitive.

Reloading div using Ajax

I have found this topic, where's a bit of code to reload a div using Ajax.
I've tried creating such example, but not successfully.
Here's my code:
index.xhtml
<h:form>
<f:ajax render=":content">
<h:commandButton value="News" action="#{navigationBean.requestPage}">
<f:param name="requestedPage" value="news.xhtml"/>
</h:commandButton>
</f:ajax>
<br/><br/>
<f:ajax render=":content">
<h:commandButton value="News" action="#{navigationBean.requestPage}">
<f:param name="requestedPage" value="games.xhtml"/>
</h:commandButton>
</f:ajax>
</h:form>
</h:panelGroup>
<h:panelGroup id="content" layout="block">
<ui:include src="#{navigationBean.viewedPage}"/>
</h:panelGroup>
backing bean
#ManagedBean
#ViewScoped
public class NavigationBean
{
private String viewedPage;
public NavigationBean()
{
viewedPage = "news.xhtml";
}
public String getViewedPage()
{
return viewedPage;
}
public void setViewedPage(String viewedPage)
{
this.viewedPage = viewedPage;
}
public void requestPage()
{
Map<String,String> map = FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap();
viewedPage = map.get("requestedPage");
}
}
how it looks:
So what am I missing? Variable viewedPage is updated, normal labels/texts etc. are updated, but can't get this div working.
Found the solution:
<h:commandButton value="Games" action="#{navigationBean.requestPage}">
<f:param name="requestedPage" value="games.xhtml"/>
<f:ajax execute="#this" render="#none" />
<f:ajax render=":content"/>
</h:commandButton>
plus changing #ViewScoped to #SessionScoped.

Resources