程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> JAVA編程 >> 關於JAVA >> JSF Spring Hibernate整合:JSH1

JSF Spring Hibernate整合:JSH1

編輯:關於JAVA

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 配置:

<!-- JSF Configuration -->
    <servlet> 
        <servlet-name>FacesServlet</servlet-name> 
        <servlet-class>javax.faces.webapp.FacesServlet</servlet-class> 
        <load-on-startup>0</load-on-startup> 
    </servlet> 
     
    <servlet-mapping> 
        <servlet-name>FacesServlet</servlet-name> 
        <url-pattern>*.do</url-pattern> 
    </servlet-mapping> 
     
    <!-- Spring Configuration -->
    <context-param> 
        <param-name>contextConfigLocation</param-name> 
        <param-value>/WEB-INF/applicationContext.xml</param-value> 
    </context-param> 
     
    <listener> 
        <listener-class> 
            org.springframework.web.context.ContextLoaderListener  
        </listener-class> 
    </listener> 
     
    <filter> 
        <filter-name>CharacterEncoding</filter-name> 
        <filter-class> 
            org.springframework.web.filter.CharacterEncodingFilter  
        </filter-class> 
        <init-param> 
            <param-name>encoding</param-name> 
            <param-value>gb2312</param-value> 
        </init-param> 
    </filter> 
    <filter-mapping> 
        <filter-name>CharacterEncoding</filter-name> 
        <url-pattern>/*</url-pattern> 
    </filter-mapping> 
     
    <!-- Sun for JSF Listener Configuration -->
    <listener> 
        <listener-class> 
            com.sun.faces.config.ConfigureListener  
        </listener-class> 
    </listener> 
     
    <!-- MyFaces for JSF Listener Configuration -->
    <listener> 
        <listener-class> 
            org.apache.myfaces.webapp.StartupServletContextListener  
        </listener-class> 
    </listener> 
          
    <welcome-file-list> 
        <welcome-file>input.faces</welcome-file> 
    </welcome-file-list>

faces-config.xml

 這裡必須要配置Delegation Configuration

<!-- Delegation Configuration -->
    <application> 
        <variable-

resolver>org.springframework.web.jsf.DelegatingVariableResolver</variable-resolver> 
    </application> 
          
    <!-- Bean Configuration -->
    <managed-bean> 
        <managed-bean-name>userBean</managed-bean-name> 
        <managed-bean-class>com.jsf.beans.User</managed-bean-class> 
        <managed-bean-scope>request</managed-bean-scope>
<!-- 注入Service,userService已在Spring中配置 -->
        <managed-property> 
            <property-name>service</property-name> 
            <value>#{userService}</value> 
        </managed-property> 
    </managed-bean> 
          
    <!-- Navigation Configuration -->
    <navigation-rule> 
        <from-view-id>/input.jsp</from-view-id> 
        <navigation-case> 
            <from-outcome>success</from-outcome> 
            <to-view-id>/success.jsp</to-view-id> 
        </navigation-case> 
        <navigation-case> 
            <from-outcome>error</from-outcome> 
            <to-view-id>/error.jsp</to-view-id> 
        </navigation-case> 
    </navigation-rule>

applicationContext.xml

這裡的Service切記將scope設置為 prototype類型,該類型持有狀態對象

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

User.java

UserService與SSH1中的一致。

http://sunspot.blog.51cto.com/372554/468430

package 

com.jsf.beans;  
import java.io.Serializable;  
import com.jsf.service.UserService;  
     
public class User  implements Serializable{  
    private String id;  
    private String name;  
    private String password;  
    private Integer age;  
    public User() {  
        super();  
    }  
    public String getId() {  
        return id;  
    }  
     
    public void setId(String id) {  
        this.id = id;  
    }  
     
    public String getName() {  
        return name;  
    }  
     
    public void setName(String name) {  
        this.name = name;  
    }  
     
    public String getPassword() {  
        return password;  
    }  
     
    public void setPassword(String password) {  
        this.password = password;  
    }  
     
    public Integer getAge() {  
        return age;  
    }  
     
    public void setAge(Integer age) {  
        this.age = age;  
    }  
     
    private UserService service;  
     
    public UserService getService() {  
        return service;  
    }  
     
    public void setService(UserService service) {  
        this.service = service;  
    }  
     
    public String execute() {  
        if(service.validate(this.id, this.password)) {  
            User user = service.getUser(this.id);  
            this.setAge(user.getAge());  
            this.setName(user.getName());  
            return "success";  
        }  
        return "error";  
    }  
}

總結:

重點在於配置faces-config.xml中要托管的解析器DelegatingVariableResolver

在 web.xml中使用Sun對JSF的監聽,MyFaces的監聽可不使用

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved