JSF <h:inputText>
標簽用于呈現(xiàn)網(wǎng)頁上的輸入字段。它在<h:form>
標簽中用于聲明允許用戶輸入數(shù)據(jù)的輸入字段。
value
屬性指的是名為User
的委托Bean
的name
屬性。該屬性保存名稱組件的數(shù)據(jù)。 用戶提交表單后,User
中的name
屬性的值將被設置為與該標簽對應的字段中輸入的文本。
打開 NetBean8.2,創(chuàng)建一個名稱為:htags的JSF工程,然后按以下步驟添加相應文件和代碼。
<h:InputText>
標簽示例在下面的示例中,我們使用標簽標簽為inputText
標簽提供標簽,一個帶有屬性的inputText
,一個用于表示提交按鈕的commandButton
。 全部都被包含在<h:form>
標簽中。
文件: inputtext.xhtml 的代碼如下所示 -
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://xmlns.jcp.org/jsf/html">
<h:head>
<title>Facelet Title</title>
</h:head>
<h:body>
<h:form id="form">
<h:inputText id="username" value="#{user.name}" label="username" maxlength="10"
size="15" alt="username" autocomplete="off" readonly="false" required="true"
requiredMessage="Username is required" style="color:red" accesskey="q">
</h:inputText> </h:form>
</h:body>
</html>
文件: User.java 的代碼如下所示 -
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package com.yiibai;
/**
*
* @author Maxsu
*/
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
@ManagedBean
@RequestScoped
public class User {
String name;
String email;
String password;
String gender;
String address;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getGender() {
return gender;
}
public void setGender(String gender) {
this.gender = gender;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
}
<h:inputText>
標簽如下:代碼如下所示 -
<input id="userform:username" name="userform:username" autocomplete="off" accesskey="q"
alt="username" maxlength="10" size="15" style="color:red" type="text">
打開瀏覽器,訪問:http://localhost:8084/htags/faces/inputtext.xhtml , 輸出如下所示 -
<h:inputText>
標簽的屬性屬性名稱 | 描述 |
---|---|
id | 它是此組件的標識符,ID必須是唯一的。您可以使用它來訪問CSS和JS文件中的HTML元素。 |
value | 它用于收集輸入文本的當前值。 |
class | 它給組件的類名,它用于從CSS和JS文件訪問組件。 |
maxlength | 在此字段中可能輸入的最大字符數(shù)。 |
alt | 由該組件呈現(xiàn)的元素的替代文本描述。 |
accesskey | 按鍵的訪問鍵將焦點轉(zhuǎn)移到該元素。它將根據(jù)瀏覽器的不同而不同。 |
size | 用于確定此字段寬度的字符數(shù)。 |
required | 它表示用戶需要為此輸入組件提供提交的值。 |
requiredMessage | 如果將required 屬性設置為true ,則在“RequiredMessage ”中提供的消息描述將顯示到網(wǎng)頁。 |
style | 它用于為組件應用CSS。 |
rendered | 它用于渲染組件。該屬性的默認值為true 。 |
convertor | 它用于注冊此組件的轉(zhuǎn)換器實例。 |
readonly | 它表示該組件禁止用戶進行更改,通過將readonly 作為此屬性的值傳遞,可以使組件只讀。 例如。 readonly =“readonly” |