webtier@glassfish.java.net

problem with the datatable in jsf

From: <webtier_at_javadesktop.org>
Date: Fri, 01 Oct 2010 01:52:24 PDT

i have one problem with datatable.At first time bean load the value and datatable can able to show the values from list.but when i click on button on the same jsp..data table again call the getList method of bean and i am not able to perform the next operation.

search bean :
****************************************
package se.bilprovningen.prippe.web.impl;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;

import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
import javax.ejb.EJB;
import javax.faces.context.FacesContext;
import javax.faces.model.SelectItem;
import javax.servlet.ServletContext;

import se.bilprovningen.prippe.common.biz.Article;
import se.bilprovningen.prippe.common.biz.Filter;
import se.bilprovningen.prippe.facade.ArticleFacade;
import se.bilprovningen.prippe.facade.exception.PrippeErrorException;
import se.bilprovningen.prippe.facade.exception.PrippeFatalException;
import se.bilprovningen.prippe.web.Search;
import se.bilprovningen.prippe.web.util.FilterFactory;

public class SearchBean implements Serializable, Search {

        /**
         *
         */
        private static final long serialVersionUID = 5112577785724797101L;

        @EJB
        ArticleFacade articleFacade;
        private HashMap<String, Article> articleMap = new HashMap<String, Article>();
        private String name;
        private String value;
        
        private List<SelectItem> propertyList = new ArrayList<SelectItem>();
        private List<String> articleIdList = new ArrayList<String>();
        

        public HashMap<String, Article> getArticleMap() {
                return articleMap;
        }

        public void setArticleMap(HashMap<String, Article> articleMap) {
                this.articleMap = articleMap;
        }

        
        public List<String> getArticleIdList() {
                return articleIdList;
        }

        public void setArticleIdList(List<String> articleIdList) {
                this.articleIdList = articleIdList;
        }

        
        public List<SelectItem> getPropertyList() {
                return propertyList;
        }

        
        public void setPropertyList(List<SelectItem> propertyList) {
                this.propertyList = propertyList;
        }

        /* (non-Javadoc)
         * @see se.bilprovningen.prippe.web.impl.Search#getName()
         */
        public String getName() {
                return name;
        }

        /* (non-Javadoc)
         * @see se.bilprovningen.prippe.web.impl.Search#setName(java.lang.String)
         */
        public void setName(String name) {
                this.name = name;
        }

        /* (non-Javadoc)
         * @see se.bilprovningen.prippe.web.impl.Search#getValue()
         */
        public String getValue() {
                return value;
        }

        /* (non-Javadoc)
         * @see se.bilprovningen.prippe.web.impl.Search#setValue(java.lang.String)
         */
        public void setValue(String value) {
                this.value = value;
        }

        /* (non-Javadoc)
         * @see se.bilprovningen.prippe.web.impl.Search#init()
         */
        
        @SuppressWarnings("unchecked")
        @PostConstruct
        public void init(){
                
                
                System.out.println("SearchBean Post-Constructed");
                setValue("");
                //Get Articles from DB and upload the List
                List<String> properties = articleFacade.getAllDescriptions();
                propertyList = new ArrayList<SelectItem>();
                if( properties != null && properties.size() > 0 ){
                        
                        Iterator itr = properties.iterator();
                        SelectItem firstItm = new SelectItem();
                        firstItm.setLabel("Select Property");
                        firstItm.setValue("");
                        propertyList.add(firstItm);
                        
                        while( itr.hasNext() ){
                                SelectItem itm = new SelectItem();
                                String propertyName = itr.next().toString();
                                itm.setLabel(propertyName);
                                itm.setValue(propertyName);
                                propertyList.add(itm);
                        }
                }
                System.out.println("SearchBean Successfully Post-Constructed");
        }
        
        /* (non-Javadoc)
         * @see se.bilprovningen.prippe.web.impl.Search#destroy()
         */
        @PreDestroy
        public void destroy(){
                System.out.println("SearchBean Pre-Destroyed");
                
        }
        
        /* (non-Javadoc)
         * @see se.bilprovningen.prippe.web.impl.Search#doSearch()
         */
        @SuppressWarnings("unchecked")
        public String doSearch() {
                System.out.println("Selected Name:"+getName());
                System.out.println("Selected Value:"+getValue());
                Filter filter = FilterFactory.getNewInstance(getName(), getValue());
                List<Filter> filterList = new ArrayList<Filter>();
                List<String> filterValues = new ArrayList<String>();
                filterList.add(filter);
                filterValues.add(getValue());
                HashMap map = null;
                try {
                        List<Article> articles = articleFacade.findArticles(filterList, filterValues, null, null);
                        if(articles != null && articles.size()>0){
                                map = new HashMap();
                                for(int i=0; i<articles.size(); i++){
                                        Article article = articles.get(i);
                                        map.put(article.getArticleId(), article);
                                        articleIdList.add(i, article.getArticleId());
                                }
                                FacesContext fc = FacesContext.getCurrentInstance();
                        ServletContext sc = (ServletContext)fc.getExternalContext().getContext();
                        sc.setAttribute("selectedArticleMap", map);
                        setArticleMap(map);
                        }
                } catch (PrippeFatalException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                } catch (PrippeErrorException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                }
                return "search";
        }
        
        public String doReturn() {
                FacesContext fc = FacesContext.getCurrentInstance();
        ServletContext sc = (ServletContext)fc.getExternalContext().getContext();
        sc.removeAttribute("selectedArticleMap");
        
                return "return";
        }
}
***************************************************************************

jsp :

<%@ page language="java"%>
<%@ taglib uri="http://java.sun.com/jsf/html" prefix="h" %>
<%@ taglib uri="http://java.sun.com/jsf/core" prefix="f" %>
<f:loadBundle basename="resources.application" var="msg"/>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
//Get Search result list
%>

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Search Result</title>
</head>
<script type="text/javascript">
        function setArticleId(articleId) {
                document.getElementById("searchResultForm:hidArticleId").value = articleId;
        }
</script>
<body>
<f:view>
                        <h:form id="searchResultForm">
                        <h:inputHidden id="hidArticleId" value="#{showArticleBean.hidArticleId}"></h:inputHidden>
                                <h:dataTable id="mainTable" value="#{searchBean.articleIdList}" var="article" border="1">
                                        <h:column >
                                                <f:facet name="header">
                                                        <h:outputText value="#{msg.label_article_id}"/>
                                                </f:facet>
                                                <h:outputText value="#{article}"/>
                                        </h:column>
                                        <h:column>
                                                <f:facet name="header">
                                                        <h:outputText value=""/>
                                                </f:facet>
                                                <h:commandButton id="showGenericProperties" value="#{msg.btn_show_article}"
                                                                        action="#{showArticleBean.doShowGenericProeprties}" onclick="setArticleId('#{article}');"/>&nbsp;
                                        </h:column>
                                        <h:column>
                                                <f:facet name="header">
                                                        <h:outputText value=""/>
                                                </f:facet>
<!-- <h:commandButton id="showPPE" value="#{msg.btn_show_ppe}" action="#{showArticleBean.doShowPPE}" onclick="setArticleId('#{article}');"/>&nbsp;-->
                                                        <h:commandButton id="showPPUProperties" value="#{msg.btn_show_ppe}"
                                                                        action="#{showArticleBean.doShowPPUProperties}" onclick="setArticleId('#{article}');"/>&nbsp;
                                        </h:column>
                                        <h:column>
                                                <f:facet name="header">
                                                        <h:outputText value=""/>
                                                </f:facet>
                                                        <h:commandButton id="showPrice" value="#{msg.btn_show_price}"
                                                                        action="#{showArticleBean.doShowPrice}" onclick="setArticleId('#{article}');"/>&nbsp;
                                        </h:column>
                                </h:dataTable>
                                <div>
                                        &nbsp;<h:commandButton id="back" value="#{msg.btn_return}" action="#{searchBean.doReturn}" />
                                </div>
                        </h:form>
</f:view>

</body>
</html>


***************************************************************************************


in this jsp if i click on any command button i am not able to go ahead
[Message sent by forum member 'arvindporlekar']

http://forums.java.net/jive/thread.jspa?messageID=484088