日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 前端技术 > javascript >内容正文

javascript

JSF Struts Spring Hibernate 整合

發布時間:2024/4/14 javascript 36 豆豆
生活随笔 收集整理的這篇文章主要介紹了 JSF Struts Spring Hibernate 整合 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

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

?圖示:

?JSF1.1 Jar包:

?MyFaces1.1 Jar包:

?Struts2.1 Jar包:

?Spring2.0 Jar包:

?Hibernate3.1 Jar包:
?????官方下載的Hibernate3.1文件夾lib下的全部Jar包

?

?其他必須包:


?

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>*.jsf</url-pattern>?
  • ????</servlet-mapping>?
  • ???? ?
  • ????<!--?Struts2?Configuration?-->?
  • ????<filter>?
  • ????????<filter-name>struts2</filter-name>?
  • ????????<filter-class>?
  • ????????????org.apache.struts2.dispatcher.FilterDispatcher ?
  • ????????</filter-class>?
  • ????</filter>?
  • ?
  • ????<filter-mapping>?
  • ????????<filter-name>struts2</filter-name>?
  • ????????<url-pattern>/*</url-pattern>?
  • ????</filter-mapping>?
  • ?
  • ????<filter>?
  • ????????<filter-name>struts-cleanup</filter-name>?
  • ????????<filter-class>?
  • ????????????org.apache.struts2.dispatcher.ActionContextCleanUp ?
  • ????????</filter-class>?
  • ????</filter>?
  • ?
  • ????<filter-mapping>?
  • ????????<filter-name>struts-cleanup</filter-name>?
  • ????????<url-pattern>/*</url-pattern>?
  • ????</filter-mapping>?
  • ?
  • ????<context-param>?
  • ????????<param-name>contextConfigLocation</param-name>?
  • ????????<param-value>/WEB-INF/applicationContext.xml</param-value>?
  • ????</context-param>?
  • ???? ?
  • ????<!--?Spring?Configuration?-->?
  • ????<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?-->?
  • ????<listener>?
  • ????????<listener-class>?
  • ????????????com.sun.faces.config.ConfigureListener ?
  • ????????</listener-class>?
  • ????</listener>?
  • ?
  • ????<!--?MyFaces?for?JSF?Listener?-->?
  • ????<listener>?
  • ????????<listener-class>?
  • ????????????org.apache.myfaces.webapp.StartupServletContextListener ?
  • ????????</listener-class>?
  • ????</listener>?
  • ?
  • ????<context-param>?
  • ????????<param-name>javax.faces.STATE_SAVING_METHOD</param-name>?
  • ????????<param-value>client</param-value>?
  • ????</context-param>?
  • 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>?
  • struts.xml

  • <!--?JSF?Interceptors?-->?
  • ????<package?name="jsf"?extends="jsf-default">?
  • ????????<interceptors>?
  • ????????????<interceptor-stack?name="jsfFullStack">?
  • ????????????????<interceptor-ref?name="params"?/>?
  • ????????????????<interceptor-ref?name="basicStack"?/>?
  • ????????????????<interceptor-ref?name="jsfStack"?/>?
  • ????????????</interceptor-stack>?
  • ????????</interceptors>?
  • ????????<default-interceptor-ref?name="jsfFullStack"?/>?
  • ????</package>?
  • ?
  • ????<!--?Struts?Configuration?-->?
  • ????<package?name="main"?namespace="/"?extends="struts-default">?
  • ????????<action?name="login"?class="loginAction"?method="login">?
  • ????????????<result?name="input">/input2.jsp</result>?
  • ????????????<result?name="error">/error.jsp</result>?
  • ????????????<result?name="success">/success.jsp</result>?
  • ????????</action>?
  • ????</package>?
  • applicationContext.xml
    ????? 這里的Service和Action切記將scope設置為prototype類型,該類型持有狀態對象

  • <!--?Service?Configuration?-->?
  • ????<bean?name="userService"?id="userService"?class="com.jsf.service.UserService"?
  • ????????factory-method="getInstance"?lazy-init="false"?scope="prototype"/>?
  • ???? ?
  • ????<!--?Action?Configuration?-->?
  • ????<bean?id="loginAction"?class="com.jsf.beans.User"?scope="prototype">?
  • ????????<property?name="service"><ref?local="userService"/></property>?
  • ????</bean>?
  • User.java?
    ??????
    UserService與SSH1中的一致。?
    ?????????????
    http://sunspot.blog.51cto.com/372554/468430

  • package?com.jsf.beans; ?
  • ?
  • import?java.io.Serializable; ?
  • ?
  • import?com.jsf.service.UserService; ?
  • import?com.opensymphony.xwork2.ActionContext; ?
  • import?com.opensymphony.xwork2.ActionSupport; ?
  • ?
  • public?class?User?extends?ActionSupport?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; ?
  • ????} ?
  • ?????????//jsf的執行方法 ?
  • ????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"; ?
  • ????} ?
  • ???? ?
  • ?????????//struts執行方法 ?
  • ????public?String?login()?throws?Exception?{ ?
  • ????????if(service.validate(this.id,?this.password))?{ ?
  • ????????????User?user?=?service.getUser(this.id); ?
  • ????????????ActionContext.getContext().put("userBean",?user); ?
  • ????????????return?"success"; ?
  • ????????} ?
  • ????????return?"error"; ?
  • ????} ?
  • } ?
  • input.jsp

    ????純JSF方式創建頁面,如果以JSF方式提交表單,系統拋出異常
    ????? >>?javax.servlet.ServletException

  • <%@?page?language="java"?pageEncoding="gb2312"%>?
  • <%@?taglib?uri="http://java.sun.com/jsf/core"?prefix="f"%>?
  • <%@?taglib?uri="http://java.sun.com/jsf/html"?prefix="h"%>?
  • <html>?
  • ????<head>?
  • ????????<title>JSSH?JSF</title>?
  • ????</head>?
  • ?
  • ????<body>?
  • ????????<f:view>?
  • ????????????<h:form>?
  • ????????????????用戶名:<h:inputText?value="#{userBean.id}"?/>?
  • ????????????????密碼:<h:inputSecret?value="#{userBean.password}"?/>?
  • ????????????????<h:commandButton?value="登錄"?action="#{userBean.execute}"></h:commandButton>?
  • ????????????</h:form>?
  • ????????</f:view>?
  • ????</body>?
  • </html>?
  • input2.jsp

    ??????使用JSF與Struts混合標簽創建頁面,以Struts方式提交表單,系統正確運轉
    ?

  • <%@?page?language="java"?pageEncoding="gb2312"%>?
  • <%@?taglib?uri="/struts-tags"?prefix="s"%>?
  • <%@?taglib?uri="http://java.sun.com/jsf/core"?prefix="f"%>?
  • <html>?
  • ????<head>?
  • ????????<title>JSSH?Struts</title>?
  • ????</head>?
  • ????<body>?
  • ????<f:view>?
  • ????????????<s:form?action="login"?namespace="/">?
  • ????????????????<s:textfield?name="id" label="用戶名"/>?
  • ????????????????<s:password?name="password" label="密碼"/>?
  • ????????????????<s:submit?value="登錄"?/>?
  • ????????????</s:form>?
  • ????</f:view>?
  • ????</body>?
  • </html>?
  • ?總結:
    ???

  • 在web.xml中使用Sun和MyFaces對JSF的監聽
  • 配置faces-config.xml中要托管的解析器DelegatingVariableResolver
  • 在struts.xml中建立JSF的攔截器,之后的package應該繼承該配置
  • 在頁面中可以實現JSF與Struts混編,但是只能以struts方式進行表單提交,如果以JSF方式進行表單提交,將會拋出異常









  • 本文轉自 sundunjam 51CTO博客,原文鏈接:http://blog.51cto.com/sunspot/469073,如需轉載請自行聯系原作者

    總結

    以上是生活随笔為你收集整理的JSF Struts Spring Hibernate 整合的全部內容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。