by Lancer
24. September 2009 15:13
JSF - JavaServer Faces Technology
系统安装:
1) 安装并配置 JDK 1.5 or above
2) 安装 Eclipse IDE for Java EE Developers
3) 安装 JBOSS
1) 在磁盘上创建一个文件夹用来存放项目:
例如: E:\Development\Study\ForShow\JSF
2)打开Eclipse,工作空间选择刚刚创建的文件夹。

3)关掉欢迎界面后,在项目浏览器中右键新建动态Web项目:

4) 填写项目名称,并新建目标服务器(新建刚刚安装的JBOSS服务器),配置选择JSF1.2:

5)按下一步,(JAVA)下一步,(Web Module)下一步,(JSF Capabilities):

6) 选择Disable Library Configuration, 点击完成。

7)项目创建成功。

8)修订配置:/myJSF/WebContent/WEB-INF/Web.xml. 添加servlet-mapping到*.JSF

9) 在/myJSF/WebContent/WEB-INF/下面创建一个index.html来作为启示页。
10)在JAVA Resource:src下面新建一个package和类:
package apj.myJSF;
public class UserBean {
private String name;
private String password;
private String errMessage;
public UserBean() {
this.name = "";
this.password = "";
this.errMessage = "";
}
public UserBean(String name, String password) {
this.name = name;
this.password = password;
this.errMessage = "";
}
public void setName(String name) {
this.name = name;
}
public String getName() {
return name;
}
public void setPassword(String password) {
this.password = password;
}
public String getPassword() {
return password;
}
public void setErrMessage(String errMessage) {
this.errMessage = errMessage;
}
public String getErrMessage() {
return errMessage;
}
public String verify() {
if (!name.equals("Lancer") || !password.equals("123456")) {
errMessage = "Name or password is not correct : ";
errMessage = errMessage + name + "|" + password;
return "failure";
} else {
return "success";
}
}
}
11) 在/myJSF/WebContent/目录下创建login.jsp文件。
"text/html;charset=Big5"%>
<html>
<head>
<title>Insert title here</title>
</head>
<body>
<f:view>
<h:form>
<h3>Please input your name</h3>
<h:outputText value="#{user.errMessage}"/><p>
Name: <h:inputText value="#{user.name}"/><p>
Password: <h:inputSecret value="#{user.password}"/><p>
<h:commandButton value="Login" action="#{user.verify}"/>
</h:form>
</f:view>
</body>
</html>
12) 在/myJSF/WebContent/目录下创建welcome.jsp文件。
<%@ taglib uri="http: prefix="f" %>
<%@ taglib uri="http: prefix="h" %>
<%@page contentType="text/html;charset=Big5"%>
<html>
<head>
<title>Insert title here</title>
</head>
<body>
<f:view>
Hello <h:outputText value="#{user.name}"/>!
<h3>Welcome JavaServer Faces!</h3>
</f:view>
</body>
</html>
13)打开/myJSF/WebContent/WEB-INF/faces-config.xml 的源码视图,添加login.jsp到welcome.jsp的控制,以及UserBean的注册。
<?xml version="1.0"?>
<!DOCTYPE faces-config PUBLIC
"-
"http:>
<faces-config>
<navigation-rule>
<from-view-id>/longin.jsp</from-view-id>
<navigation-case>
<from-outcome>success</from-outcome>
<to-view-id>/welcome.jsp</to-view-id>
</navigation-case>
<navigation-case>
<from-outcome>failure</from-outcome>
<to-view-id>/longin.jsp</to-view-id>
</navigation-case>
</navigation-rule>
<managed-bean>
<managed-bean-name>user</managed-bean-name>
<managed-bean-class>
apj.myJSF.UserBean
</managed-bean-class>
<managed-bean-scope>session</managed-bean-scope>
</managed-bean>
</faces-config>
14)修改index.html,添加一个link到login.jsf.
<a href="login.jsf">login.jsf</a>
15)运行myJSF项目(Run on Server).



405ada49-0874-47ff-abfd-7e3d783aa1c3|1|3.0
Tags: jsf, jboss
技术文章