How to show A success message on jsf 2 - jsf-2

How can i make the success message display out? I have the following code, but only the warning message appear. What do i have to change so that the success message will be display out in my success page ?
userbean.java :
public String Login() throws Exception {
String status = "failure";
current=userBo.validateUser(getLogin(),getPass());
if(current!=null){ exist=false; status = "success";
String message = "submitted successfully !!";
FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(message)); }
exist=true; return status; }
login.xhtml :
<f:view>
<h:form>
<table border="0" cellpadding="0" cellspacing="0">
<tr>
<th>Username</th>
<td><h:inputText value="#{user.login}"
styleClass="login-inp" /></td>
</tr>
<tr>
<th>Password</th>
<td><h:inputSecret value="#{user.pass}"
onfocus="this.value=''" styleClass="login-inp" /></td>
</tr>
<tr>
<th></th>
<!-- <td><input type="button" class="submit-login" /> -->
<td><h:commandButton action="#{user.checkUser}" styleClass="submit-login" />
<h:messages globalOnly="true" />
</td>
</tr>
</table>
</h:form>
</f:view>
success.xhtml :
<h:form>
<h:body>
<h1>heyyyy ,y sucseded !!!</h1> <br/><br/>
<h:commandLink action ="#{user.logout}" > disconnect </h:commandLink>
</h:body>
</h:form>
warning message :
Infos: WARNING: FacesMessage(s) have been enqueued, but may not have been displayed.
sourceId=null[severity=(INFO 0), summary=(submitted successfully !!), detail=(submitted successfully !!)]

Your <h:messages globalOnly="true" /> is been placed in the wrong view. You placed it in login.xhtml, but you're navigating to success.xhtml on success instead of returning to login.xhtml. If you put the <h:messages globalOnly="true" /> in success.xhtml, then it will be displayed the way you expected.

on your action method i.e. action="#{user.checkUser}" return a string that string will decide which page to show using your faces-config.xml file.
eg.
< navigation-rule>
< from-view-id>*< /from-view-id>
< navigation-case>
<from-outcome>ReturnedString</from-outcome>
<to-view-id>success.xhtml(path of your file)</to-view-id>
<redirect/>
</navigation-case>
< /navigation-rule>
this will definately work.

Related

rich:popupPanel with <a4j:status> is not working in JSF2

When i hit on Submit button rich popup panel is not working.Should it be outside the h:form or inside . I tried both ways but it is not working. Please help me where i am going wrong .Below is my 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:f="http://java.sun.com/jsf/core"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:fn="http://java.sun.com/jsp/jstl/functions"
xmlns:a4j="http://richfaces.org/a4j"
xmlns:rich="http://richfaces.org/rich">
<h:body>
<ui:composition template="/pages/layout.xhtml">
<ui:define name="heading">
<h:outputText value="Paging” </h:outputText>
</ui:define>
<ui:define name="content">
<h:panelGrid id="panel2">
<h:form id="my_form">
<table>
<tr>
<td>
<h:panelGroup id="globalmessages">
<h:message id="key1" for="key" style="color:red" showDetail="true" />
<h:message id="lock1" for="lock" errorStyle ="color:red" showDetail="true" />
</h:panelGroup>
</td>
</tr>
</table>
<table width="700px" >
<tr>
<td align="right" width="30%"><label>Files: </label></td>
<td align="left" width="70%"><h:outputText value= "Hosting file" /></td>
</tr>
<tr>
<td align="right" width="30%"><label for="lastdate">
Last Date:</label></td>
<td align="left" width="70%"><h:panelGrid id="panel1">
<h:panelGroup>
<rich:calendar id=" lastdate "
value="#{myBean.ladate}"
locale="Locale.US" popup="true" datePattern="MM/dd/yyyy"
showApplyButton="false" todayControlMode = "hidden" cellWidth="24px" cellHeight="220px"
enableManualInput = "true" converterMessage = "Entered wrong date format"
style="width:200px" required="true" requiredMessage="Last date: is required."/>
(MM/DD/YYYY)
</h:panelGroup>
</h:panelGrid>
<h:commandButton value ="getHost" action="#{mybean.hosted}" id="key" />
</td>
</tr>
<tr></tr><tr></tr><tr></tr><tr></tr>
<tr>
<td align="right" width="30%"><label>Data: </label></td>
<td align="left" width="70%"><h:inputText id="datarelated" value="#{myBean.dataExist}" style="padding-bottom: 68px; width: 165px;"/></td>
</tr>
<tr></tr><tr></tr><tr></tr>
<tr>
<td align="right">
<a4j:commandButton value ="SubmitVal" action="#{myBean.onVal}" id="lock" style="height: 25px; width: 90px;"
render="globalmessages" status="ajaxStatus"/>
</td>
</tr>
</table>
</h:form>
<a4j:status name="ajaxStatus"
onstart="#{rich:component('waitPanel')}.show('',{top:'340px', left:'450px',height:'50px', width:'350px'})"
onstop="#{rich:component('waitPanel')}.hide()" >
</a4j:status>
<rich:popupPanel id="waitPanel" style="text-align:center">
<h:form id="test">
<h:outputText value="Please wait...Calculating" style="font-weight:bold;font-size:large" />
</h:form>
</rich:popupPanel>
</h:panelGrid>
</ui:define>
</ui:composition>
</h:body>
</html>

Unable to save dynamic added row

I am adding row dynamicall on click of commandButton.
in which I have two textfield and one dropdown and last column is save.
I am able to save old data which are already in DB,
But unable to save the newly added row. actually control is not reaching to bean when I click on save commandLink.
<div>
<div class="float_right">
<p:commandButton styleClass="add_uom"
value="#{msg['uom.add.new.uom']}" process="#this"
actionListener="#{uomBean.addRow}" immediate="true"
update=":uomFormId:languageDetail">
</p:commandButton>
</div>
<div class="clear"> </div>
<div>
<h:panelGroup id="languageDetail">
<table width="100%" cellspacing="1" cellpadding="1"
border="0" class="role_detail_section">
<thead>
<tr>
<ui:repeat var="language" value="#{uomBean.languageList}">
<th>#{language.languageCode}</th>
</ui:repeat>
<th>#{msg['uom.measuremnet.type.heading']}</th>
<th>#{msg['uom.save']}</th>
<th>#{msg['uom.activate.deactivate.heading']}</th>
</tr>
</thead>
<tbody>
<ui:repeat var="uom" value="#{uomBean.uomDTOList}">
<tr>
<ui:repeat var="uomCountry" value="#{uom.uomList}">
<td><h:inputText styleClass="values"
value="#{uomCountry.languageValue}" /></td>
</ui:repeat>
<td><h:selectOneMenu
value="#{uom.measureTypeId}"
required="true"
requiredMessage="#{msg['uom.measurement.error.msg']}">
<f:selectItem itemLabel="Select" itemValue="0" />
<f:selectItems value="#{uomBean.measureList}"
var="uomtDTO"
itemLabel="#{uomtDTO.measurmentTypeValue}"
itemValue="#{uomtDTO.measurmentTypeId}" />
</h:selectOneMenu></td>
<td>
<p:commandLink value="Saving.." action="#{uomBean.testUOM()}" ></p:commandLink>
</td>
</td>
</tr>
</ui:repeat>
</tbody>
</table>
</h:panelGroup>
<h:messages globalOnly="true" />
</div>
</div>
UOMDTO is
private Country country;
private Language language;
private String uomDisplayName;
private String uomDescription;
private List<UOMDetailDTO> uomList;
private Integer measureTypeId;
and UOMDetailDTO is
private String languageName;
private String languageCode;
private String languageValue;
Would be nice to see the source of uomBean.
I can only guess, but if uomBean is request scoped, the new row has been "lost" by the time you click save and thus cannot be saved. If uomBean is view or session scoped, then I would guess that you somehow reset/reload your uomDTOList, loose the new row and get the same problem as if it was request scoped.

ViewScoped bean losing its state after clicking a commandLink

I'm using Icefaces 3.2 along with JSF 2.1.8. I have the below views included in the main xhtml page. It looks like myViewBean which is #ViewScoped is losing it's state after the link myLink is clicked. The actionListener has logic to get the myViewBean and set the required list of items. During the render response phase, the myViewBean seems to have empty empList. If the #ViewScoped is changed to #SessionScoped, it works in the expected way, but I have 10 other beans all of which need to be changed to SessionScoped. What makes the bean lose its state? Or is there any other way to make use of ViewScoped?
main.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:c="http://java.sun.com/jsp/jstl/core"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:ice="http://www.icesoft.com/icefaces/component"
xmlns:ui="http://java.sun.com/jsf/facelets" >
<f:view contentType="text/html">
<h:head>
</h:head>
<h:body id="bodyId" bgcolor="white">
<table border="0" cellpadding="0" cellspacing="0">
<tr>
<td>
<ice:panelGroup>
<ice:form id="mainForm" name="mainForm">
<table>
<tbody>
<tr>
<td>
<ui:include src="myTabs.xhtml"/>
</td>
</tr>
</tbody>
</table>
</ice:form>
</ice:panelGroup>
</td>
</tr>
</table>
</h:body>
</f:view>
</html>
myTabs.xhtml
<f:subview
xmlns:f="http://java.sun.com/jsf/core"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:ice="http://www.icesoft.com/icefaces/component"
xmlns:ui="http://java.sun.com/jsf/facelets">
<div>
<table class="icePnlTbSet" id="myTabSet"
border="0" cellpadding="0" cellspacing="0">
<tbody>
<tr>
<td>
<table cellpadding="0" cellspacing="0">
<tbody>
<tr>
<!-- My Tab 1 -->
<td class="icePnlTb" style="vertical-align: bottom;" title="tab1">
<table cellpadding="0" cellspacing="0">
<tbody>
<tr>
<td class="LftTop"><img alt="" src="/images/spacer.gif "
width="4" height="1" /></td>
<td class="MidTop"><img alt="" src="/images/spacer.gif "
width="4" height="1" /></td>
<td class="RtTop"><img alt="" src="/images/spacer.gif "
width="4" height="1" /></td>
</tr>
<tr>
<td class="LftMid"><img alt="" src="/images/spacer.gif "
width="4" height="1" /></td>
<td class="MidMid">
<ice:commandLink id="myLink" value="MyTab 1" title=""
actionListener="#{mySessionBean.tabChanged}">
<f:param value="0" name="tab" />
</ice:commandLink>
</td>
<td class="RtMid"><img alt="" src="/images/spacer.gif "
width="4" height="1" /></td>
</tr>
<tr>
<td class="LftBtm"><img alt="" src="/images/spacer.gif "
width="4" height="1" /></td>
<td class="MidBtm"><img alt="" src="/images/spacer.gif "
width="4" height="1" /></td>
<td class="RtBtm"><img alt="" src="/images/spacer.gif "
width="4" height="1" /></td>
</tr>
</tbody>
</table>
</td>
<!-- Repeat for n tabs -->
</tr>
</tbody>
</table>
</td>
</tr>
</tbody>
</table>
</div>
<ui:include src="tabcontent1.xhtml" />
</f:subview>
tabcontent1.xhtml
<f:subview
xmlns:f="http://java.sun.com/jsf/core"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:ice="http://www.icesoft.com/icefaces/component"
xmlns:ui="http://java.sun.com/jsf/facelets">
<ice:panelGroup>
<ice:dataTable id="empTable" value="#{myViewBean.empList}"
var="emp">
...
</ice:dataTable>
</ice:panelGroup>
</f:subview>
A #ViewScoped bean will only hold data if you return null or void from action methods (which will navigate back to the same page), once you navigate away from the view the data is lost.
May be you can try #RequestScoped.

How to display contents in reverse order in <h:dataTable>

I am accepting data in Array and displaying it in the same screen using <h:dataTable>.
How can display contents in reverse order, i.e. show the latest entry in the first row?
Below is code can i display nonDox.non_List in reverse order
<h:body>
<h:panelGroup rendered="#{not empty dataBase}">
<h:form id="nonDoxScanForm">
<f:event listener="#{nonDox.validate_AccNo}" type="postValidate" />
<table border="0" class="InnerBox" width="55%">
<tr><th colspan="7" align="Center" class="clsTitle">Non Dox Add</th></tr>
<tr>
<td>Delivery Date</td>
<td >Consginee (F2) </td>
<td>Weight (F3)</td>
<td>SubBranch Code(F4)</td>
<td >Consignment No (F10)</td>
<td align="Center">(F12)</td>
</tr>
<tr>
<td> <p:calendar value="#{nonDox.delDate}" id="fDat" /> </td>
<td><h:inputText size="20" id="fcon" value="#{nonDox.consignee}" /> </td>
<td><h:inputText size="20" id="fweig" value="#{nonDox.weight}" >
</h:inputText> </td>
<td><h:inputText size="20" id="fsub" value="#{nonDox.subBranchCode}" >
<f:validateLength maximum="3"></f:validateLength>
</h:inputText> </td>`enter code here`
<td><h:inputText size="10" id="acno" value="#{nonDox.accNo}" onfocus="this.select()" >
</h:inputText>
</td>
<td> <h:commandButton value="Add" id="fAdd" action="#{nonDox.addAction}" onclick="return validate();" />
</td>
<td> <h:commandButton value="Save" action="#{nonDox.saveAction}" >
</h:commandButton>
</td>
</tr>
<tr>
<td colspan="7">
<h:message for="acno"/>
</td>
</tr>
</table>
<h:dataTable value="#{ nonDox.non_List}" var="o"
styleClass="order-table"
headerClass="order-table-header"
rowClasses="order-table-odd-row,order-table-even-row" width="55%"
>
<h:column>
<f:facet name="header">Del Date</f:facet>
#{o.cor_Date.substring(0, 10)}
</h:column>
<h:column>
<f:facet name="header">consignee</f:facet>
#{o.consignee}
</h:column>
<h:column>
<f:facet name="header">Weight</f:facet>
#{o.weight}
</h:column>
<h:column>
<f:facet name="header">Rate</f:facet>
#{o.rate}
</h:column>
<h:column>
<f:facet name="header">subBranchCode</f:facet>
#{o.subBranchCode}
</h:column>
<h:column>
<f:facet name="header" >POD No</f:facet>
#{o.accNo}
</h:column>
<h:column>
<f:facet name="header">Action</f:facet>
<h:commandLink value="Delete" action="#{nonDox.deleteAction(o)}" />
</h:column>
</h:dataTable>
</h:form>
<h:panelGroup >
<h:panelGroup rendered="#{empty dataBase}">
<h1>Session Expired</h1>
<h:link outcome="Login" target="CommonContent" >Login</h:link>
</h:panelGroup>
</h:body>
If you're using an List as in new ArrayList<Item>(), it's easy: use Collections#reverse():
private List<Item> data;
#PostConstruct
public void init() {
data = service.list();
Collections.reverse(data);
}
// ...
Just reference it in <h:dataTable value="#{bean.data}"> the usual way.
If you're using an array as in Item[], then you'd need to convert it to List first with help of Arrays#asList(), so that you can feed it to Collections#reverse():
private List<Item> data;
#PostConstruct
public void init() {
Item[] items = service.array();
data = Arrays.asList(items);
Collections.reverse(data);
}
// ...
Or just rewrite the service method in such way that you don't need to perform this conversion step in the backing bean.
Key is, you shouldn't expect to perform this job in the view. You should just prepare the model in such way that it's exactly as what the view expects. The <h:dataTable> itself doesn't provide any facilities like that.

p:commandButton does not work in the last ajax request

I have to Tab in selectoneButton as "request" and "archive". In that tab I have information that is called with ajax. In that information I need to have a button As "acceptCommonButton" and "noNowCommonButton" so when a User clicks on the "acceptCommonButton" button it calls the "acceptRequest" method. When a user Clicks on "noNowCommonButton" button it calls to "notNowRequest" method.
My problem is that the Button Works until the last request is in "archive" Tab. It means that when the last request is in "request" tab my Button does not work. it's too Weird!!!!
I think the problem here is caused by DataGrid.
could you please see my Code?!!
<table style="width: 100%">
<tr>
<td>
<p:selectOneButton value="#{inviteRequestManagedBean.filterType}">
<f:selectItem itemLabel="#{inviteRequest_msg.request}" itemValue="request"/>
<f:selectItem itemLabel="#{inviteRequest_msg.archive}" itemValue="archive" />
<f:ajax event="change" render="requestDataGrid" />
</p:selectOneButton>
</td>
</tr>
<tr>
<td>
<p:dataGrid id="requestDataGrid" var="tBusinessPartnerRequestInfo"
value="#{inviteRequestManagedBean.filterBusinessRequest()}" columns="1" rows="22" >
<p:column>
<div>
<table border="0" width="100%">
<tr>
<td>
<p:graphicImage value="#{tBusinessPartnerRequestInfo.partySender_imageUrl}"/>
</td>
<td>
<div>
<table border="0" width="100%">
<tr>
<td>
<h:outputLabel value="#{tBusinessPartnerRequestInfo.requestDate}"/>
</td>
</tr>
<tr>
<td>
<h:outputLabel value="#{tBusinessPartnerRequestInfo.partySender_fullName}"/>
</td>
</tr>
</table>
</div>
</td>
<td>
<p:commandButton id="acceptCommonButton" value="#{inviteRequest_msg.accept}"
actionListener="#{inviteRequestManagedBean.acceptRequest(tBusinessPartnerRequestInfo.id)}"
update="#form" process="#form">
</p:commandButton>
</td>
<td>
<p:commandButton id="noNowCommonButton" value="#{inviteRequest_msg.notnow}"
actionListener="#{inviteRequestManagedBean.notNowRequest(tBusinessPartnerRequestInfo.id)}"
update="#form" process="#form">
</p:commandButton>
</td>
<td>
<p:panel>
<p:ajaxStatus>
<f:facet name="start">
<p:graphicImage value="../resources/img/loading.gif"/>
</f:facet>
<f:facet name="complete">
<h:outputLabel value=""/>
</f:facet>
</p:ajaxStatus>
</p:panel>
</td>
</tr>
</table>
</div>
<hr/>
</p:column>
</p:dataGrid>
<p:panel>
</p:panel>
</td>
</tr>
</table>

Resources