JSF中控件赋值方式

JSF中对控件的赋值有以下几种方式。
1. 通过资源文件赋值。
2. 通过MBean属性赋值。
3. 通过隐含对象赋值。
4. 将控件绑定到MBean中的属性。

其中,赋值可以为一个公式。下面分别介绍:
1. 通过资源文件赋值:
需先定义资源文件Message.peoperties,代码如下:

LOGIN = Login
PASSWORD 
= Password:
USER_NAME 
= User Name:
JSF页面中引用资源文件:
<%@ 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 locale="en">
    
<f:loadBundle var="msg" basename="Message"/>
    
<h:form>
        
<h:panelGrid border="1" columns="2">
            
<h:outputText value="#{msg.USER_NAME}"></h:outputText>
            
<h:inputText></h:inputText>
            
<h:outputText value="#{msg.PASSWORD}"></h:outputText>
            
<h:inputText></h:inputText>
        
</h:panelGrid>
    
</h:form>
</f:view>
</body>
</html>

代码中JSF文件通过<f:loadBundle>标签引入资源文件,然后用#{msg.USER_NAME}来引用Resource文件中USER_NAME的值。在JSF1.2中,也可以通过在faces-config中配置resource-bundle来引入资源文件。

2. 通过MBean属性赋值:

MBean代码如下:

package net.moon.beans;

public class UserInfo {
    
private String userName;
    
private String password;
    
    
public String getUserName(){
        
return userName;
    }

    
    
public String getPassword(){
        
return password;
    }

    
    
public void setUserName(String userName){
        
this.userName = userName;
    }

    
    
public void setPassword(String password){
        
this.password = password;
    }

}

在faces-config文件中配置MBean:
    <managed-bean>
        
<managed-bean-name>
        userInfo
</managed-bean-name>
        
<managed-bean-class>
        net.moon.model.UserInfo
</managed-bean-class>
        
<managed-bean-scope>
        session
</managed-bean-scope>
    
</managed-bean>
在JSF页面中引用MBean的属性:
<%@ 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 locale="en">
    
<f:loadBundle var="msg" basename="Message"/>
    
<h:form>
        
<h:panelGrid border="1" columns="2">
            
<h:outputText value="#{msg.USER_NAME}"></h:outputText>
            
<h:inputText value="#{userInfo.username}"></h:inputText>
            
<h:outputText value="#{msg.PASSWORD}"></h:outputText>
            
<h:inputText value="#{userInfo.password}"></h:inputText>
        
</h:panelGrid>
    
</h:form>
</f:view>
</body>
</html>

用这种方式可以将控件的Value与MBean中的某一属性双向绑定,从而可以在其中之一被修改时自动修改另外一个的值,这种用法较多。

3. 用隐含对象赋值:
JSF中的隐含对象包含:

隐含对象  描述
applicationScope 整个应用域,相当于JSP中的application
cookie Cookie中资料,相当于JSP中的cookie
facesContext JSF中的FacesContext对象,可以通过它来访问其它对象
header 当前Request的Http header的Map
headerValues 当前Request中所有的Http header的Map
initParam 当前应用的初始化参数的Map
param 请求参数的Map
paramValues 当前参数的Map
requestScope 当前请求,相当于JSP中的request
sessionScope Session的Map,相当于JSP中的session
view 当前view

JSF代码中的引用方法如下:
<h:outputText value="#{sessionScope.userInfo.userName}"></h:outputText>
4. 将控件绑定到MBean中的某一属性,然后可以在MBean中修改控件数据:
基本方法是在MBean中建立类型为javax.faces.component包中的某一控件的属性,然后在JSF代码中用控件的binding属性来将整个控件与MBean中的属性绑定,这样就可以在MBean中修改JSF控件的各种属性,这里不做详细介绍。
posted @ 2007-09-19 11:22  moonsnow  阅读(185)  评论(0编辑  收藏  举报