In JSF, you can use the <h:inputText /> tag to render a HTML input of type=”text”, text box. For example,

JSF tag…

<h:inputText />

Render this HTML code…

<input type="text" name="j_idt6:j_idt7" />

P.S The name attribute value is randomly generated by JSF.

JSF textbox example

A full JSF 2 example to render a textbox input field via <h:inputText /> tag.

1. Managed Bean

A simple managed bean, with an “userName” property.

package com.mkyong.form;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import java.io.Serializable; @ManagedBean
@SessionScoped
public class UserBean implements Serializable { private String userName; public String getUserName() {
return userName;
} public void setUserName(String userName) {
this.userName = userName;
}
}

2. View Page

Two pages for the demonstration.

demo.xhtml – render a textbox via “h:inputText”, button via “h:commandButton”, if the button is clicked, textbox value will be submitted to the “userBean.userName’ property via setUserName() method, and forward to “user.xhtml”.

<?xml version="1.0" encoding="UTF-8"?>
<!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://java.sun.com/jsf/html"> <h:body>
<h1>JSF 2 textbox example</h1> <h:form>
<h:inputText value="#{userBean.userName}" />
<h:commandButton value="Submit" action="user" />
</h:form> </h:body>
</html>

user.xhtml – display the submitted textbox value via “h:outputText

<?xml version="1.0" encoding="UTF-8"?>
<!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://java.sun.com/jsf/html"> <h:body>
<h1>JSF 2 textbox example</h1> Submitted value : <h:outputText value="#{userBean.userName}" /> </h:body>
</html>

3. Demo

URL : http://localhost:8080/JavaServerFaces/

Display “demo.xhtml” page



If the button is clicked, display “user.xhtml” page, and the submitted textbox value

j

最新文章

  1. HDU 1087 简单dp,求递增子序列使和最大
  2. C#面向对象总结2
  3. C#:文件、文件夹特别操作
  4. 12个强大的Chrome插件
  5. Python 网页爬虫
  6. Android智能聊天机器人
  7. C#读书
  8. Tomcat配置文件Executor元素属性介绍
  9. 前端cookie操作用到的一些小总结
  10. Spring中Bean的生命周期及其扩展点
  11. twitter api
  12. JavaIO流中的拷贝
  13. centos7发行版号对应基于RHEL Source(版本)对照表
  14. Makefile 中的.PHONY
  15. Netty---入门程序,搭建Websocket 服务器
  16. word中括号中公式对齐
  17. python版 google密码认证器
  18. 关于Unity3d的世界空间和本地空间的一点思考
  19. neutron源码分析(一)OpenStack环境搭建
  20. lda:变分的推导

热门文章

  1. WIN7建立网络映射磁盘
  2. 函数buf_page_get_gen
  3. Discuz 5.x/6.x/7.x投票SQL注入分析
  4. javascript数组详解
  5. SQL复制表及表结构
  6. 五大主流SQL数据库
  7. 利用nginx+lua+memcache实现灰度发布
  8. 【JSP】弹出带输入框可 确认密码 对话框
  9. delphi 转换sql server 中的 bit类型
  10. K2 blackpearl 流程开发(二)