JSF1.1 + Spring2.0 + Hibernate3.1

 图示:

 JSF1.1 Jar包:

 MyFaces1.1 Jar包:

 Spring与Hibernate包的配置与文章《SSH1基本配置(Struts1.2 + Spring2.0 + Hibernate3.1)》一致
地址:http://sunspot.blog.51cto.com/372554/468430

web.xml 配置

  1. <!-- JSF Configuration --> 
  2.     <servlet> 
  3.         <servlet-name>FacesServlet</servlet-name> 
  4.         <servlet-class>javax.faces.webapp.FacesServlet</servlet-class> 
  5.         <load-on-startup>0</load-on-startup> 
  6.     </servlet> 
  7.  
  8.     <servlet-mapping> 
  9.         <servlet-name>FacesServlet</servlet-name> 
  10.         <url-pattern>*.do</url-pattern> 
  11.     </servlet-mapping> 
  12.  
  13.     <!-- Spring Configuration --> 
  14.     <context-param> 
  15.         <param-name>contextConfigLocation</param-name> 
  16.         <param-value>/WEB-INF/applicationContext.xml</param-value> 
  17.     </context-param> 
  18.  
  19.     <listener> 
  20.         <listener-class> 
  21.             org.springframework.web.context.ContextLoaderListener  
  22.         </listener-class> 
  23.     </listener> 
  24.  
  25.     <filter> 
  26.         <filter-name>CharacterEncoding</filter-name> 
  27.         <filter-class> 
  28.             org.springframework.web.filter.CharacterEncodingFilter  
  29.         </filter-class> 
  30.         <init-param> 
  31.             <param-name>encoding</param-name> 
  32.             <param-value>gb2312</param-value> 
  33.         </init-param> 
  34.     </filter> 
  35.     <filter-mapping> 
  36.         <filter-name>CharacterEncoding</filter-name> 
  37.         <url-pattern>/*</url-pattern> 
  38.     </filter-mapping> 
  39.  
  40.     <!-- Sun for JSF Listener Configuration --> 
  41.     <listener> 
  42.         <listener-class> 
  43.             com.sun.faces.config.ConfigureListener  
  44.         </listener-class> 
  45.     </listener> 
  46.  
  47.     <!-- MyFaces for JSF Listener Configuration --> 
  48.     <listener> 
  49.         <listener-class> 
  50.             org.apache.myfaces.webapp.StartupServletContextListener  
  51.         </listener-class> 
  52.     </listener> 
  53.       
  54.     <welcome-file-list> 
  55.         <welcome-file>input.faces</welcome-file> 
  56.     </welcome-file-list> 

faces-config.xml
        这里必须要配置Delegation Configuration

  1. <!-- Delegation Configuration --> 
  2.     <application> 
  3.         <variable-resolver>org.springframework.web.jsf.DelegatingVariableResolver</variable-resolver> 
  4.     </application> 
  5.       
  6.     <!-- Bean Configuration --> 
  7.     <managed-bean> 
  8.         <managed-bean-name>userBean</managed-bean-name> 
  9.         <managed-bean-class>com.jsf.beans.User</managed-bean-class> 
  10.         <managed-bean-scope>request</managed-bean-scope>
  11. <!-- 注入Service,userService已在Spring中配置 --> 
  12.         <managed-property> 
  13.             <property-name>service</property-name> 
  14.             <value>#{userService}</value> 
  15.         </managed-property> 
  16.     </managed-bean> 
  17.       
  18.     <!-- Navigation Configuration --> 
  19.     <navigation-rule> 
  20.         <from-view-id>/input.jsp</from-view-id> 
  21.         <navigation-case> 
  22.             <from-outcome>success</from-outcome> 
  23.             <to-view-id>/success.jsp</to-view-id> 
  24.         </navigation-case> 
  25.         <navigation-case> 
  26.             <from-outcome>error</from-outcome> 
  27.             <to-view-id>/error.jsp</to-view-id> 
  28.         </navigation-case> 
  29.     </navigation-rule> 

applicationContext.xml
      这里的Service切记将scope设置为prototype类型,该类型持有状态对象

  1. <!-- Service Configuration --> 
  2. <bean name="userService" id="userService" class="com.jsf.service.UserService" 
  3.         factory-method="getInstance" lazy-init="false" scope="prototype"/> 

User.java 
     
UserService与SSH1中的一致。 
            
http://sunspot.blog.51cto.com/372554/468430

 

  1. package com.jsf.beans;  
  2. import java.io.Serializable;  
  3. import com.jsf.service.UserService;  
  4.  
  5. public class User  implements Serializable{  
  6.     private String id;  
  7.     private String name;  
  8.     private String password;  
  9.     private Integer age;  
  10.     public User() {  
  11.         super();  
  12.     }  
  13.     public String getId() {  
  14.         return id;  
  15.     }  
  16.  
  17.     public void setId(String id) {  
  18.         this.id = id;  
  19.     }  
  20.  
  21.     public String getName() {  
  22.         return name;  
  23.     }  
  24.  
  25.     public void setName(String name) {  
  26.         this.name = name;  
  27.     }  
  28.  
  29.     public String getPassword() {  
  30.         return password;  
  31.     }  
  32.  
  33.     public void setPassword(String password) {  
  34.         this.password = password;  
  35.     }  
  36.  
  37.     public Integer getAge() {  
  38.         return age;  
  39.     }  
  40.  
  41.     public void setAge(Integer age) {  
  42.         this.age = age;  
  43.     }  
  44.  
  45.     private UserService service;  
  46.  
  47.     public UserService getService() {  
  48.         return service;  
  49.     }  
  50.  
  51.     public void setService(UserService service) {  
  52.         this.service = service;  
  53.     }  
  54.  
  55.     public String execute() {  
  56.         if(service.validate(this.id, this.password)) {  
  57.             User user = service.getUser(this.id);  
  58.             this.setAge(user.getAge());  
  59.             this.setName(user.getName());  
  60.             return "success";  
  61.         }  
  62.         return "error";  
  63.     }  
  64. }  

 

 总结:
    重点在于配置faces-config.xml中要托管的解析器DelegatingVariableResolver   

    在web.xml中使用Sun对JSF的监听,MyFaces的监听可不使用