Javaのお勉強



JSF binding@Java

JSFでbindingを行います。
jsfで配置したコンポーネントとバッキングビーンをbindingします。

jspにinputText、commandButton、outputLabelを配置します。
それぞれにbindingを設定します。
また、ボタンにはactionを設定します。

Beanを作成します。
bindingするメンバを作成します。
また、ボタンを押した時の処理を記述します。

jsp

<%@page contentType="text/html"%>
<%@page pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
    "http://www.w3.org/TR/html4/loose.dtd">
<% request.setCharacterEncoding("UTF-8");%>

<%@taglib prefix="f" uri="http://java.sun.com/jsf/core"%>
<%@taglib prefix="h" uri="http://java.sun.com/jsf/html"%>
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>inputText、commandButton、outputLabel binding</title>
</head>
<body>

    <f:view>
        <h:form>
            <h:inputText id="inputText" binding="#{ViewPageBean.inputText}" />
            <br>
            <h:commandButton id="btn" binding="#{ViewPageBean.btn}" action="#{ViewPageBean.btnClick}" />
            <h:outputLabel id="outputLabel" binding="#{ViewPageBean.outputLabel}" />
        </h:form>
    </f:view>

</body>
</html>


Bean

package Beans;

import javax.faces.component.html.*;


public class ViewPageBean {

    //javax.faces.component.html
    private HtmlInputText inputText;
    private HtmlOutputLabel outputLabel;
    private HtmlCommandButton btn;

    public ViewPageBean() {
        //HtmlInputText
        inputText = new HtmlInputText();

        //HtmlOutputLabel
        outputLabel = new HtmlOutputLabel();

        //HtmlCommandButton
        btn = new HtmlCommandButton();
        btn.setValue("実行");
    }

    public HtmlInputText getInputText() {
        return this.inputText;
    }

    public void setInputText(HtmlInputText inputText) {
        this.inputText = inputText;
    }

    public HtmlOutputLabel getOutputLabel() {
        return this.outputLabel;
    }

    public void setOutputLabel(HtmlOutputLabel outputLabel) {
        this.outputLabel = outputLabel;
    }

    public HtmlCommandButton getBtn() {
        return this.btn;
    }

    public void setBtn(HtmlCommandButton btn) {
        this.btn = btn;
    }

    //ボタンクリック処理
    public void btnClick() {
        this.outputLabel.setValue(this.inputText.getValue());
    }
}




Copyright (C) 2008-2026 Javaのお勉強. All Rights Reserved.