0

I have a problem as I mentioned in the topic title. I want to pass the values from two different <h:inputText/> elements as parameters to a method that takes two parameters in Java and send the result to a single <h:outputText/> element. I don't know how to do this.

For this problem, I created the setter and getter methods separately on the CDI Bean to represent both <h:inputText/> elements. Sample codes are as follows.

XHTML 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:p="http://xmlns.jcp.org/jsf/passthrough"
      xmlns:f="http://xmlns.jcp.org/jsf/core">
    <h:head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
        <title>Ana Sayfa</title>
    </h:head>
    <h:body>
        <h:form id="indexForm">
            <h:outputLabel value="Tarih1"/><br/>
            <h:inputText id="tarih1Text" p:placeholder="gg/aa/yyyy" value="#{dateCDIBean.date1}"/>
            <br/>
            <h:outputLabel value="Tarih2"/><br/>
            <h:inputText id="tarih2Text" p:placeholder="gg/aa/yyyy" value="#{dateCDIBean.date2}"/>
            <br/>
            <h:commandButton id="btn" value="Farkı Hesapla"/>
            <br/>
            <h:outputText id="cikti" value="#{dataCDIBean.value}"/>
        </h:form>
    </h:body>
</html>

CDI Bean Code:

/*
 * Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
 * Click nbfs://nbhost/SystemFileSystem/Templates/JSF/JSFManagedBean.java to edit this template
 */
package tr.com.bilisim.cdibeans;

import jakarta.enterprise.context.SessionScoped;
import jakarta.inject.Named;
import java.io.Serializable;
import tr.com.selimfatih.tarihfark.TarihFark;
import tr.com.selimfatih.tarihkontrol.TarihKontrol;

/**
 *
 * @author Mehmet Fatih ÇİN
 */
@Named
@SessionScoped
public class dateCDIBean implements Serializable {

    private String date1;
    private String date2;
    private String value;

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

    public String getDate1() {
        return date1;
    }

    public void setDate1(String date1) {
        TarihKontrol tk1 = new TarihKontrol();
        tk1.Kontrol(date1);
        if (tk1.hataliMi == false) {
            this.date1 = date1;
        }
    }

    public String getDate2() {
        return date2;
    }

    public void setDate2(String date2) {
        TarihKontrol tk2 = new TarihKontrol();
        tk2.Kontrol(date2);
        if (tk2.hataliMi == false) {
            this.date2 = date2;
        }
    }

    public String hesapla() {
        if (date1 != null && date2 != null) {
            TarihFark fark = new TarihFark(date1, date2);
            date1 = "";
            date2 = "";
            return fark.GunFark() + " gün, " + fark.AyFark() + " ay, " + fark.YilFark() + " yıl";
        } else {
            return "Yukarıdaki alanlara \"gg/aa/yyyy\" ya da \"gg.aa.yyyy\" formatında tarih değeri giriniz.";
        }
    }

    public String getValue() {
        return hesapla();
    }

    public void setValue(String value) {
        this.value = value;
    }
}

How can I apply the solution in the topic title as an alternative to what I did above? Or can you tell me if this is possible?

Jasper de Vries
  • 19,370
  • 6
  • 64
  • 102
  • What's missing in your question is when to send the data. Most obvious way would be simply having a command button and an action. – Jasper de Vries Dec 31 '22 at 08:18
  • Can you rephrase your comment with an example? I did not fully understand your answer. I made the difference between two dates as day, month, year very simply in the swing interface. But I am new to JSF and Java EE technologies. – Mehmet Fatih ÇİN Dec 31 '22 at 08:30
  • Take a few steps back and learn about JSF actions. Then about data types and converters. A good place to start: https://stackoverflow.com/tags/jsf/info – Jasper de Vries Dec 31 '22 at 09:11

1 Answers1

0
        <h:form id="indexForm">
            <h:outputLabel value="Tarih1"/><br/>
            <h:inputText id="tarih1Text" value="#{dateCDIBean.date1}"/>
            <br/>
            <h:outputLabel value="Tarih2"/><br/>
            <h:inputText id="tarih2Text" value="#{dateCDIBean.date2}"/>
            <br/>
            <h:commandButton id="btn" value="Farkı Hesapla" action="#{dateCDIBean.concdata()}"/>
            <br/>
            <h:outputLabel id="cikti" value="#{dateCDIBean.value}"/>
        </h:form>
    public void concdata() {
        value = date1 + date2;
        PrimeFaces.current().ajax().update("indexForm:cikti");
    }