javax.faces.view.facelets.TagAttributeException - jsf

I'm creating a small application using JSF,facing the exception, below is the code for the form.
<h:form>
<h:selectManyCheckbox value="#{transferMB.selectedItems}">
<f:selectItem itemValue="1" itemLabel="Transfer Status" />
<p:ajax update=":transForm" />
</h:selectManyCheckbox>
</h:form>
exception " javax.faces.view.facelets.TagAttributeException:"

This is a follow-up for issue while loading data
As mentioned there, you need to use EL 2.2 for passing parameter inside EL-Expressions. Seems you don't use that.
So we need another way to get this solved:
<h:form id="transForm">
<p:panelGrid columns="1" rendered="#{transferMB.transFormEnabled}">
<h:outputText value="transForm"/>
</p:panelGrid>
</h:form>
<h:form id="spreadForm">
<p:panelGrid columns="1" rendered="#{transferMB.spreadFormEnabled}">
<h:outputText value="spreadForm"/>
</p:panelGrid>
</h:form>
and in your corresponding bean:
public boolean isTransFormEnabled() {
if (selectedItems.contains("1")) {
return true;
} else {
return false;
}
}
public boolean isSpreadFormEnabled() {
if (selectedItems.contains("2")) {
return true;
} else {
return false;
}
}

You should be calling Boolean-like values into your rendered attributes.
Example:
<p:panelGroup id="group" rendered="{#bean.isOneSelected}">
...
</p:panelGroup>
<p:panelGroup id="group" rendered="{#bean.isTwoSelected}">
...
</p:panelGroup>
And for getting values from selectManyMenu. Please check here.
Also the rendered tag is a Boolean like condition to tell whether a tag should be displayed. You shouldn't need to pass a parameter into that. You should be getting that information from you transferMB

Related

Ajax not function on first submit, after page refresh only result will refresh

I am using Java EJB with JSF.
What I doing is just a simple search result from database and display it on a table using JSF with ajax. But why when first submitting, the result is not appear. And second time only the result will appear.
Below is my JSF page code
<h:form id="wholeForm">
<p:panelGrid id="resultTable" styleClass="result_table1" >
<p:row>
<p:column>
<div align="center" id="font-size1">
<h:selectOneRadio value="#{scanResult.typeOfScan}">
<f:selectItem itemValue="Carrier" itemLabel="Carrier"></f:selectItem>
<f:selectItem itemValue="Slot" itemLabel="Slot"></f:selectItem>
<f:selectItem itemValue="HD" itemLabel="HD"></f:selectItem>
<p:ajax event="change"></p:ajax>
</h:selectOneRadio>
Scan: <p:inputText styleClass="searchField" id="counter" value="#{scanResult.serialNumber}" a:autofocus="true">
<p:ajax event="keydown" update="wholeForm" onstart="if (event.keyCode != 13) { return false;}" listener="#{scanResult.checkResult()}" />
</p:inputText>
<br/><br/>
</div>
</p:column>
</p:row>
</p:panelGrid>
<p:panelGrid id="resultTable1" styleClass="result_table1" rendered="#{not empty scanResult.scanResultCarrier}" >
<c:forEach items="#{scanResult.scanResultCarrier}" var="result" >
<!-- ..do something and call out result -->
</p:column>
</p:row>
</c:forEach>
</p:panelGrid>
</h:form>
And my managed bean is as below
#Named(value = "scanResult")
#SessionScoped
public class scanResult implements Serializable {
//Some code here
public void checkResult() {
scanResultCarrier = new ArrayList<>();
scanResultSlot = new ArrayList<>();
System.out.println("checking");
if (typeOfScan.equals("Carrier")) {
System.out.println("Serial " + serialNumber);
scanResultCarrier = scanresultcarrierFacade.searchResultCarrier(serialNumber);
System.out.println(scanResultCarrier.size());
} else if (typeOfScan.equals("Slot")) {
scanResultSlot = scanresultslotFacade.searchResultSlot(serialNumber);
} else if (typeOfScan.equals("HD")) {
} else {
}
serialNumber = "";
}
}
I use System.out.println(lsitofresult.size()); to print out my result and the result is not blank. Mean that I successful retrieve my result from database.
But my result table not able to show out after I click on enter.
Then I notice that my url is as below
http://localhost:8080/outgoingScanSystem-war/faces/index.xhtml;jsessionid=8b6cefa932ff60984607ee38ec13
And after I refresh my page, the result will appear again.
And my url change to :
http://localhost:8080/outgoingScanSystem-war/faces/index.xhtml
May I know why? Is it related to URL? I have no idea where should I start my troubleshoot. Anyone can give me some guideline?
I see some problems here. First of all, you're using the session scope while your managed bean should be view scoped (there's no reason to use the session here, see the link below). That's the cause why your results are getting displayed when you refresh the page.
Second, I'd rather put the search results out from the form. I see no reason for them to be in the same form of the search itself. Then, when performing a searh you should only update the result list:
<p:ajax event="keydown" update="resultTable1" onstart="if (event.keyCode != 13) { return false;}" listener="#{scanResult.checkResult()}" />
Third, as you're using Primefaces, I encourage you to take a look to its p:remoteCommand tool in order to perform ajax-based calls to the beans from your JS code directly. This way you should avoid your second problem, which seems to be that you're preventing the standard form sending on the ajax start event (which might not even get called). You could do something like this:
<p:remoteCommand name="search" update="resultTable1" actionListener="#{scanResult.checkResult}" />
<p:inputText styleClass="searchField" id="counter" value="#{scanResult.serialNumber}"
a:autofocus="true" onkeypress="if (event.keyCode != 13) {search(); return false;}">
<!-- Update the input text value in the bean for each pressed key -->
<p:ajax event="keydown" />
</p:inputText>
See also:
How to choose the right bean scope?
Jsf calling bean method from input text when pressing enter

Conditionally rendering a primefaces commandlink

I'm trying to conditionally render a commandlink. However I discovered when debugging the bean function stocksView.hasPortfolios is never called. Is this the correct way to user render or should I be doing this in another way?
<h:form id="form">
<p:dataGrid id="prices" var="orderBooks" value="#{stocksView.latestPricesResults}" columns="3" rows="12">
<f:facet name="header">
WST 100
</f:facet>
<p:column>
<p:panel header="#{orderBooks.bidOrderId.member.memberId}">
<h:panelGrid columns="1">
<h:outputText value="#{orderBooks.price}" />
<h:outputText value="#{orderBooks.bidOrderId.member.party}" />
<h:outputText value="#{orderBooks.lastUpdate}" />
<p:commandLink update=":form:buyDetail" oncomplete="PF('buyDialog').show()" title="View Detail">
<h:outputText value="Buy"/>
<f:setPropertyActionListener value="#{orderBooks}" target="#{stocksView.selectedStock}" />
</p:commandLink>
<p:commandLink update=":form:sellDetail" oncomplete="PF('sellDialog').show()" title="View Detail" rendered="#{stocksView.hasPortfolios[orderBooks.bidOrderId.member.memberId]}">
<h:outputText value="Sell"/>
<f:setPropertyActionListener value="#{orderBooks}" target="#{stocksView.selectedStock}" />
</p:commandLink>
</h:panelGrid>
</p:panel>
</p:column>
</p:dataGrid>
</h:form>
This is the hasPortfolios method in StocksView (which is a ViewScoped Bean). The hasPortfolios variable is private Map hasPortfolios;
public void hasPortfolios(){
hasPortfolios = new HashMap<Boolean,String>();
Collection<Portfolio> portfolios = user.getTrader().getPortfolios();
Iterator<Portfolio> iter = portfolios.iterator();
while(iter.hasNext()){
Portfolio p = iter.next();
if(p.getBalance() == 0){
hasPortfolios.put(false, p.getMember().toString());
logger.log(Level.INFO, "hasPortfolios false");
}
else
{
hasPortfolios.put(true,p.getMember().toString());
logger.log(Level.INFO, "hasPortfolios true");
}
}
}
To my eyes you should call the function like this instead of using square brackets:
rendered="#{stocksView.hasPortfolios(orderBooks.bidOrderId.member.memberId)}
Also make sure its public and there is not overloaded version of it.
As usual I was doing a few things wrong. Firstly my map returns a Boolean object in the rendered attribute, and this has to be a boolean primitive. So to get around that I call booleanValue()
rendered="#{stocksView.hasPortfolios[orderBooks.bidOrderId.member.memberId].booleanValue()}"
Also I had my map key/value in the wrong order so the Boolean was actually the key instead of the Member ID.
And finally, I was calling getMember().toString() which returns the Member object as a String, rather than the actual Member ID string which is returned by calling getMember().getMemberId(). So hasPortfolios should actually look like this:
public void hasPortfolios(){
logger.log(Level.INFO, "hasPortfolios start");
hasPortfolios = new HashMap<String,Boolean>();
Collection<Portfolio> portfolios = user.getTrader().getPortfolios();
Iterator<Portfolio> iter = portfolios.iterator();
while(iter.hasNext()){
Portfolio p = iter.next();
if(p.getBalance() == 0){
hasPortfolios.put(p.getMember().getMemberId(),new Boolean(false));
logger.log(Level.INFO, "hasPortfolios false");
}
else
{
hasPortfolios.put(p.getMember().getMemberId(),new Boolean(true));
logger.log(Level.INFO, "hasPortfolios true");
}
}
}
The Sell commandlink is now rendered when there is a portfolio for the user.
Thanks for your help everyone, it pointed me in the right direction.

How to avoid exception, <c:if> and <c:catch>

I'm trying to render a page, but there is the possibility that some EL Expressions will generate some exceptions.
So I tryed first of all, to sorround by block with a < c:if> in such the way that it shows the code only by checking the critical condition.
But I saw that in case of "error" my page was redirected again as a "HTTP 500 - Internal Server Error".
So I tought that maybe the EL Expression inside the block where computed in any case, also if the < c:if> block where not shown. So I sorrounded my block with a < c:catch> as i read that this block will catch the exceptions. So I also added on all the methods the declaration "throws Exception".
But, again, when the critical condition is not respected, my page will be redirect to the 500 Error page.
I post my XHTML code:
<c:if test="#{!partyBean.emptySet}">
<c:catch>
<p:panel id="panel" style="margin-bottom:10px;">
<f:facet name="header" >
<h:outputLabel value="#{partyBean.currentparty.name}" />
</f:facet>
<f:facet name="actions">
<p:commandButton id="asktojoin" styleClass="ui-panel-titlebar-icon "
action="#{joinRequestBean.askForParty(partyBean.currentparty)}" value="Ask to Join"/>
</f:facet>
<f:facet name="footer" >
<p:commandButton id="pr" action="#{partyBean.previus()}" value="previousParty" rendered="#{partyBean.hasPrevious()}">
</p:commandButton>
<p:commandButton id="nx" style="margin-right:10px" action="#{partyBean.next()}" value="nextParty" rendered="#{partyBean.hasNext()}">
</p:commandButton>
</f:facet>
<h:panelGrid columns="2">
<h:outputLabel value="Symbol:" />
<h:graphicImage value="#{('/partysymbols/'.concat(partyBean.currentparty.symbol))}" width="200" height="171" />
<h:outputLabel for="program" value="Program:" />
<h:outputLabel id="program" value="#{partyBean.currentparty.program}" />
<h:link id="partyname" outcome="memberlist" value="memberlist">
<f:param name="partyname" value="#{partyBean.currentparty.name}" />
</h:link>
</h:panelGrid>
</p:panel>
</c:catch>
</c:if>
<c:if test="#{partyBean.emptySet}">
<h1>There are no parties at the moment</h1></c:if>
</h:form>
</h:body>
And my bean:
#ManagedBean(name="partyBean")
#SessionScoped
public class PartyBean {
#EJB
private PartyManagerLocal ejb;
private PartyDTO[] party;
int i=0;
#PostConstruct
private void init() {
i=0;
refresh();
}
private void refresh(){
Object[] list_o = ejb.getListOfParty().toArray();
party = new PartyDTO[list_o.length];
for(int i=0;i<list_o.length;i++){
party[i]=(PartyDTO)list_o[i];
}
if(i>=list_o.length)
i=list_o.length;
}
public boolean isEmptySet() {
refresh();
return party.length==0;
}
public String next() throws Exception{
refresh();
if(i<party.length-1)
i++;
return "partyview.xhtml?faces-redirect=true";
}
public String previus() throws Exception{
refresh();
if(i>0)
i--;
return "partyview.xhtml?faces-redirect=true";
}
public boolean hasPrevious(){
return i>=1;
}
public boolean hasNext(){
return i<party.length-1;
}
public PartyDTO getCurrentparty() throws Exception{
return party[i];
}
}
I apologize to show to you my whole code, but i have no idea about where is the mistake. Then I also apologize about my horrible code, but at the moment i just need that it works.
The critical condition is that my array should be not empty. Could happen that my Array will be empty but in that case, i need to advise the user about it, instead redirect to the 500 Error page.
Thankyou in advance,
Samuele
You could encapsulate what you want conditionally by surrounding the code in a panelGroup and using the rendered attribute.
Example
<h:panelGroup rendered="#{PartyBean.isEmptySet}">
<!-- Code inside here... -->
<!-- This will render if isEmptySet returns true -->
</h:panelGroup>

jsf inputText only displayed value when is readonly true or outputText

I have a h:inputText with valueChangeListener, when the user type some code another h:inputText display data from MySQL about that code, the valueChangeListener works but the second h:inputText not displayed the value and only do it when I set the readonly attribute or I change the component to h:outputText
my facelets page is:
<h:form id="idFacturacion">
<rich:panel>
<f:facet name="header">
<h:outputText value="FACTURACION AL CLIENTE" />
</f:facet>
<h:panelGrid columns="4">
<h:outputText value="Cedula: " />
<h:inputText value="#{facturaBean.encFactura.cedula}" onchange="submit();" valueChangeListener="#{facturaBean.processValueChange}" />
<h:outputText value="Nombre: " />
<h:inputText value="#{facturaBean.encFactura.nombre_cli}" />
</h:panelGrid>
</rich:panel>
</h:form>
facturaBean is:
#ManagedBean
#SessionScoped
public class FacturaBean {
private EncFactura encFactura = new EncFactura();
//getter and setter
public void processValueChange(ValueChangeEvent event){
String ced = event.getNewValue().toString();
try{
//do the database thing
if(resultSet.next()){
encFactura.setNombre_cli(resultSet.getString("nombre_cli"));
}else{
encFactura.setNombre_cli("");
}
}catch(SQLException error){
facesContext.addMessage(null, new FacesMessage("Hubo un error SQL."));
}
}
}
Please see
Change inputText value from listener method… and
Possible to execute `valueChangeListener` for `p:inputText` without hitting `enter` key?
May I suggest using ajax?
Here is a primefaces example but you could apply to richfaces..
<h:inputText value="#{facturaBean.stringOne}" >
<p:ajax event="change" listener="#{facturaBean.processValueChange}" update="strTwo"/> </h:inputText> <h:outputText value="Nombre: " />
<h:inputText id="strTwo" value="#{facturaBean.stringTwo}" />
</h:panelGrid>
private String stringOne= "";
private String stringTwo= "";
public void processValueChange(){
stringTwo = stringOne;
}
With getters etc.. basically on change, fires off to ajax, you do your database call etc, then it returns the response and updates your other input field, it's a much cleaner way than trying to submit forms etc..
Also are you sure you want session scope?

How to get the selected item from an p:orderList?

I want to get the selected item from an orderList. It seems to me that this functionality is missing. Is there a functionality in PF? Or is there a possible workaround?
I really appreciate your answer!
My technology stack:
Hibernate: 4.0.1.Final
Spring: 3.1.1.RELEASE
Primefaces: 3.5
jsf-version: 2.2.0-m08
PrimefacesMobile-version: 0.9.3
Apache Tomcat/7.0.12
I've resolved this by using a button for delete. The code snippet is below:
<p:orderList id="layersList" value="#{mappingLayerController.layers}"
converter="layerConverter" var="layer" itemValue="#{layer}"
controlsLocation="left">
<p:column>
<h:outputText value="#{layer.layerName}"/>
</p:column>
<p:column style="width: 4%">
<p:commandButton icon="ui-icon-close" actionListener="#{controller.deleteLayer()}">
<f:param name="layerName" value="#{layer.layerName}" />
</p:commandButton>
</p:column>
</p:orderList>
And in the backing bean you can get the supplied parameter via the f:param tag as:
String layerName = FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap().get("layerName");
I hope this helps.
I ran into the same issue trying to create an edit action for each item in my orderList. I tried passing my var to the action, creating an action listener in combination with an attribute, and a select listener all without success.
I ended up using a dataTable ... draggableRows="true" (see data table reorder in showcase). In the data table I could simply pass my var as a parameter to an action... So I thought. I ran into the issue that after reordering, clicking the edit button gave me the wrong item. I created a workaround being that on reorder I update the model and redraw the table. It works, but doesn't feel right.
<p:dataTable id="myTable"
value="#{myBean.myItems}"
var="item"
sortBy="#{item.orderNumber}" sortOrder="ascending"
draggableRows="true">
...
<p:column headerText="Actions">
<p:commandButton title="Edit"
action="#{myBean.edit(item)}"
process="#this">
</p:column>
<p:ajax event="rowReorder"
listener="#{myBean.onSlotContentReorder}"
update="myTable"/>
</p:dataTable>
It is possible in PrimeFaces 5 and higher. There is no out of box feature, but it can be easily achieved with additional list of selected items. See example
Java code
private List<Record> allItems; // with getter and setter
private List<Record> selectedItems; // with getter and setter
public void onSelect(SelectEvent event) {
if (null == event || null == event.getObject()) {
return;
}
if (!event.isCtrlKey()) {
setSelectedItems(new ArrayList<Record>());
}
Record item = (Record)event.getObject();
if (!getSelectedItems().contains(item)) {
getSelectedItems().add(item);
}
}
public void onUnselect(UnselectEvent event) {
if (null == event || null == event.getObject()) {
return;
}
Record item = (Record)event.getObject();
if (getSelectedItems().contains(item)) {
getSelectedItems().remove(item);
}
}
public void deleteSelected() {
if (getAllItems().isEmpty()) {
addErrorMessage("listEmpty");
return;
}
if (getSelectedItems().isEmpty()) {
addErrorMessage("noItemSelected");
return;
}
for (Record item : getSelectedItems()) {
if (getAllItems().contains(item)) {
getAllItems().remove(item);
}
}
}
XHTML
<h:panelGroup id="listGroup">
<p:orderList id="list" value="#{bean.allItems}"
var="item" itemLabel="#{item.code}" itemValue="#{item}"
converter="#{itemConverter}" controlsLocation="none">
<f:facet name="caption">#{msg.listName}</f:facet>
<p:ajax event="select" listener="#{bean.onSelect}" />
<p:ajax event="unselect" listener="#{bean.onUnselect}" />
</p:orderList>
<p:contextMenu id="listMenu" for="list">
<p:menuitem value="#{msg.delete}"
actionListener="#{bean.deleteSelected}"
update="listGroup, messages"
rendered="#{not empty bean.allItems}" />
</p:contextMenu>
</h:panelGroup>

Resources