(
转自http://www.blogjava.net/gaofeng/articles/127842.html
作者:Java.net
)
Eclipse3.3刚刚发布,正在学习JSF,于是使用Eclipse3.3做了一个JSF的Demo,很简单,主要是页面的跳转、组件和Bean的绑定等基础...
1、工具准备: Eclipse3.3 WTP2.0 (最好下载一个all-in-one的版本..省的麻烦)...
依赖包:jsf1.2.04-p02,目前的最新版本.内含:jsf-api.jar;jsf-impl.jar.
jstl.jar;standard.jar;commons-beanutils.jar;commons-collections.jar
commons-digester.jar
Web服务器使用Tomcat6..我用的是6.0.10,目前最近的好像是6.0.13.
2、在Eclipse中新建一个Dynamic Web Project...Project name任意..Target Runtime选择Apache Tomcat v6.0,下一步可以设置应用的组件,这里把JSF选上.其余默认..
3、完成后,项目的文件结构已经建好,开始编码:
首先定义一个PersionBean:
1 package com.xzuf.jsf;
2 import java.io.Serializable;
3 /**
4 * PersonBean
5 * @author xzgf <a href='mailto:
[email protected]'>
[email protected]</a>
6 * @create 2007-7-2
7 */
8 public class PersonBean implements Serializable {
9 private String name;
10 private String password;
11 /**
12 * @return the password
13 */
14 public String getPassword() {
15 return password;
16 }
17 /**
18 * @param password the password to set
19 */
20 public void setPassword(String password) {
21 this.password = password;
22 }
23 public String getName() {
24 return name;
25 }
26 public void setName(String name) {
27 this.name = name;
28 }
29 }
30 接着在faces-config.xml注册刚定义的PersonBean,使得可以在应用中直接使用bean的实例.
<managed-bean>
<description>
jsf test bean</description>
<managed-bean-name>
personBean</managed-bean-name>
<managed-bean-class>
com.xzuf.jsf.PersonBean</managed-bean-class>
<managed-bean-scope>
session</managed-bean-scope>
</managed-bean>当然,在新版的Eclipse中已经可以图形化的对Bean进行定义了,只要使用默认的打开方式,就可以看到一个非常直观的界面..方便了各种配置...
接着定义两个jsp页面,并增加jsf标签..完整的代码请到附件中下载..
firstjsf.jsp
<body>
<center>
<h3>Please enter your user name and password</h3>
<f:view>
<h:form id="myForm">
<h:panelGrid columns="2">
<h:outputText value="User Name:"></h:outputText>
<h:inputText value="#{personBean.name}" required="true"></h:inputText>
<h:outputText value="Password:"></h:outputText>
<h:inputSecret id="userpassword" value="#{personBean.password}" required="true"> </h:inputSecret>
<h:outputText value=""></h:outputText>
<h:commandButton value="Login" action="login"></h:commandButton>
<h:graphicImage id="waveImg" url="/images/wave.med.gif"></h:graphicImage>
<h:message showSummary="true" showDetail="true"
style="color: red; font-family: 'New Century Schoolbook', serif; font-style: oblique"
id="errors1" for="userpassword"/>
</h:panelGrid>
</h:form>
</f:view>
</center>welcome.jsp
<body>
<f:view>
<h:outputText value="#{personBean.name}"></h:outputText> Hello!!
<br>Your password is :
<h:outputLabel value="#{personBean.password}"></h:outputLabel>
<h3>Welcome to JavaServerFace</h3>
</f:view>
</body>还是在faces-config.xml.中配置页面导航,可以通过拖拽设置...
全部保存后,右击项目,在Debug中选择Debug on server.....