1

I've got a bit of a problem. Basically I have a page with a form containing an input text field, a search button and a ui:repeat structure that displays the results of the search using more input text fields and a couple of other buttons (the idea is to list all search hits and allow the user to edit them directly from the same page). Now, the search and the display part works well - it does what it's supposed to. However, when I try to click on one of the buttons that are displayed in the ui:repeat section, the action isn't invoked.

When I press the search button (Pretraga in code) a function is called in the Kupac bean which then gets the results from the database and places them into a list and finally redirects back to the same page (from my understanding this will load the page again) and lists the results using the ui:repeat component. But as I said, when I press the other buttons (Izmeni and Glavna in code) they do nothing. Kupac bean is @RequestScoped and I already tried changing it to @ViewScoped, however this only made it worse by having the search not work as well (i.e. it will display nothing).

Here's the JSF page code:

<?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://xmlns.jcp.org/jsf/html"
      xmlns:ui="http://xmlns.jcp.org/jsf/facelets"
      xmlns:f="http://xmlns.jcp.org/jsf/core">
    <h:head>
        <title>Pregled kupaca</title>
    </h:head>
    <h:body>
        <h3 align="center">Pregled kupaca</h3>
        <h:form>
            <table align="center">
                <tr>
                    <td align="center" colspan="2"><h:link outcome="pocetna.xhtml">Pocetna</h:link></td>
                </tr>
                <tr>
                    <td>Firma</td>
                    <td><h:inputText value="#{kupac.naziv_firme}"/></td>
                </tr>
                <tr>
                    <td align="center" colspan="2">
                        <h:commandButton action="#{kupac.listKupci()}" value="Pretraga">
                        </h:commandButton>                    
                    </td>
                </tr>
            </table>
            <br/>
            <ui:repeat value="#{kupac.zeljeni_kupci}" var="kupac">
                <table align="center" border="1">
                    <tr>
                        <td>Ime i prezime</td>
                        <td><h:inputText 
                                required="true"
                                requiredMessage="Niste uneli ime i prezime!"
                                value="#{kupac.ime_prezime}"/></td>
                    </tr>
                    <tr>
                        <td>Adresa</td>
                        <td><h:inputText 
                                required="true"
                                requiredMessage="Niste uneli adresu!"
                                value="#{kupac.adresa}"/></td>
                    </tr>
                    <tr>
                        <td>Naziv firme</td>
                        <td><h:inputText 
                                required="true"
                                requiredMessage="Niste uneli naziv firme!"
                                value="#{kupac.naziv_firme}"/></td>
                    </tr>
                    <tr>
                        <td>Adresa firme</td>
                        <td><h:inputText 
                                required="true"
                                requiredMessage="Niste uneli adresu firme!"
                                value="#{kupac.adresa_firme}"/></td>
                    </tr>
                    <tr>
                        <td>Br. telefona</td>
                        <td><h:inputText 
                                required="true"
                                requiredMessage="Niste uneli broj telefona!"
                                value="#{kupac.br_tel}"/></td>
                    </tr>
                    <tr>
                        <td>
                            <h:commandButton value="Izmeni" action="#{kupac.izmenaKupca()}"/>
                        </td>
                        <td><h:commandButton action="#{kupac.test()}" value="Glavna"/></td>
                    </tr>
                </table>
            <br/><br/>
            </ui:repeat>
        </h:form>
    </h:body>
</html>

and here's the full bean code in Java:

package beans;

import exceptions.DBException;
import java.io.Serializable;
import java.util.LinkedList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
import javax.faces.bean.ViewScoped;
import util.Database;

@ManagedBean
@RequestScoped
public class Kupac implements Serializable {

    private int id_kupac;
    private String ime_prezime;
    private String adresa;
    private String naziv_firme;
    private String adresa_firme;
    private String br_tel;
    private List<Kupac> svi_kupci;
    private List<Kupac> zeljeni_kupci;

    public void init() {
        listKupci();
    }

    public void poruka() {
        System.out.println("ID "+id_kupac);
    }

    public String test() {
        return "pocetna";
    }

    public String izmenaKupca() throws DBException {
        Kupac kupac = new Kupac();
        Database db = Database.getInstance();

        kupac.setId_kupac(id_kupac);
        kupac.setIme_prezime(ime_prezime);
        kupac.setAdresa(adresa);
        kupac.setNaziv_firme(naziv_firme);
        kupac.setAdresa_firme(adresa_firme);
        kupac.setBr_tel(br_tel);

        try {
            db.updateKupac(kupac);
        } catch (DBException ex) {
            return "error";
        }
        return "pregled_kupaca";
    }

    public String unosKupca() throws DBException {
        Kupac kupac = new Kupac();
        Database db = Database.getInstance();

        kupac.setIme_prezime(ime_prezime);
        kupac.setAdresa(adresa);
        kupac.setNaziv_firme(naziv_firme);
        kupac.setAdresa_firme(adresa_firme);
        kupac.setBr_tel(br_tel);

        try {
            db.insertKupac(kupac);
        } catch (DBException ex) {
            return "error";
        }

        return "pocetna";
    }

    public String listKupci() {
        Database db = Database.getInstance();
        zeljeni_kupci = new LinkedList<Kupac>();
        try {
            svi_kupci = db.listKupci();
            for (Kupac k : svi_kupci) {
                if (k.naziv_firme.equals(naziv_firme) || "".equals(naziv_firme)) {
                    zeljeni_kupci.add(k);
                }
            }
        } catch (DBException ex) {
            return "error";
        }
        return "pregled_kupaca";
    }

    public List<Kupac> getZeljeni_kupci() {
        return zeljeni_kupci;
    }

    public void setZeljeni_kupci(List<Kupac> zeljeni_kupci) {
        this.zeljeni_kupci = zeljeni_kupci;
    }

    public List<Kupac> getSvi_kupci() {
        return svi_kupci;
    }

    public void setSvi_kupci(List<Kupac> svi_kupci) {
        this.svi_kupci = svi_kupci;
    }

    public int getId_kupac() {
        return id_kupac;
    }

    public void setId_kupac(int id_kupac) {
        this.id_kupac = id_kupac;
    }

    public String getIme_prezime() {
        return ime_prezime;
    }

    public void setIme_prezime(String ime_prezime) {
        this.ime_prezime = ime_prezime;
    }

    public String getAdresa() {
        return adresa;
    }

    public void setAdresa(String adresa) {
        this.adresa = adresa;
    }

    public String getNaziv_firme() {
        return naziv_firme;
    }

    public void setNaziv_firme(String naziv_firme) {
        this.naziv_firme = naziv_firme;
    }

    public String getAdresa_firme() {
        return adresa_firme;
    }

    public void setAdresa_firme(String adresa_firme) {
        this.adresa_firme = adresa_firme;
    }

    public String getBr_tel() {
        return br_tel;
    }

    public void setBr_tel(String br_tel) {
        this.br_tel = br_tel;
    }

    /**
     * Creates a new instance of Kupac
     */
    public Kupac() {
    }
}

I'm using JSF 2.2 on a GlassFish 4.0 server using Netbeans 7 IDE.

Sorry for the long post and the fact that this question has now been asked several times, but I haven't managed to fix it for 2 hrs now and would appreciate any help. Cheers!

Luiggi Mendoza
  • 85,076
  • 16
  • 154
  • 332
stellarossa
  • 1,730
  • 1
  • 18
  • 29
  • Have you gone through the reasons explained here: http://stackoverflow.com/a/2120183/1065197? Also, I think is weird when you say using `@ViewScoped` make things worse, in fact it should work with this change. – Luiggi Mendoza Sep 17 '13 at 17:03
  • I've actually seen that post but it seems as though most/none of those points apply to my code. – stellarossa Sep 17 '13 at 17:06

0 Answers0