JSF Demo Project (Study notes)

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和类:

UserBean.java
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文件。

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文件。

welcome.jsp
<%@ taglib uri="http://java.sun.com/jsf/core" prefix="f" %>
<%@ taglib uri="http://java.sun.com/jsf/html" 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的注册。

faces-config.xml
<?xml version="1.0"?>
<!DOCTYPE faces-config PUBLIC
"-//Sun Microsystems, Inc.//DTD JavaServer Faces Config 1.0//EN"
"http://java.sun.com/dtd/web-facesconfig_1_0.dtd">

<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).

 

Tags: ,

技术文章

Comments

9/25/2009 3:37:23 PM #

lancer

JSF Bean调用EJB3的样例:

    Properties props= new Properties();
    props.setProperty("java.naming.factory.initial", "org.jnp.interfaces.NamingContextFactory");
    props.setProperty("java.naming.provider.url", "localhost:1099");
    
    try
    {
      InitialContext ctx = new InitialContext(props);
      
      SayHelloRemote s = (SayHelloRemote)ctx.lookup("JSFWebEAR/SayHello/remote");
      strMessage = s.SayHi(name);
    }
    catch (NamingException e)
    {
      strMessage= e.getMessage();  
    }

lancer | Reply

Add comment


(Will show your Gravatar icon)

biuquote
  • Comment
  • Preview
Loading



Copyright © 2009 APJ Software

最新评论

Comment RSS

公告

欢迎使用APJ Blog!

日历

<<  September 2010  >>
MoTuWeThFrSaSu
303112345
6789101112
13141516171819
20212223242526
27282930123
45678910

View posts in large calendar