简体   繁体   中英

How to send values entered in two separate <h:inputText/> elements to a single method in java?

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?

        <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");
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM