JSF动态生成组件

46 篇文章 0 订阅
29 篇文章 0 订阅

         用过Servlet和JSP的开发者对动态生成组件应该是情有独钟了,可以根据数据的情况生成特定的组件,这样增大了Form的灵活性,那么JSF中如何生成动态的窗体呢,其实非常简单。主要逻辑就是通过FacesContext得到viewRoot对象,然后通过viewRoot对象的getChildren方法可以得到viewRoot下的所有第一级组件,然后分别对每个组件的getChildren方法进行递归调用,就可以得到整个组件树,当然可以对某个组件的getChildren得到的List使用add方法添加组件了,代码如下,页面有两个commandButton,其中一个可以添加一个TextBox控件,另外一个可以在console打印出当前的组件列表。 

package  net.moon;

import  java.util.List;

import  javax.faces.component.UIComponent;
import  javax.faces.component.UIViewRoot;
import  javax.faces.component.html.HtmlInputText;
import  javax.faces.context.FacesContext;

public   class  DymaComponent  {

    
private UIViewRoot viewRoot;
    
private static int inputIndex = 0;

    
private List<UIComponent> getComponentChildren(UIComponent component){
        List
<UIComponent> componentList = null;
        System.out.println(component.getId());
        
if(component.getChildCount() > 0){
            
for(UIComponent ui : component.getChildren()){
                componentList 
= getComponentChildren(ui);
            }

        }

        
return componentList;
    }

    
    
public String getComponentsList(){
        viewRoot 
= FacesContext.getCurrentInstance().getViewRoot();
        
for(UIComponent component : viewRoot.getChildren()){
            getComponentChildren(component);
        }

        
return null;
    }

    
    
public String addTextBox(){
        viewRoot 
= FacesContext.getCurrentInstance().getViewRoot();
        UIComponent form1 
= viewRoot.getChildren().get(0);
        HtmlInputText input 
= new HtmlInputText();
        input.setId(
"input" + (inputIndex++));
        input.setValue(
"Input 1");
        input.setRendered(
true);
        form1.getChildren().add(input);
        
return null;
    }

    
}

 

<% @ page contentType="text/html; charset=UTF-8"  %>
<% @ taglib uri="http://java.sun.com/jsf/html" prefix="h"  %>
<% @ taglib uri="http://java.sun.com/jsf/core" prefix="f"  %>

< html >
    
< head >
        
< meta  http-equiv ="Content-Type"  content ="text/html; charset=UTF-8" />
        
< title ></ title >
    
</ head >
    
< body >
        
< f:view >
            
< h:form  id ="form1" >
                
< h:commandButton  id ="commmand1"  action ="#{dymaComponent.getComponentsList}"  value ="Print ViewRoot" ></ h:commandButton >
                
< h:commandButton  action ="#{dymaComponent.addTextBox}"  value ="Add TextBox" ></ h:commandButton >
            
</ h:form >
        
</ f:view >
    
</ body >
</ html >

 

 

 

<? xml version="1.0" encoding="UTF-8" ?>

< faces-config
    
xmlns ="http://java.sun.com/xml/ns/javaee"
    xmlns:xsi
="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation
="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-facesconfig_1_2.xsd"
    version
="1.2" >
    
< managed-bean >
        
< managed-bean-name >
        dymaComponent
</ managed-bean-name >
        
< managed-bean-class >
        net.moon.DymaComponent
</ managed-bean-class >
        
< managed-bean-scope >
        session
</ managed-bean-scope >
    
</ managed-bean >

</ faces-config >

 

 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值