I'd recommend that you find another way of accomplishing your task.
Best practice for JSF is to use a single form tag for the entire page.
I then use sandbox:subForm tags to perform selective, partial validation.
On 4/23/07, bajista man <[EMAIL PROTECTED]> wrote:
Hi Guys!
I'm having a strange problem. I have two forms in one page, one that allows
me to insert and edit data and other that shows a list of the data that I
have, the list is scrollable and sorteable and I can click into one item of
the list to fill the main form an edit the data. If I send the fields of the
main form without data I have a validation error because all of them are
marked as required but after that when I click into any of the list items
they are not being shown in the main form but they do in different forms at
the same page.
Does anybody know why is this happening?
Thanks,
Johann
The Managed Bean:
package ventas;
import java.io.Serializable;
import java.util.List;
import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.context.ApplicationContext;
public class Bean implements Serializable {
private static Log log = LogFactory.getLog(Bean.class);
private final static int DEFAULT_ROWS = 5;
public final static int NO_ROW_SELECTED = -1;
private String sortColumn = null;
private boolean sortAscending = true;
private int selectedRowIndex;
private int rowIndex;
private int rowsPerPage = DEFAULT_ROWS;
private transient ItemsDao itemsDao;
private Item item = new Item();
private transient List items;
public Bean() {
setItems(getItems());
}
public Item getItem() {
log.debug("getItem llamado para item [" + item + "]");
return item;
}
public void setItem(Item item) {
log.debug("setItem llamado para item [" + this.item + "] a [" + item +
"]");
this.item = item;
}
public String[] getStatuses() {
return new String[] { "DISPONIBLE", "RESERVADO", "COMPRADO" };
}
//Sorting
public String getSortColumn() {
return sortColumn;
}
public void setSortColumn(String sortColumn) {
this.sortColumn = sortColumn;
}
public boolean isSortAscending() {
return sortAscending;
}
public void setSortAscending(boolean sortAscending) {
this.sortAscending = sortAscending;
}
//Scrolling
public List getItems() {
if (items == null) {
List list = getItemsDao().readAllItems();
if (list != null && list.size() > 0)
items = list;
}
return items;
}
public void setItems(List records) {
setRecords(items, DEFAULT_ROWS);
}
public void setRecords(List items, int rowsPerPage) {
this.items = items;
this.rowsPerPage = rowsPerPage;
}
public void setRowsPerPage(int rowsPerPage) {
this.rowsPerPage = rowsPerPage;
}
public int getRowsPerPage() {
return rowsPerPage;
}
public int getSelectedRowIndex() {
return selectedRowIndex;
}
public void setSelectedRowIndex(int selectedRowIndex) {
this.selectedRowIndex = selectedRowIndex;
this.setRowIndex(selectedRowIndex);
}
public int getRowIndex() {
log.debug("rowIndex: " + rowIndex);
return rowIndex;
}
public void setRowIndex(int rowIndex) {
log.debug("rowIndex1: " + rowIndex);
this.rowIndex = roundDownRowIndex(rowIndex);
log.debug("rowIndex2: " + this.rowIndex);
}
private int roundDownRowIndex(int rowIndex) {
int page = rowIndex / rowsPerPage;
int roundedIndex = page * rowsPerPage;
return roundedIndex;
}
// Actions
public String agregar() {
log.debug("Agregar llamado para item [" + item + "]");
getItemsDao().createItem(item);
addInfo("Item [" + item.getName() + "] creado");
item = new Item();
items = null;
return null;
}
public String actualizar() {
log.debug("actualizar llamado para item [" + item + "]");
getItemsDao().updateItem(item);
addInfo("Item [" + item.getName() + "] actualizado");
item = new Item();
items = null;
return null;
}
public String eliminar() {
log.debug("eliminar llamado para item [" + item + "]");
getItemsDao().deleteItem(item);
addInfo("Item [" + item.getName() + "] eliminado");
item = new Item();
items = null;
return null;
}
public String cargarItem() {
log.debug("cargarItem llamado para item [" + item + "]");
item = getItemsDao().readItem(item.getName());
return null;
}
//Privates
private ItemsDao getItemsDao() {
if (itemsDao == null) {
ApplicationContext ac = (ApplicationContext)
FacesContext.getCurrentInstance
().getExternalContext().getApplicationMap().get(InitServlet.APP_CTX);
itemsDao = (ItemsDao) ac.getBean("itemsDao");
}
return itemsDao;
}
private void addError(String msg) {
FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(
FacesMessage.SEVERITY_ERROR, "", msg));
}
private void addInfo(String msg) {
FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(
FacesMessage.SEVERITY_INFO, "", msg));
}
}
The JSP:
<%@ taglib uri="http://java.sun.com/jsf/html" prefix="h"%>
<%@ taglib uri="http://java.sun.com/jsf/core" prefix="f"%>
<%@ taglib uri="http://myfaces.apache.org/tomahawk" prefix="t"%>
<html>
<head>
<meta HTTP-EQUIV="Content-Type" CONTENT="text/html;charset=UTF-8" />
<title>Sistema Ventas</title>
<link rel="stylesheet" type="text/css" href="<%= request.getContextPath()
%>/css/common.css" />
</head>
<body>
<h2>Listado Ventas</h2>
<f:view>
<t:saveState id="bean" value="#{bean}" />
<t:messages id="messages" errorClass="error" infoClass="info"
replaceIdWithLabel="true" showSummary="false" showDetail="true" />
<h:form id="form">
<h:panelGrid id="headerGrid" columns="2" columnClasses="header,detail"
styleClass="defaultTable">
<t:outputLabel for="name" value="Nombre" />
<h:inputText id="name" value="#{bean.item.name}" required="true" />
<t:outputLabel for="description" value="Descripcion" />
<h:inputTextarea id="description" value="#{bean.item.description}"
required="true" cols="50" rows="2" />
<t:outputLabel for="status" value="Estado" />
<h:selectOneMenu id="status" value="#{bean.item.status}">
<f:selectItem itemLabel="#{bean.statuses[0]}" itemValue="0" />
<f:selectItem itemLabel="#{bean.statuses[1]}" itemValue="1" />
<f:selectItem itemLabel="#{bean.statuses[2]}" itemValue="2" />
</h:selectOneMenu>
<t:outputLabel for="price" value="Precio" />
<h:inputText id="price" value="#{bean.item.price}" required="true" />
<f:facet name="footer">
<h:panelGroup>
<h:commandButton action="#{bean.agregar}" value="Agregar"
rendered="#{empty(bean.item.name)}" />
<h:commandButton action="#{bean.actualizar}" value="Actualizar"
rendered="#{!empty(bean.item.name)}" />
<h:commandButton action="#{bean.eliminar}" value="Eliminar"
rendered="#{!empty(bean.item.name)}" />
</h:panelGroup>
</f:facet>
</h:panelGrid>
</h:form>
<h:outputText value="(A ver si descubren los bugs)" />
<h:outputText value="Status" style="font-size:20px;color:white" />
<h:form id="form2">
<t:dataTable id="listDataTable" headerClass="header" rows="#{
bean.rowsPerPage}" rowIndexVar="rowIndex"
first="#{bean.rowIndex}" columnClasses="detail,detail,detailNumber"
value="#{bean.items}" var="i" sortable="true"
sortColumn="#{bean.sortColumn}" sortAscending="#{bean.sortAscending}"
preserveDataModel="false" preserveSort="true"
styleClass="defaultTable">
<t:column defaultSorted="true">
<f:facet name="header">
<h:outputText value="Nombre" />
</f:facet>
<t:popup styleClass="popup" closePopupOnExitingElement="true"
closePopupOnExitingPopup="true" displayAtDistanceX="0"
displayAtDistanceY="2">
<h:commandLink action="#{bean.cargarItem}" value="#{i.name}">
<t:updateActionListener property="#{bean.item.name}" value="#{
i.name}" />
<t:updateActionListener property="#{bean.selectedRowIndex}"
value="#{rowIndex}" />
</h:commandLink>
<f:facet name="popup">
<h:panelGroup>
<h:panelGrid columns="1">
<h:outputText value="#{i.description}" escape="false" />
</h:panelGrid>
</h:panelGroup>
</f:facet>
</t:popup>
<h:outputText value="#{i.lowerCaseName}" style="visibility:hidden"
/>
</t:column>
<t:column>
<f:facet name="header">
<h:outputText value="Estado" />
</f:facet>
<h:outputText value="#{bean.statuses[i.status]}" />
</t:column>
<t:column>
<f:facet name="header">
<h:outputText value="Precio" />
</f:facet>
<h:outputText value="$#{i.price}" style="text-align: right" />
</t:column>
<f:facet name="footer">
<t:dataScroller id="listScroll" for="listDataTable" fastStep="10"
pageCountVar="pageCount" pageIndexVar="pageIndex"
paginator="true" paginatorMaxPages="9"
paginatorActiveColumnStyle="font-weight:bold;"
renderFacetsIfSinglePage="false">
<f:facet name="first">
<t:graphicImage url="/images/arrow-first.gif" border="1" />
</f:facet>
<f:facet name="last">
<t:graphicImage url="/images/arrow-last.gif" border="1" />
</f:facet>
<f:facet name="previous">
<t:graphicImage url="/images/arrow-previous.gif" border="1" />
</f:facet>
<f:facet name="next">
<t:graphicImage url="/images/arrow-next.gif" border="1" />
</f:facet>
<f:facet name="fastforward">
<t:graphicImage url="/images/arrow-ff.gif" border="1" />
</f:facet>
<f:facet name="fastrewind">
<t:graphicImage url="/images/arrow-fr.gif" border="1" />
</f:facet>
</t:dataScroller>
</f:facet>
</t:dataTable>
</h:form>
</f:view>
</body>
</html>