`

在Eclipse 3.5中使用JSF 2.0开发Web应用环境配置及一个简单例子(系列之二)

    博客分类:
  • jsf
阅读更多
       这里假设你对eclipse的开发环境已经比较熟悉了,我们直接从配置和开发开始,与eclipse相关的功能就不多做介绍了。另外还假设你对JSP、JSTL等都有初步了解。

一、    配置JSF  2.0开发环境
首先下载软件:Eclipse  IDE  for  Java  EE  Developers  (189  MB)或者Myeclipse  7.5  、JSF  2.0的Beta1版本(mojarra-2.0.0-Beta1)
下载地址:
eclipse:http://www.eclipse.org/downloads/
mojarra2.0.0bate1:
https://javaserverfaces.dev.java.net/files/documents/1866/135560/mojarra-2.0.0-Beta1-binary.zip

好了,配置好eclipse的开发环境。将获得的JSF  2.0实现包mojarra-2.0.0-Beta1-binary.zip解压缩,可以看见一个lib目录,目录下的两个文件jsf-api.jar  jsf-impl.jar就是JSF  2.0的接口和参考实现了,也是我们开发中要使用的了。下面我们就一步步来配置开发环境。

1、    用Eclipse创建一个Web  Project,选择Java  EE  5.0,取名TestJsf2.0,其他一切如常,这样创建了一个工程TestJsf2.0。
2、    打开工程TestJsf2.0的编译路径配置:工程TestJsf2.0上右键点击,选择Build  Path  ->Configure  Build  Path…,在Libraries  中添加你刚才下载的两个jar包:jsf-api.jar  jsf-impl.jar,并在Order  and  Export中将这两个jar包移到Java  EE  5  libraries前面(这一步非常重要,由于java  ee  5中包含了JSF  1.2,如果不改动编译使用jar包的顺序,首先采用的就是JSF  1.2,这样很多JSF  2.0中的功能就无法使用了)。
3、    在web.xml中加入JSF支持
    <servlet>
        <servlet-name>Faces  Servlet</servlet-name>
        <servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>

        <servlet-mapping>
                <servlet-name>Faces  Servlet</servlet-name>
                <url-pattern>/faces/*</url-pattern>
                <url-pattern>*.xhtml</url-pattern>
        </servlet-mapping>

这样设置后,整个初步配置工作就完成了,接下来我们就来看看一个初步的例子,页面表现采用新加入标准的facelets。

二、    一个简单的例子。

我们先来看看两个文件:

helloworld.xhtml

<!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"
            xmlns:f="http://java.sun.com/jsf/core"
            xmlns:ui="http://java.sun.com/jsf/facelets">
    <h:head>
        <title>Hello  World</title>
        <meta  http-equiv="keywords"  content="enter,your,keywords,here"  />
        <meta  http-equiv="description"  content="A  short  description  of  this  page."  />
        <meta  http-equiv="content-type"  content="text/html;  charset=UTF-8"  />
    </h:head>
    <h:body>
        <h:form  id="form">
            <div  style="padding:100px  0  0  100px;font-size:22px;font-weight:bold">
                    Hello,#{hello.name}!
            </div>
        </h:form>
    </h:body>
</html>

HelloWorld.java

package  test;
import  javax.faces.bean.ManagedBean;
import  javax.faces.bean.SessionScoped;

@ManagedBean(name="hello")
@SessionScoped
public  class  HelloWorld  implements  java.io.Serializable{
    private  static  final  long  serialVersionUID  =  6866250699219535733L;
    private  String  name;

    /**
      *  @return  the  name
      */
    public  String  getName()  {
        this.name  =  "World";
        return  name;
    }
}


看看helloworld.xhtml,如果你对facelets比较熟悉,那对这样的写法就不陌生了。其中ui头就是新加入的facelets表现标签了。另外相对于JSF  1.2还加入了  h:head,  h:body。#{hello.name}这一个就直接关联了ManagedBean中的name属性了,现在不用一定加在h:outputText标签中了,这是为了简洁加入的新的方式。

再看看ManagedBean,其中最大的变化就是加入了Annotation:ManagedBean和SessionScoped,这是JSF  2.0中新引入的,这样就可以直接简化了faces-config.xml。这个例子中根本不需要这个配置文件就可以运行了。好了,在浏览器中输入:http://localhost:8080/TestJsf2.0/helloworld.xhtml  就能看见结果了。
分享到:
评论
1 楼 paomo30000 2010-10-19  
请问为什么我在输入了http://localhost:8080/TestJsf2.0/helloworld.xhtml 以后,浏览器变为下载了呢?我用的TOMCAT6.18 其中conf/web.xml 里的conf/web.xml 也有<mime-mapping>
        <extension>xhtml</extension>
        <mime-type>application/xhtml+xml</mime-type>
    </mime-mapping>

为什么总是下载?

相关推荐

Global site tag (gtag.js) - Google Analytics