日期:2014-05-16  浏览次数:20368 次

JSF2.0的一个简单Demo

学习javaeetutorial6.pdf 106页到113页 附件中javaeetutorial6.zip

环增需求:JDK1.6??? 服务器:tomcat6.0.29? web项目?? 框架:JSF2.0

简单描述:项目启动后,进入欢迎界面faces/greeting.xhtml

?<welcome-file-list>
??????? <welcome-file>faces/greeting.xhtml</welcome-file>
???</welcome-file-list>

?

Servlet

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

?

交给JSF处理

进入faces-config.xml的配置

/greeting.xhtml

<navigation-rule>
??? <description>
??????? The decision rule used by the NavigationHandler to
??????? determine which view must be displayed after the
??????? current view, greeting.jsp is processed.
??? </description>
??? <from-view-id>/greeting.xhtml</from-view-id>
??? <navigation-case>
??????? <description>
??????????? Indicates to the NavigationHandler that the response.jsp
??????????? view must be displayed if the Action referenced by a
??????????? UICommand component on the greeting.jsp view returns
??????????? the outcome "success".
??????? </description>
????? <from-outcome>success</from-outcome>
????? <to-view-id>/response.xhtml</to-view-id>
??? </navigation-case>

? </navigation-rule>

?

?

由于请求的操作,应该action默认的不是success 即<from-outcome>success</from-outcome>
不符合,所以直接返回请求页面/greeting.xhtml

进入开始页面,

<managed-bean>
??? <description>
????? The backing bean that backs up the guessNumber Web application
??? </description>
??? <managed-bean-name>UserNumberBean</managed-bean-name>
??? <managed-bean-class>guessNumber.UserNumberBean</managed-bean-class>
??? <managed-bean-scope>session</managed-bean-scope>
??? <managed-property>
????? <property-name>minimum</property-name>
????? <property-class>long</property-class>
????? <value>0</value>
??? </managed-property>
??? <managed-property>
????? <property-name>maximum</property-name>
????? <property-class>long</property-class>
????? <value>10</value>
??? </managed-property>
? </managed-bean>

?

配的是Session作用域,所以每次请求的,bean只有第一次会被实例化(无参构造函数生成的随机数就被确定了,以后的请求这这个值是不会改变的),然后是返回结果页面response.xhtml只显示Bean中的UserNumberBean.response,如果用户输的value等于随机数就显示猜对了,否则显示猜错了。。

?

?

?

?

JSF的一个简单例子:

说明:

Web\WEB-INF\web.xml配置

<!--上下文参数--> 
<context-param>
        <param-name>javax.faces.PROJECT_STAGE</param-name>
        <param-value>Development</param-value>
    </context-param>
<!--servlet路径拦截-->
    <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>
    &l