JSF Struts Spring Hibernate 整合

JSF1.1+Struts2.1+Spring2.0+Hibernate3.1

 图示:
JSF Struts Spring Hibernate 整合

 JSF1.1 Jar包:
JSF Struts Spring Hibernate 整合

 MyFaces1.1 Jar包:
JSF Struts Spring Hibernate 整合

 Struts2.1 Jar包:

JSF Struts Spring Hibernate 整合

 Spring2.0 Jar包:

JSF Struts Spring Hibernate 整合

 Hibernate3.1 Jar包:
     官方下载的Hibernate3.1文件夹lib下的全部Jar包

 

 其他必须包:
JSF Struts Spring Hibernate 整合


 

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>*.jsf</url-pattern> 
  11.     </servlet-mapping> 
  12.       
  13.     <!-- Struts2 Configuration --> 
  14.     <filter> 
  15.         <filter-name>struts2</filter-name> 
  16.         <filter-class> 
  17.             org.apache.struts2.dispatcher.FilterDispatcher  
  18.         </filter-class> 
  19.     </filter> 
  20.  
  21.     <filter-mapping> 
  22.         <filter-name>struts2</filter-name> 
  23.         <url-pattern>/*</url-pattern> 
  24.     </filter-mapping> 
  25.  
  26.     <filter> 
  27.         <filter-name>struts-cleanup</filter-name> 
  28.         <filter-class> 
  29.             org.apache.struts2.dispatcher.ActionContextCleanUp  
  30.         </filter-class> 
  31.     </filter> 
  32.  
  33.     <filter-mapping> 
  34.         <filter-name>struts-cleanup</filter-name> 
  35.         <url-pattern>/*</url-pattern> 
  36.     </filter-mapping> 
  37.  
  38.     <context-param> 
  39.         <param-name>contextConfigLocation</param-name> 
  40.         <param-value>/WEB-INF/applicationContext.xml</param-value> 
  41.     </context-param> 
  42.       
  43.     <!-- Spring Configuration --> 
  44.     <listener> 
  45.         <listener-class> 
  46.             org.springframework.web.context.ContextLoaderListener  
  47.         </listener-class> 
  48.     </listener> 
  49.  
  50.     <filter> 
  51.         <filter-name>CharacterEncoding</filter-name> 
  52.         <filter-class> 
  53.             org.springframework.web.filter.CharacterEncodingFilter  
  54.         </filter-class> 
  55.         <init-param> 
  56.             <param-name>encoding</param-name> 
  57.             <param-value>gb2312</param-value> 
  58.         </init-param> 
  59.     </filter> 
  60.     <filter-mapping> 
  61.         <filter-name>CharacterEncoding</filter-name> 
  62.         <url-pattern>/*</url-pattern> 
  63.     </filter-mapping> 
  64.       
  65.     <!-- Sun for JSF Listener --> 
  66.     <listener> 
  67.         <listener-class> 
  68.             com.sun.faces.config.ConfigureListener  
  69.         </listener-class> 
  70.     </listener> 
  71.  
  72.     <!-- MyFaces for JSF Listener --> 
  73.     <listener> 
  74.         <listener-class> 
  75.             org.apache.myfaces.webapp.StartupServletContextListener  
  76.         </listener-class> 
  77.     </listener> 
  78.  
  79.     <context-param> 
  80.         <param-name>javax.faces.STATE_SAVING_METHOD</param-name> 
  81.         <param-value>client</param-value> 
  82.     </context-param> 

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> 

struts.xml


  1. <!-- JSF Interceptors --> 
  2.     <package name="jsf" extends="jsf-default"> 
  3.         <interceptors> 
  4.             <interceptor-stack name="jsfFullStack"> 
  5.                 <interceptor-ref name="params" /> 
  6.                 <interceptor-ref name="basicStack" /> 
  7.                 <interceptor-ref name="jsfStack" /> 
  8.             </interceptor-stack> 
  9.         </interceptors> 
  10.         <default-interceptor-ref name="jsfFullStack" /> 
  11.     </package> 
  12.  
  13.     <!-- Struts Configuration --> 
  14.     <package name="main" namespace="/" extends="struts-default"> 
  15.         <action name="login" class="loginAction" method="login"> 
  16.             <result name="input">/input2.jsp</result> 
  17.             <result name="error">/error.jsp</result> 
  18.             <result name="success">/success.jsp</result> 
  19.         </action> 
  20.     </package> 

applicationContext.xml
      这里的Service和Action切记将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"/> 
  4.       
  5.     <!-- Action Configuration --> 
  6.     <bean id="loginAction" class="com.jsf.beans.User" scope="prototype"> 
  7.         <property name="service"><ref local="userService"/></property> 
  8.     </bean> 

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


  1. package com.jsf.beans;  
  2.  
  3. import java.io.Serializable;  
  4.  
  5. import com.jsf.service.UserService;  
  6. import com.opensymphony.xwork2.ActionContext;  
  7. import com.opensymphony.xwork2.ActionSupport;  
  8.  
  9. public class User extends ActionSupport implements Serializable{  
  10.     private String id;  
  11.     private String name;  
  12.     private String password;  
  13.     private Integer age;  
  14.     public User() {  
  15.         super();  
  16.     }  
  17.  
  18.     public String getId() {  
  19.         return id;  
  20.     }  
  21.  
  22.     public void setId(String id) {  
  23.         this.id = id;  
  24.     }  
  25.  
  26.     public String getName() {  
  27.         return name;  
  28.     }  
  29.  
  30.     public void setName(String name) {  
  31.         this.name = name;  
  32.     }  
  33.  
  34.     public String getPassword() {  
  35.         return password;  
  36.     }  
  37.  
  38.     public void setPassword(String password) {  
  39.         this.password = password;  
  40.     }  
  41.  
  42.     public Integer getAge() {  
  43.         return age;  
  44.     }  
  45.  
  46.     public void setAge(Integer age) {  
  47.         this.age = age;  
  48.     }  
  49.  
  50.     private UserService service;  
  51.  
  52.     public UserService getService() {  
  53.         return service;  
  54.     }  
  55.  
  56.     public void setService(UserService service) {  
  57.         this.service = service;  
  58.     }  
  59.          //jsf的执行方法  
  60.     public String execute() {  
  61.         if(service.validate(this.id, this.password)) {  
  62.             User user = service.getUser(this.id);  
  63.             this.setAge(user.getAge());  
  64.             this.setName(user.getName());  
  65.             return "success";  
  66.         }  
  67.         return "error";  
  68.     }  
  69.       
  70.          //struts执行方法  
  71.     public String login() throws Exception {  
  72.         if(service.validate(this.id, this.password)) {  
  73.             User user = service.getUser(this.id);  
  74.             ActionContext.getContext().put("userBean", user);  
  75.             return "success";  
  76.         }  
  77.         return "error";  
  78.     }  
  79. }  

input.jsp

    纯JSF方式创建页面,如果以JSF方式提交表单,系统抛出异常
      >> 
javax.servlet.ServletException


  1. <%@ page language="java" pageEncoding="gb2312"%> 
  2. <%@ taglib uri="http://java.sun.com/jsf/core" prefix="f"%> 
  3. <%@ taglib uri="http://java.sun.com/jsf/html" prefix="h"%> 
  4. <html> 
  5.     <head> 
  6.         <title>JSSH JSF</title> 
  7.     </head> 
  8.  
  9.     <body> 
  10.         <f:view> 
  11.             <h:form> 
  12.                 用户名:<h:inputText value="#{userBean.id}" /> 
  13.                 密码:<h:inputSecret value="#{userBean.password}" /> 
  14.                 <h:commandButton value="登录" action="#{userBean.execute}"></h:commandButton> 
  15.             </h:form> 
  16.         </f:view> 
  17.     </body> 
  18. </html> 

input2.jsp

      使用JSF与Struts混合标签创建页面,以Struts方式提交表单,系统正确运转
 


  1. <%@ page language="java" pageEncoding="gb2312"%> 
  2. <%@ taglib uri="/struts-tags" prefix="s"%> 
  3. <%@ taglib uri="http://java.sun.com/jsf/core" prefix="f"%> 
  4. <html> 
  5.     <head> 
  6.         <title>JSSH Struts</title> 
  7.     </head> 
  8.     <body> 
  9.     <f:view> 
  10.             <s:form action="login" namespace="/"> 
  11.                 <s:textfield name="id" label="用户名"/> 
  12.                 <s:password name="password" label="密码"/> 
  13.                 <s:submit value="登录" /> 
  14.             </s:form> 
  15.     </f:view> 
  16.     </body> 
  17. </html> 

 总结:
   

  1. 在web.xml中使用Sun和MyFaces对JSF的监听
  2. 配置faces-config.xml中要托管的解析器DelegatingVariableResolver
  3. 在struts.xml中建立JSF的拦截器,之后的package应该继承该配置
  4. 在页面中可以实现JSF与Struts混编,但是只能以struts方式进行表单提交,如果以JSF方式进行表单提交,将会抛出异常









本文转自 sundunjam 51CTO博客,原文链接:http://blog.51cto.com/sunspot/469073,如需转载请自行联系原作者
上一篇:SilverLight 4页面跳转大全


下一篇:java并发编程(4)--线程池的使用