15.6. LiteBean的使用

LiteBean声明以后,就可以被使用了。使用方式有两种:

1) 直接在页面中通过EL表达式使用LiteBean

<f:view xmlns="http://www.w3.org/1999/xhtml"
    xmlns:f="http://java.sun.com/jsf/core"
    xmlns:w="http://www.apusic.com/jsf/widget"> 
    <w:page title="pageTitle"> 
        <w:textField width="100" value=1"#{myBean.txt}"></w:textField>
    </w:page>
</f:view>
import java.io.Serializable;
import org.operamasks.faces.annotation.ManagedBean;
import org.operamasks.faces.annotation.ManagedBeanScope;

@ManagedBean(name="myBean", scope=ManagedBeanScope.SESSION)
public class MyBean implements Serializable {
    private String txt;
    
    public String getTxt() {
        return txt;
    }
    public void setTxt(String txt) {
        this.txt = txt;
    }
}

2) 基于IoVC使用LiteBean。

下面的代码

<f:view xmlns="http://www.w3.org/1999/xhtml"
    xmlns:f="http://java.sun.com/jsf/core"
    xmlns:w="http://www.apusic.com/jsf/widget"> 
    <w:page title="pageTitle"> 
        <w:textField width="100" id="txt"></w:textField>
    </w:page>
</f:view>
import java.io.Serializable;
import org.operamasks.faces.annotation.ManagedBean;
import org.operamasks.faces.annotation.ManagedBeanScope;

@ManagedBean(name="myBean", scope=ManagedBeanScope.SESSION)
public class MyBean implements Serializable {
    @Bind
    private String txt;
}

上面两种方法的运行效果是一样的。我们并不排斥上述两种用法中的任意一种,但我们建议您使用IoVC这种新的编程模式,使用方法请参考第 20 章 IoVC