I have started learning JSF. I understand from oracle fourm like
when the command button has immediate attribute then all its actions, conversions and validations will happen in ApplyRequestPhase for the button.
When a input has immediate attribute then similarly conversion , validation and all things will be happen in ApplyRequest phase for that input.
But my question is like Both command button and an input text has immediate attribute then can i get the value of input text in the ApplyRequest phase when i click the button?
If it is no then why this behavior for inputs ?
If it is yes then that is not working in my sample.
<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">
<h:head>
JSF Immediate Sample
</h:head>
<h:body>
<h:form>
<h:messages globalOnly="true" showDetail="true" showSummary="true"/>
<h:panelGrid border="2" columns="2">
<h:outputText value="Enter Data (Str - Boo)" />
<h:inputText value="#{classicBean.propBoolean}" immediate="true"/>
<h:outputText value="Enter Data" />
<h:inputText value="#{classicBean.propString2}" />
<h:outputText value="Enter Data (Immediate)" />
<h:inputText value="#{classicBean.propString3}" immediate="true"/>
<h:commandButton value="ImmediateButton" action="#{classicBean.testMethod}" immediate="true" />
<h:commandButton value="CommandButton" action="#{classicBean.testMethod}" />
</h:panelGrid>
</h:form>
</h:body>
</html>
public class ClassicManagedBean {
private boolean propBoolean;
private String propString;
private String propString2;
private String propString3;
private String propString4;
private Integer propInteger;
public String testMethod() {
System.out.println("Inside TestMetod......");
System.out.println("Property Boolean :"+propBoolean);
System.out.println("Property String2 :"+propString2);
System.out.println("Property String3 :"+propString3);
return null;
}
}
Log
Coming to RESTORE_VIEW 1 beforePhase
Coming to RESTORE_VIEW 1 afterPhase
Coming to APPLY_REQUEST_VALUES 2 beforePhase
Inside TestMetod......
Property Boolean :false
Property String2 :null
Property String3 :null
Coming to APPLY_REQUEST_VALUES 2 afterPhase
Coming to RENDER_RESPONSE 6 beforePhase
Coming to RENDER_RESPONSE 6 afterPhase
Screenshot & input
Thanks
Manivannan
Related
im using jsf 2.2 majorra and primefaces 5.1 to build a webapp
i know this has probably been answered before, but i dont find my error... i took examples from the primefaces showcase and tried some stuff suggested by the internet, but it doesnt want to work
<?xml version="1.0" encoding="UTF-8"?>
<ui:composition template="/WEB-INF/template/master.xhtml"
xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://xmlns.jcp.org/jsf/html"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:p="http://primefaces.org/ui"
xmlns:pe="http://primefaces.org/ui/extensions"
xmlns:ui="http://xmlns.jcp.org/jsf/facelets">
<ui:define name="content">
<p:selectManyMenu id="userList"
value="#{userAdministrationController.selectedUsers}" var="t"
filter="true" filterMatchMode="contains" showCheckbox="true">
<f:selectItems value="#{userAdministrationController.selectItemList}"
var="user" itemLabel="#{user.fullName}" itemValue="#{user}" />
<p:column>
<h:outputText value="#{t}" />
</p:column>
</p:selectManyMenu>
<p:separator />
<p:commandButton value="Submit" oncomplete="PF('dlg').show()"
icon="ui-icon-check" />
</ui:define>
</ui:composition>
im trying to load this list so that some users are already selected when visiting the page, like a default value
all users show as desired, but no default values are loaded to the checkboxes
thanks in advance
okay now i found the answer:
working code->
<p:selectManyMenu id="userList"
value="#{userAdministrationController.selectedUsers}" var="t"
filter="true" filterMatchMode="contains" showCheckbox="true" >
<f:selectItems value="#{userAdministrationController.selectItemList}"
var="user" itemValue="#{user}" />
<p:column>
<h:outputText value="#{t}" />
</p:column>
</p:selectManyMenu>
without the converter! and in my bean i had to make a List for the preselected values, the stuff in the list has to be the same type as the VALUES of the selectitems
List<User> inactiveUsers = userService.findByActive(false);
List<User> userList = userService.findAll();
selectItemList = new ArrayList<SelectItem>();
for (User user : userList) {
SelectItem selectItem = new SelectItem(user.getFullName(), WordUtils.capitalize(user.getFullName()));
selectItemList.add(selectItem);
}
selectedUsers = new ArrayList<String>();
for (User user : inactiveUsers) {
String userName = user.getFullName();
selectedUsers.add(userName);
}
I created a tagfile for a confirm dialog with a command button:
<?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:c="http://java.sun.com/jsp/jstl/core"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:p="http://primefaces.org/ui"
xmlns:a4j="http://richfaces.org/a4j"
xmlns:aym="http://aym.com/facelets"
xmlns:rich="http://richfaces.org/rich"
>
<h:body>
<ui:composition>
<p:growl id="messages" />
<h:panelGrid columns="1" cellpadding="5">
<p:commandButton value="#{label}" onclick="#{pupupId}.show()" type="button" ajax="false"/>
</h:panelGrid>
<p:confirmDialog message="#{message}"
showEffect="bounce" hideEffect="explode"
header="İşlem Onay" severity="alert" widgetVar="#{pupupId}">
<p:commandButton value="Evet" update="messages" oncomplete="#{pupupId}.hide()" ajax="false"
action="#{actionMethod}" />
<p:commandButton value="Hayır" onclick="#{pupupId}.hide()" type="button" />
</p:confirmDialog>
</ui:composition>
</h:body>
</html>
Here's how I'm using it:
<aym:onayButon id="onay2" label="#{lbl.kaydet}"
actionMethod="#{userMB.addUser}" pupupId="onaylaPopup"
message="#{msg.onay_sonuc}" />
I am passing the action method that I am going to call when I click the button. However, when I do that, then I am getting a PropertyNotFoundException:
WARNING: #{actionMethod}: javax.el.PropertyNotFoundException: /WEB-INF/tags/com/components/onayButton.xhtml #29,33 action="#{actionMethod}": /pages/index.xhtml #33,48 actionMethod="#{userMB.addUser}": Property 'addUser' not found on type com.otv.managed.bean.UserManagedBean
javax.faces.FacesException: #{actionMethod}: javax.el.PropertyNotFoundException: /WEB-INF/tags/com/components/onayButton.xhtml #29,33 action="#{actionMethod}": /pages/index.xhtml #33,48 actionMethod="#{userMB.addUser}": Property 'addUser' not found on type com.otv.managed.bean.UserManagedBean
at com.sun.faces.application.ActionListenerImpl.processAction(ActionListenerImpl.java:118)
It works fine when I hardcode the action method in the tagfile like so:
<p:commandButton ... action="#{userMB.addUser}" />
How is this caused and how can I solve it?
Try as a workaround to give action bean and action method as two separate parameters:
Template
<p:commandButton value="Evet"
update="messages" oncomplete="#{pupupId}.hide()" ajax="false"
action="#{actionBean[actionMethod]}" />
call
<aym:onayButon id="onay2" label="#{lbl.kaydet}"
actionBean="#{userMB}" actionMethod="addUser" pupupId="onaylaPopup"
message="#{msg.onay_sonuc}" />
On the call, the bean needs to be in curly brackets, the method name is only given as string. See also Solution on a similar topic .
Maybe it helps...
I have few password fields and a dropdown box. Whenever drop down value is changed (value change event), I am displaying a popup to agree to Terms&Conditions. As soon as Terms&Conditions are accepted, password fields are cleared.
XHTML file
Enter Password: <h:inputSecret value="#{myBean.password}" />
Confirm Password: <h:inputSecret value="#{myBean.confirmPassword}" />
Select an Amount: <h:selectOneMenu value="#{myBean.amount}">
<f:selectItems value="#{myBean.amountValues}" />
<f:ajax event="valueChange" render="#this">
<rich:componentControl target="pnl" operation="show" />
</f:ajax>
</h:selectOneMenu>
<rich:popupPanel id="pnl" domElementAttachment="form" height="150" width="400" moveable="false" resizeable="false">
<f:facet name="header"><h:outputText value="Do you accept our Terms&Conditions"/></f:facet>
<h:commandButton value="Yes" immediate="true" action="#{myBean.agreed}"/>
<h:commandButton value="No" immediate="true" action="#{myBean.disagreed}" />
</rich:popupPanle>
MyBean
public void agreed() {
//set some variables
}
public void disagreed() {
//set some variables
}
For some reason whole page is getting rendered, since the form is not submitted yet, the password values are not set in the bean. How can I prevent it?
This is the Facelet:
<!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">
<h:head>
<title>Title</title>
</h:head>
<h:body>
<h:form>
<h:commandButton value ="Converter" action ="#{conversorMonetarioBean.converte}"/>
<h:inputText value ="#{conversorMonetarioBean.valor}"/>
<h:outputLabel value ="de" for ="de"/>
<h:selectOneMenu value ="#{conversorMonetarioBean.de}" id="de">
<f:selectItems
value ="#{conversorMonetarioBean.taxas.keySet()}"
var ="moeda"
itemValue ="#{moeda}"
itemLabel ="#{moeda}" />
</h:selectOneMenu>
<h:outputLabel value ="para" for ="para"/>
<h:selectOneMenu value ="#{conversorMonetarioBean.para}" id="para">
<f:selectItems
value ="#{conversorMonetarioBean.taxas.keySet()}"
var ="moeda"
itemValue ="#{moeda}"
itemLabel ="#{moeda}" />
</h:selectOneMenu>
</h:form>
<h:outputFormat value ="{0} em {1} equivale a {2} em {3}" rendered ="#{conversorMonetarioBean.resultado != null}">
<f:param value ="#{conversorMonetarioBean.valor}"/>
<f:param value ="#{conversorMonetarioBean.de}"/>
<f:param value ="#{conversorMonetarioBean.resultado}"/>
<f:param value ="#{conversorMonetarioBean.para}"/>
</h:outputFormat>
</h:body>
</html>
It's saying that f:selectItems is an unknown tag.
What could be wrong ? I'm using JSF2, GlassFish 3.1.2, Eclipse. I tried to ctrl+space and only have h:something tags.
You forgot to import f namespace in your html declaration, xmlns:f="http://java.sun.com/jsf/core", so that the top section will be like:
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:f="http://java.sun.com/jsf/core">
Also, your IDE is possibly showing you a hint that f namespace is missing, so following its instructions to do the import will solve your problem.
I have an issue with view scoped bean. I know it's common question but I didn't get right answer. I have a datatable that is responsible for showing search results upon user entered search some criteria.
Here is xhtml:
<html xmlns:h="http://java.sun.com/jsf/html"
xmlns:f="http://java.sun.com/jsf/core"
xmlns="http://www.w3.org/1999/xhtml"
xmlns:p="http://primefaces.org/ui">
<h:panelGroup id ="adminReportLogList">
<p:dataTable value="#{adminLogView.lazyModelReport}" var="model" id="reportLogList" lazy="true" paginator="true" rows="20" paginatorAlwaysVisible="false" paginatorPosition="bottom" emptyMessage="emppty list" styleClass="dtable" style="table-layout: fixed; width: 100%"
paginatorTemplate="{RowsPerPageDropdown} {FirstPageLink} {PreviousPageLink} {CurrentPageReport} {NextPageLink} {LastPageLink}" rowsPerPageTemplate="5,10,15,20">
<p:column headerText="Report number" sortBy="#{model.reportNumber}">
<h:outputText value="#{model.reportLogId}"/>
</p:column>
<p:column headerText="Report date" sortBy="#{model.reportDate}">
<h:outputText value="#{model.reportDate}">
<f:convertDateTime pattern="yyyy/MM/dd hh:mm:ss"/>
</h:outputText>
</p:column>
<p:column headerText="Search date" sortBy="#{model.searchLogId.searchDate}">
<h:outputText value="#{model.searchLogId.searchdate}">
<f:convertDateTime pattern="yyyy/MM/dd hh:mm:ss"/>
</h:outputText>
</p:column>
<p:column headerText="User name" sortBy="#{model.searchLogId.loginLogId.username}">
<h:outputText value="#{model.searchLogId.loginLogId.username}"/>
</p:column>
<p:column headerText="Customer number" sortBy="#{model.customerId}">
<h:outputText value="#{model.customerId.registernumber}"/>
</p:column>
<p:column headerText="Customer name" sortBy="#{model.customerId}">
<h:outputText value="#{model.customerId.name}"/>
</p:column>
</p:dataTable>
</h:panelGroup>
<br/>
// SEARCH PANEL
<p:panel>
<h:panelGrid columns="3" styleClass="insGrid" id="reportLogSearchPanel">
<h:outputText value="User: "/>
<p:inputText id="reportLogSearchByUserName" value="#{adminLogView.searchUserName}">
<p:watermark for="reportLogSearchByUserName" value ="Customer name"/>
</p:inputText>
<h:message for="reportLogSearchByUserName" id="msgReportLogSearchByUserName" styleClass="errorMessage"/>
<h:outputText value="Customer id number: "/>
<p:inputText id="reportLogSearchByCustomerName" value="#{adminLogView.searchCustomerName}">
<p:watermark for="reportLogSearchByCustomerName" value="Customer id number"/>
</p:inputText>
<h:message for="reportLogSearchByCustomerName" id="msgReportLogSearchBySearchDate" styleClass="errorMessage"/>
<h:outputText value="Report date "/>
<p:inputText id="reportLogSearchByReportDate" value="#{adminLogView.searchReportDate}">
<p:watermark for="reportLogSearchByReportDate" value="Report date YYYY/MM/DD"/>
</p:inputText>
<h:message for="reportLogSearchByReportDate" id="msgReportLogSearchByReportDate" styleClass="errorMessage"/>
<h:panelGroup/>
<h:commandButton value="Search" styleClass="btn" action="#{adminLogView.searchReport()}">
<f:ajax render =":form:adminReportLogList :form:reportLogList" execute=":form:reportLogSearchPanel"/>
</h:commandButton>
</h:panelGrid>
</p:panel>
which is used in tag in another xhtml which is: /I think it's not cause in this case/
<ui:composition xmlns:ui="http://java.sun.com/jsf/facelets"
template="./../templates/admin_main.xhtml"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:p="http://primefaces.org/ui">
<ui:define name="content">
<h:panelGroup id="include">
<ui:include src="#{adminLogView.page}.xhtml"/>
</h:panelGroup>
</ui:define>
</ui:composition>
Here is my bean
#Named(value = "adminLogView")
#ViewScoped
public class AdminLogView implements Serializable{
#EJB
private LogServiceLocal logService;
private List<Reportlog> ReportLogList;
String page = "reportLog";
LazyDataModel<Reportlog> lazyModelReport;
LazyDataModel<Loginlog> lazyModelLogin;
LazyDataModel<Searchlog> lazyModelSearch;
String searchCustomerName;
String searchUserName;
String searchReportDate;
#PostConstruct
public void init(){
this.lazyModelReport = new LazyReportLogDataModel(this.logService);
}
public void searchReport(){
Map<String, String> filter = new HashMap<String, String>();
if(this.searchCustomerName != null && !this.searchCustomerName.isEmpty())
filter.put("customerName", this.searchCustomerName);
if(this.searchReportDate != null && !this.searchReportDate.isEmpty())
filter.put("reportDate", this.searchReportDate);
if(this.searchUserName != null && !this.searchUserName.isEmpty())
filter.put("userName", this.searchUserName);
this.lazyModelReport.load(0, 30, null, SortOrder.UNSORTED, filter);
}
}
When we navigate to a page above then #postConstruct method called multiple times, even with sessionScoped. Even when we click search Button in same view, bean is initialized again. Only RequestScope works fine.
Is there I misunderstand or forgot. PS.
Thanks in advance.
Do you have the correct SessionScoped? javax.enterprise.context.SessionScoped
Do you have Myfaces CODI or any other adapter on the classpath? Otherwise #ViewScoped has undocumented behavior since it is a JSF-2 scope.
If you have the correct #SessionScoped the behavior noted is completely unknown problem for me at least.
Also:
#Named(value = "adminLogView")
the value you give here is what it would default to. Might as well skip setting value like that.
Good luck
#javax.faces.bean.ViewScoped can't work with CDI. for that reason you need to use SeamFaces or MyFaces CODI to benefit from the viewscope services.
CDI offers extensions to create your own scope so you can implement the context and use the #NormalScope to do this.
CDI fires an event AfterBeanDiscovery after each bean call
You can use CDI extension to #Observes this event and add your context implementation
In your scope implementation you can :
Use Contextual to get your bean by its name from FacesContext ViewRoot Map and return it after each ajax call back
Use CreationalContext if the bean name from first step is not found to create it in the FacesContext ViewRoot Map
for a more in-depth explanation i recommand this link : http://www.verborgh.be/articles/2010/01/06/porting-the-viewscoped-jsf-annotation-to-cdi/