项目环境:
1、JSF2.1+
2、JDK1.6+
3、Tomcat6.0+
4、Eclipse3.6+ 我用的Indigo
目录结构
jsf2.0只需一个javax.faces-2.x.x.jar即可,无需依赖额外的jar包。前端页面必须用xhtml,无需和后台java文件同名。
html代码
<!DOCTYPE HTML> <html xmlns="http://www.w3.org/1999/xhtml" xmlns:h="http://java.sun.com/jsf/html" xmlns:f="http://java.sun.com/jsf/core"> <head> <meta charset="utf-8" /> <title>Hello World</title> </head> <body> <h:form> <h:inputText binding="#{hello.text}" /> <h:commandButton value="确定" actionListener="#{hello.action}" /> </h:form> </body> </html>页面内必须要用<h:form>把组件包含住。<h:inputText>是一个文本输入框,binding="#{hello.text}"标明此组件与后台hello bean中的text组件绑定映射关系。前端页面用到的组件,通常都要绑定后台java managed bean中对应的组件,实现同步关联,以便在后台读写组件的属性。<h;commandButton>是一个按钮,这里后台不需要控制它的属性,所以没有在后台绑定组件。actionListener="#{hello.action}"表示按钮触发后会调用后台hello bean中的action方法,在action方法中我们会设置<h:inputText>的值,显示hello world!
这里注意的关键点有:
1、必须有<h:form>
2、通常页面上用了什么组件,后台bean就要对应的有一个组件和页面组件绑定,建立映射关系,建议不要在后台bean中用值绑定方式,只用组件绑定方式。组件绑定方式易懂、易读、易操作。在同一项目中同时采用组件绑定和值关联两种方式特别容易混淆代码逻辑。
3、只需知道actionListener的用法就行了,其它的valueChangeListener、action等实际应用极少,且初学者不易理解,我建议都用actionListener处理。
java代码
package test; import java.text.SimpleDateFormat; import java.util.Calendar; import javax.faces.bean.ManagedBean; import javax.faces.component.html.HtmlInputText; @ManagedBean public class Hello { private HtmlInputText text; public void action() { Calendar now = Calendar.getInstance(); text.setValue("hello world! " + new SimpleDateFormat("hh:mm:ss").format(now.getTime())); } public HtmlInputText getText() { return text; } public void setText(HtmlInputText text) { this.text = text; } }
JSF2.0不再使用xml配置managed bean,而是改用annotation。只需给java类加上@managedBean标注,即可声明此类为jsf managed bean。Hello.java中的text变量必须用get set公布出去,前端页面才能绑定。action方法也不再像JSF1.x中需要加(ActionEvent e)参数。当页面按钮触发时,会调用action方法,此方面中,设置text的value属性,改变text显示的文字。
web.xml
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="HelloWorld" version="2.5"> <display-name>HelloWorld</display-name> <listener> <listener-class>com.sun.faces.config.ConfigureListener</listener-class> </listener> <servlet> <servlet-name>Faces Servlet</servlet-name> <servlet-class>javax.faces.webapp.FacesServlet</servlet-class> <load-on-startup>0</load-on-startup> </servlet> <servlet-mapping> <servlet-name>Faces Servlet</servlet-name> <url-pattern>/faces/*</url-pattern> <url-pattern>*.faces</url-pattern> <url-pattern>*.jsf</url-pattern> </servlet-mapping> <welcome-file-list> <welcome-file>index.html</welcome-file> </welcome-file-list> </web-app>web.xml声明必须用2.5以上版本,<listener-class>com.sun.faces.config.ConfigureListener</listener-class>和Faces Servlet中的<load-on-startup>0</load-on-startup>是为了让JSF2.0在jboss7和websphere7中也能运行,tomcat中可以不加
运行结果
url:http://localhost:8080/HelloWorld/hello.faces 或者http://localhost:8080/HelloWorld/faces/hello.xhtml
下载示例代码