呆羊在晒太阳  
公告
  • Y:哦,是你呀。
    X:我现在正在忙。
    Y:忙什么?
    X:呵呵,今天出太阳了,我把钱搬出来晒一晒。
    ***********************
    abc
    小叉
    很高兴能结识大家!
    ***********************
日历
<2005年10月>
2526272829301
2345678
9101112131415
16171819202122
23242526272829
303112345
统计
  • 随笔 - 164
  • 文章 - 2
  • 评论 - 196
  • 引用 - 0

导航

常用链接

留言簿(7)

随笔分类(158)

文章分类(2)

相册

log

搜索

  •  

积分与排名

  • 积分 - 69621
  • 排名 - 786

最新评论

阅读排行榜

 

You usually set up your web application as you would do with any other JSF web application. The following sample configuration files show you how to enable the above mentioned features.

The web.xml configuration:

WEB-INF/web.xml
<?xml version="1.0"?>
<!DOCTYPE web-app PUBLIC
  "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
  "http://java.sun.com/dtd/web-app_2_3.dtd">

<web-app>
    <!--
        The filter used to publish RequestHandledEvent to Spring.
        Should be mapped to the same url pattern than the FacesServlet.
    -->
    <filter>
        <filter-name>RequestHandled</filter-name>
        <filter-class>de.mindmatters.faces.spring.RequestHandledFilter</filter-class>
    </filter>
    <filter-mapping>
        <filter-name>RequestHandled</filter-name>
        <url-pattern>/faces/*</url-pattern>
    </filter-mapping>
    
    <!--
        The listener used to load the parent application context (the spring beans).
    -->
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>

    <!--
        The declaration of your FacesServlet.
        Map it to whatever url pattern you like.
    -->
    <servlet>
        <servlet-name>FacesServlet</servlet-name>
        <servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>FacesServlet</servlet-name>
        <url-pattern>/faces/*</url-pattern>
    </servlet-mapping>
</web-app>

A web.xml configuration with net.sourceforge.myfaces JSF-implementation 1.0.2 (because an internal spring-based VariableResolver is used a few features which are not supported by MyFaces yet, e.g. list-entries, map-entries..., are enabled):

WEB-INF/web.xml
<?xml version="1.0"?>
<!DOCTYPE web-app PUBLIC
  "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
  "http://java.sun.com/dtd/web-app_2_3.dtd">

<web-app>
    <!--
        The filter used to publish RequestHandledEvent to Spring.
        Should be mapped to the same url pattern than the FacesServlet.
    -->
    <filter>
        <filter-name>RequestHandled</filter-name>
        <filter-class>org.springframework.web.jsf.RequestHandledFilter</filter-class>
    </filter>
    <filter-mapping>
        <filter-name>RequestHandled</filter-name>
        <url-pattern>/faces/*</url-pattern>
    </filter-mapping>
    
    <!--
        The listener used to load the parent application context (the spring beans).
    -->
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>

    <!--
        The declaration of MyFaces startup-listener.
    -->
    <listener>
        <listener-class>net.sourceforge.myfaces.webapp.StartupServletContextListener</listener-class>
    </listener>

    <!--
        The declaration of your FacesServlet.
        Map it to whatever url pattern you like.
    -->
    <servlet>
        <servlet-name>FacesServlet</servlet-name>
        <servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>FacesServlet</servlet-name>
        <url-pattern>/faces/*</url-pattern>
    </servlet-mapping>
</web-app>

Example

In this example, we just use two beans:

  • example.NameBean
    with one property:
    • name
    and two methods that receive JSF events:
    • public void action(ActionEvent event)
    • public void valueChanged(ValueChangeEvent event)
  • example.ReferencingBean
    with one property:
    • referencedBean

WEB-INF/faces-config.xml (partial)
<!-- a purely JSF managed bean -->
<managed-bean>
    <managed-bean-name>jsfBean</managed-bean-name>
    <managed-bean-class>example.NameBean</managed-bean-class>
    <managed-bean-scope>session</managed-bean-scope>
    <managed-property>
        <property-name>name</property-name>
    </managed-property>
</managed-bean>

<!-- a de.mindmatters.faces.spring.SpringBeanFactory used to define the scope of a Spring managed bean -->
<managed-bean>
    <managed-bean-name>scopedAccessSpringBean</managed-bean-name>
    <managed-bean-class>de.mindmatters.faces.spring.SpringBeanFactory</managed-bean-class>
    <managed-bean-scope>session</managed-bean-scope>
</managed-bean>

<!-- a purely JSF managed bean that holds a reference to a Spring managed bean -->
<managed-bean>
    <managed-bean-name>referencingBean</managed-bean-name>
    <managed-bean-class>example.ReferencingBean</managed-bean-class>
    <managed-bean-scope>session</managed-bean-scope>
    <managed-property>
        <property-name>referencedBean</property-name>
        <value>#{managedPropertyAccessSpringBean}</value>
    </managed-property>
</managed-bean>

WEB-INF/applicationContext.xml (partial)
<!-- a purely Spring managed bean -->
<bean id="directAccessSpringBean" class="example.NameBean"/>

<!-- a Spring managed bean supplied with a scope within JSF -->
<bean id="scopedAccessSpringBean" class="example.NameBean" singleton="false"/>

<!--
    a purely Spring managed bean being referenced by a JSF managed bean
    (but which of course can be accessed directly as well)
->
<bean id="managedPropertyAccessSpringBean" class="example.NameBean" singleton="false"/>

test.jsp
<%@ taglib uri="http://java.sun.com/jsf/html" prefix="h" %>
<%@ taglib uri="http://java.sun.com/jsf/core" prefix="f" %>
<html>
    <head><title>test</title></head>
    <body bgcolor="white">
        <f:view>
            <h:messages/>
            <h:form>
                purely JSF managed bean:
                <h:inputText value="#{jsfBean.name}"
                             valueChangeListener="#{jsfBean.valueChanged}"/>
                <h:commandButton value="action"
                                 actionListener="#{jsfBean.action}"/>
                <br/>
                purely Spring managed bean:
                <h:inputText value="#{directAccessSpringBean.name}"
                             valueChangeListener="#{directAccessSpringBean.valueChanged}"/>
                <h:commandButton value="action"
                                 actionListener="#{directAccessSpringBean.action}"/>
                <br/>
                scoped Spring managed bean:
                <h:inputText value="#{scopedAccessSpringBean.name}"
                             valueChangeListener="#{scopedAccessSpringBean.valueChanged}"/>
                <h:commandButton value="action"
                                 actionListener="#{scopedAccessSpringBeanaction}"/>
                <br/>
                referenced Spring managed bean:
                <h:inputText value="#{referencingBean.referencedBean.name}"
                             valueChangeListener="#{referencingBean.referencedBean.valueChanged}"/>
                <h:commandButton value="action"
                                 actionListener="#{referencingBean.referencedBean.action}"/>
                <br/>
                <h:commandButton id="submit" action="save" value="save"/>
            </h:form>
        </f:view>
    </body>
</html>
posted on 2005-10-15 21:14 小叉 阅读(418) 评论(0)  编辑  收藏 所属分类: jsf

只有注册用户登录后才能发表评论。


网站导航:
 
 
Copyright © 小叉 Powered by: 博客园 模板提供:沪江博客