JSF和Spring都实现了依赖注入,通过将依赖对象注入到Pojo中去,从而实现了对象间的解耦。一般说来有四种方式整合JSF和Spring
1.使用Spring的WebApplicationContextUtils 通过Spring的WebApplicationContextUtils工具类,在ServletContext中获取ApplicationContext,通过这种方式来得到Spring Bean。 这种方式可以实现,但并不是最优方案,因为JSF的pojo明显的与Spring偶合起来了,是比较糟糕的实现。 2.通过为JSF实现一个祖先Pojo,专门处理ApplicationContext 专门做一个祖先Pojo,里面只用来通过WebApplicationContextUtils获取ApplicationContext,别的什么都不干,然后其它的Pojo从这个祖先类继承。 这种方式算是第一种方式的变种,较前一种方案稍微好了一点,但这样一来,每次增加Page都需要手工调整对应的java类的代码,稍微有点麻烦。 3.使用JSF-Spring.jar JSF-Spring.jar是专门用来让JSF和Spring整合的,使用它可以解决JSF和Spring偶合的问题,方案比较不错,不过这个jar包只要加入netbeans工程,Visual JSF 设计器就打不开了。所以在Netbeans上基本上没法用。 4.使用Spring自带的VariableResolver 这个方案是最优方案,在Netbeans中实现起来没有任何问题,而且也实现了JSF和Spring之间的解耦。 实现步骤简述如下 4.1.加入spring包 4.2.修改web.xml 增加如下内容 <context-param> <!--spring配置文件--> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/applicationContext*.xml</param-value> </context-param> <listener> <!--spring的上下文加载器Listener,用来创建ApplicationContext的--> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> 4.3. 修改faces-config.xml 在其中增加如下内容 <application> <variable-resolver> <!--org.springframework.web.jsf.DelegatingVariableResolver 使用这个也可以--> org.springframework.web.jsf.SpringBeanVariableResolver </variable-resolver> </application> 4.4. 增加spring bean DemoManager.java 代码如下 /* * To change this template, choose Tools | Templates * and open the template in the editor. */ package com.runz.demo.manager; /** * * @author jim */ public interface DemoManager { public String getMessage(); } DemoManagerImpl.java 代码如下 package com.runz.demo.manager.impl; import com.runz.demo.manager.DemoManager; /** * * @author jim */ public class DemoManagerImpl implements DemoManager { private String message; public String getMessage() { return message; } public void setMessage(String message) { this.message = message; } } 4.5. 修改applicationContext.xml 增加Bean的定义 <bean id="demoManager" class="com.runz.demo.manager.impl.DemoManagerImpl"> <property name="message" value="这是来自spring注入的消息,你看到了没有?"></property> </bean> 4.6. 设计JSF页面 在页面上拖转一个名为lblMessage的标签和一个名为btnShowMessage的按钮,双击按钮生成代码如下 public String btnShowMessage_action() { // 待做事项:处理操作。返回的值是一个导航 // 如果名称为 null,则返回到同一页。 } 4.7. 修改Page1.java文件 接下来就需要未Page1增加一个DemoManager字段并增加其Setter方法。然后在按钮的单击事件里填写代码。 package com.runz.faces; import com.runz.demo.manager.DemoManager; import com.sun.rave.web.ui.appbase.AbstractPageBean; import com.sun.webui.jsf.component.Body; import com.sun.webui.jsf.component.Button; import com.sun.webui.jsf.component.Form; import com.sun.webui.jsf.component.Head; import com.sun.webui.jsf.component.Html; import com.sun.webui.jsf.component.Label; import com.sun.webui.jsf.component.Link; import com.sun.webui.jsf.component.Page; import javax.faces.FacesException; /** * <p>Page bean that corresponds to a similarly named JSP page. This * class contains component definitions (and initialization code) for * all components that you have defined on this page, as well as * lifecycle methods and event handlers where you may add behavior * to respond to incoming events.</p> * * @author jim */ public class Page1 extends AbstractPageBean { private DemoManager demoManager; ...... //JSF的注入方法 public void setDemoManager(DemoManager demoManager) { this.demoManager = demoManager; } //这是对应的按钮事件 public String btnShowMessage_action() { // 待做事项:处理操作。返回的值是一个导航 // 如果名称为 null,则返回到同一页。 this.getLblMessage().setText(this.demoManager.getMessage()); return null; } } 4.8. 修改faces-config.xml中关于Page1的Manage bean的声明部分 <managed-bean> <managed-bean-name>Page1</managed-bean-name> <managed-bean-class>com.runz.faces.Page1</managed-bean-class> <managed-bean-scope>request</managed-bean-scope> <managed-property> <property-name>demoManager</property-name> <value>#{demoManager}</value> </managed-property> </managed-bean> 上面黑体字部分就是JSF的注入声明,#{变量名}的方式通过VariableResolver对象在Spring的ApplicationContext中检索同名Bean,找到后就注入倒JSF的Bean中去。 ok,大功告成 |