使用backing bean来添加UIComponents 到页面中?

本文介绍了一个使用JavaServer Faces (JSF) 创建动态UI组件的例子。通过Jsp1Bean类,实例化了HtmlOutputText和HtmlInputText组件,并将其添加到UIPanel中。配置了faces-config.xml来管理bean的生命周期。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

下面是一个例子:

jsp1.jsp:

<%@taglib uri="http://java.sun.com/jsf/core" prefix="f"%>
<%@taglib uri="http://java.sun.com/jsf/html" prefix="h"%>
<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<html>
<f:view>
<head>
<title>jsp1</title>
  <link rel="stylesheet" type="text/css" href="./style.css" title="Style"/>
</head>
<body bgcolor="#ffffff">  TESTING...
  <h:form id="form1">
    <h:panelGrid id="panelgridtest" binding="#{jsp1Bean.component}"/>
  </h:form>
</body>
</f:view>
</html>

 

Jsp1Bean.java:

 

package test;
 
import javax.faces.application.Application;
import javax.faces.component.UIComponent;
import javax.faces.component.UIPanel;
import javax.faces.component.UIViewRoot;
import javax.faces.component.html.HtmlInputText;
import javax.faces.component.html.HtmlOutputText;
import javax.faces.context.FacesContext;
 
public class Jsp1Bean
{
    UIComponent component = null;
    FacesContext facesContext = FacesContext.getCurrentInstance();
    UIViewRoot uIViewRoot = facesContext.getViewRoot();
    Application application = facesContext.getApplication();
 
    public Jsp1Bean()
    {
    }
 
    public UIComponent getComponent()
    {
        if (component == null)
        {
            component = new UIPanel();
        }
        return component;
    }
 
    public void setComponent(UIComponent component)
    {
        this.component = component;
    }
 
     //initialization block
    {
        try
        {
            //outputText1
            HtmlOutputText outputText1 = (HtmlOutputText
              facesContext.getApplication().createComponent(HtmlOutputText.COMPONENT_TYPE);
            outputText1.setValue("---the outputText1 value---");
            //inputText1
            HtmlInputText inputText1 = (HtmlInputText)
                facesContext.getApplication().createComponent(HtmlInputText.COMPONENT_TYPE);
            inputText1.setValue("---the inputText1 value---");
 
            //add outputText1 and inputText1 to component ("UIPanel")
            this.getComponent().getChildren().add(outputText1);
            this.getComponent().getChildren().add(inputText1);
        }
        catch (java.lang.Throwable t)
        {
            System.out.println("java.lang.Throwable exception encountered...t.getMessage()=" + t.getMessage());
            t.printStackTrace();
        }
    }
 
    public String doAction()
    {
        return "submit";
    }
}

 

faces-config.xml:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE faces-config PUBLIC "-//Sun Microsystems, Inc.//DTD JavaServer Faces Config 1.1//EN"
                              "http://java.sun.com/dtd/web-facesconfig_1_1.dtd">
<faces-config>
  <navigation-rule>
    <from-view-id>/jsp1</from-view-id>
    <navigation-case>
      <from-action>submit</from-action>
      <to-view-id>/jsp1</to-view-id>
      <redirect/>
    </navigation-case>
  </navigation-rule>
  <managed-bean>
    <managed-bean-name>jsp1Bean</managed-bean-name>
    <managed-bean-class>test.Jsp1Bean</managed-bean-class>
    <managed-bean-scope>session</managed-bean-scope>
  </managed-bean>
</faces-config>
 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值