Jak dodać obiekt klasy do listy JSF

0

Witam

Od jakiegoś czasu walczę z problemem dodania klasy do listy i wyświetlenia danych.

<?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://java.sun.com/jsf/html"
      xmlns:f="http://java.sun.com/jsf/core">
    <h:head>
        <title>Dodanie Rekordu</title>
    </h:head>
    <h:body>
        <h:form>
            <h:inputText id="w1" value="#{rekord.wyraz1}" /><br></br>
            <h:inputText id="w2" value="#{rekord.wyraz2}" /><br></br>
            <h:commandButton value="Dodaj" action="#{lista.dodaj(w1, w2)}"> /*tutaj próbowałem różnie w zależności od wersji funkcji również rekord.wyraz1, rekord.wyraz2*/
            </h:commandButton>
        </h:form>
    </h:body>
</html>
package paczka;

//import javax.annotation.ManagedBean;

import javax.enterprise.context.SessionScoped;
import javax.faces.bean.ManagedBean;


/**
 *
 * @author waldek
 */
@ManagedBean(name="rekord")
@SessionScoped
public class Rekord {
    private String wyraz1;
    private String wyraz2;

    public Rekord(String wyraz1, String wyraz2){
        this.wyraz1 = wyraz1;
        this.wyraz2 = wyraz2;
    }
    public Rekord(){}

    public String getWyraz1() {
        return wyraz1;
    }

    public void setWyraz1(String wyraz1) {
        this.wyraz1 = wyraz1;
    }

    public String getWyraz2() {
        return wyraz2;
    }

    public void setWyraz2(String wyraz2) {
        this.wyraz2 = wyraz2;
    }
    
}
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package paczka;

import java.util.ArrayList;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.event.ActionEvent;

/**
 *
 * @author waldek
 */
@ManagedBean(name="lista")
@SessionScoped
public class Lista {
    Rekord rekord;

    ArrayList<Rekord> lista = new ArrayList<Rekord>();

    public Rekord getRekord() {
        return rekord;
    }

    public void setRekord(Rekord rekord) {
        this.rekord = rekord;
    }

    public Lista(){}

    public ArrayList<Rekord> getLista() {
        return lista;
    }

    public void setLista(ArrayList<Rekord> lista) {
        this.lista = lista;
    }
    /*public String dodaj(String wyraz1, String wyraz2){
        Rekord rekord = new Rekord(wyraz1,wyraz2);
        lista.add(rekord);
        return "dodany";
    }*/
    //funkcja dodaj
    public String dodaj(String t1, String t2){
            Rekord rekord = new Rekord(t1, t2);
            if(lista.add(rekord) == true){
                return "dodany";
        }else{
                return "false";
        }
    }
    //koniec funkcji dodaj

    //funkcje nawigacyjne
    public String powrot(){
        return "index";
    }
    public String wyswielt(){
        return "wyswietlenie";
    }
    //koniec funkcje nawigacyjne tylko
}
<?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://java.sun.com/jsf/html">
    <h:head>
        <title>Rekord został dodany</title>
    </h:head>
    <h:body>
        Rekord został dodany;
        <h:form>
        <h:commandButton value="Powrót" action="#{lista.powrot}" />
        <h:commandButton value="Wyswietl Listę" action="#{lista.wyswielt}" />
        </h:form>
    </h:body>
</html>
<?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://java.sun.com/jsf/html"
      xmlns:f="http://java.sun.com/jsf/core">
    <h:head>
        <title>Lista</title>
    </h:head>
    <h:body>
        <h:form>
        <h:commandButton value="Powrót" action="#{lista.powrot}" />
        
        <h:dataTable id="Table" value="#{lista.lista}" var="item" >
            <h:column>
                <f:facet name="header">
                    <h:outputText value="Wyraz1" /> <h:outputText value="Wyraz2" />
                </f:facet>
                <h:outputText value="#{item}" /> <h:outputText value="#{item}" />
            </h:column>
        </h:dataTable>
        </h:form>
        
    </h:body>
</html>

W zasadzie wszystkie kombinacje prowadzą do wyświetlenia jak poniżej, bądź błędów:

"Wyraz1Wyraz2
[email protected]@b8ed99
[email protected]@7be55c
[email protected]@154c4ea
[email protected]@1290383
[email protected]@502b2"

Prośba o naprowadzenie w jaki sposób sobie z tym poradzić.

Pozdrowienia.

0

Witam

W zasadzie udało mi się dodać i wyświetlić dane.
Czekam jednak na inne pomysły.

<?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://java.sun.com/jsf/html"
      xmlns:f="http://java.sun.com/jsf/core">
    <h:head>
        <title>Dodanie Rekordu</title>
    </h:head>
    <h:body>
        <h:form>
            <h:inputText id="w1" value="#{rekord.wyraz1}" /><br></br>
            <h:inputText id="w2" value="#{rekord.wyraz2}" /><br></br>
            <h:commandButton value="Dodaj" action="#{lista.dodajRekord(rekord.wyraz1, rekord.wyraz2)}">
            </h:commandButton>
        </h:form>
    </h:body>
</html>
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package paczka;

//import javax.annotation.ManagedBean;

import javax.enterprise.context.SessionScoped;
import javax.faces.bean.ManagedBean;


/**
 *
 * @author waldek
 */
@ManagedBean(name="rekord")
@SessionScoped
public class Rekord {
    private String wyraz1;
    private String wyraz2;

    public Rekord(String wyraz1, String wyraz2){
        this.wyraz1 = wyraz1;
        this.wyraz2 = wyraz2;
    }
    public Rekord(){}

    public String getWyraz1() {
        return wyraz1;
    }

    public void setWyraz1(String wyraz1) {
        this.wyraz1 = wyraz1;
    }

    public String getWyraz2() {
        return wyraz2;
    }

    public void setWyraz2(String wyraz2) {
        this.wyraz2 = wyraz2;
    }
    
}
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package paczka;

import java.util.ArrayList;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.event.ActionEvent;

/**
 *
 * @author waldek
 */
@ManagedBean(name="lista")
@SessionScoped
public class Lista {
    Rekord rekord;

    ArrayList<Rekord> lista = new ArrayList<Rekord>();

    public Rekord getRekord() {
        return rekord;
    }

    public void setRekord(Rekord rekord) {
        this.rekord = rekord;
    }

    public Lista(){}

    public ArrayList<Rekord> getLista() {
        return lista;
    }

    public void setLista(ArrayList<Rekord> lista) {
        this.lista = lista;
    }
    /*public String dodaj(String wyraz1, String wyraz2){
        Rekord rekord = new Rekord(wyraz1,wyraz2);
        lista.add(rekord);
        return "dodany";
    }*/
    //funkcja testowa
    /*public String dodaj(Rekord rekord){
            //Rekord rekord = new Rekord("test1","test2");
        String t1 = rekord.getWyraz1();
        String t2 = rekord.getWyraz2();
         System.out.println(t1 + " " + t2);
        Rekord rek = new Rekord(t1,t2);
            if(lista.add(rek) == true){
                return "dodany";
        }else{
                return "false";
        }
    }*/
    /*public String dodaj(String wyraz1, String wyraz2){
        Rekord rekord = new Rekord();
        rekord.setWyraz1(wyraz1);
        rekord.setWyraz2(wyraz2);
        lista.add(rekord);
        return "dodany";
    }*/
    private Rekord utworzRekord(String wyraz1, String wyraz2){
        Rekord rekord = new Rekord();
        rekord.setWyraz1(wyraz1);
        rekord.setWyraz2(wyraz2);
        return rekord;
    }
    public String dodajRekord(String wyraz1, String wyraz2){
        lista.add(utworzRekord(wyraz1, wyraz2));
        return "dodany";
    }
    //koniec funkcja testowa
    //funkcje nawigacyjne tylko
    public String powrot(){
        return "index";
    }
    public String wyswielt(){
        return "wyswietlenie";
    }
    //koniec funkcje nawigacyjne tylko
}
<?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://java.sun.com/jsf/html">
    <h:head>
        <title>Rekord został dodany</title>
    </h:head>
    <h:body>
        Rekord został dodany;
        <h:outputText value="#{rekord.wyraz1}" /> i <h:outputText value="#{rekord.wyraz2}" />
        <br></br>
        <h:form>
        <h:commandButton value="Powrót" action="#{lista.powrot}" />
        <h:commandButton value="Wyswietl Listę" action="#{lista.wyswielt}" />
        </h:form>
    </h:body>
</html>
<?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://java.sun.com/jsf/html"
      xmlns:f="http://java.sun.com/jsf/core">
    <h:head>
        <title>Lista</title>
    </h:head>
    <h:body>
        <h:form>
        <h:commandButton value="Powrót" action="#{lista.powrot}" />
        
        <h:dataTable id="Table" value="#{lista.lista}" var="item" >
            <h:column>
                <f:facet name="header">
                    <h:outputText value="Wyraz1" /> <h:outputText value="Wyraz2" />
                </f:facet>
                <h:outputText value="#{item.wyraz1}" /> <h:outputText value="#{item.wyraz2}" />
            </h:column>
        </h:dataTable>
        </h:form>
        
    </h:body>
</html>

Pozdrowienia

0

Najprościej to chyba tak:
jsf sam wypełnia odpowiednie pola obiekcie Rekord
w action tylko utrwalasz obiekt może być to lista może być baza.

<?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://java.sun.com/jsf/html"
      xmlns:f="http://java.sun.com/jsf/core">
    <h:head>
        <title>Dodanie Rekordu</title>
    </h:head>
    <h:body>
        <h:form>
            <h:inputText id="w1" value="#{lista.rekord.wyraz1}" /><br></br>
            <h:inputText id="w2" value="#{lista.rekord.wyraz2}" /><br></br>
            <h:commandButton value="Dodaj" action="#{lista.dodajRekord}">
            </h:commandButton>
        </h:form>
    </h:body>
</html>

w klasie List zmieniasz tylko(lub dopisujesz nową) dodajRekord()

public class Lista {
...
    public String dodajRekord(){
        if(rekord != null){
            lista.add(rekord);
            return "dodany";
        }else
            return "error";
    }
...

Rekord nie musi chyba być nawet @ManagedBean (ale tego nie jestem pewien) i nie musi mieć @SessionScoped może być @RequestScoped - bo chyba potrzebujesz utrwalenia tylko listy rekordów(co dzieje się w Lista)

0

Witam

mateuszd napisał(a)

w klasie List zmieniasz tylko(lub dopisujesz nową) dodajRekord()

public class Lista {
...
    public String dodajRekord(){
        if(rekord != null){
            lista.add(rekord);
            return "dodany";
        }else
            return "error";
    }
...

Nie chce działać, wybiera else ;-)

Dziękuję bardzo.
Pozdrawiam

1 użytkowników online, w tym zalogowanych: 0, gości: 1