Hiternate和struts2框架整合問題總匯

包的分類:

基于Model2的JavaWeb開發(fā)模式其實就是大家常說的MVC框架。大概把整個包分為3個部分,第一個是JSP的view視圖,第二個是Model 層與數(shù)據(jù)庫關(guān)聯(lián),第三個是Controller對邏輯進(jìn)行實現(xiàn)。在hibernate和struts2整合的項目中,大致可以分為如圖所示的幾個包:


  • Entity包
    該類的作用就是存放與數(shù)據(jù)庫相關(guān)的表的基于面向?qū)ο笏枷朐O(shè)計的Javabean類和hibernate表單映射的文件(例如User.hbm.xml)

  • Db包
    顧名思義與數(shù)據(jù)庫關(guān)聯(lián)的包,我們可以寫一個會話工廠的單利模式類。這樣以后獲取Session(會話)的時候就可以通過該類。

private static SessionFactory sessionFactory;
     
     public MySessionFactory() {
    }
     
     public static SessionFactory getSessionFactory(){
         
         if(sessionFactory==null){
             //創(chuàng)建配置文件對象
                Configuration configuration=new Configuration();
                configuration.configure();
                //創(chuàng)建服務(wù)注冊對象
                ServiceRegistry serviceRegistry=new ServiceRegistryBuilder().applySettings(
                        configuration.getProperties()).buildServiceRegistry();
                //創(chuàng)建會話工廠對象
                sessionFactory=configuration.buildSessionFactory(serviceRegistry);
                 return sessionFactory;
         }else{
             
              return sessionFactory;
         }
     }

一個成熟的控制器應(yīng)該要調(diào)用Service層里的業(yè)務(wù)對象來實現(xiàn)具體的業(yè)務(wù)處理

Service包主要存放具體功能的類。這些類以Dao來命名(如UserDao),dao是一個功能匯集的接口類。

顧名思義是對Service層中的接口類進(jìn)行具體的實現(xiàn)。

Action是管理業(yè)務(wù)(Service)執(zhí)行,調(diào)度和跳轉(zhuǎn)的

  • 其他

    還有一些如hibernate和struts2的配置文件

具體的調(diào)度過程:

頁面-Action-Service-Service.impl-Entity-頁面

在Jsp頁面中獲取到請求的數(shù)據(jù),通過Struts調(diào)用Action動作跳到控制層
Action包中,在Action層中對該動作進(jìn)行處理,處理的過程是通過實現(xiàn)一個Service包里dao接口類的方法(體現(xiàn)多態(tài)),如:UserDao dao=new UserDapImpl(), dao.add() 在Dao的實現(xiàn)類中有可能要對數(shù)據(jù)庫進(jìn)行操作,或者在Session(會話)中獲取一些屬性值,這里都會調(diào)用Entity(實體)類的對象。邏輯處理完之后,Action里的對應(yīng)方法會返回一個String類型的字符集,在Struts配置文件中會找到該字符集(需提前自己配置),并且跳轉(zhuǎn)到對應(yīng)的jsp網(wǎng)頁也就是視圖層。


框架整合中常見的Error:

如果在之前已經(jīng)裝了幾次都不成功,最好在cmd界面中,sc delete mysql把之前的mysql服務(wù)關(guān)閉,然后重新安裝服務(wù)net start mysql,如果報錯1067,大概就是配置文件沒配置好,我安裝的是MySQL5.5的,在配置文件my.ini中(沒有則自行創(chuàng)建),里面主要設(shè)置如下代碼,關(guān)鍵是對datadir和basedir的路徑設(shè)置要正確:

[mysqld]

# The TCP/IP Port the MySQL Server will listen on
port=3306


#Path to installation directory. All paths are usually resolved relative to this.
basedir=C:/Program Files/MySQL/MySQL Server 5.5/

#Path to the database root
datadir=C:/Program Files/MySQL/MySQL Server 5.5/data/

# The default character set that will be used when a new schema or table is
# created and no character set is defined
character-set-server=latin1

# The default storage engine that will be used when create new tables when
default-storage-engine=MYISAM

在Hibernate和Struts2的整合中,可能會出現(xiàn)找不到包的情況

  1. 文件的位置要放對

    web.xml要放在WEB-INF下面,index.jsp要放在WebRoot下

  2. 相關(guān)配置要正確

    Struts.xml

        <struts>
       <package name="default" namespace="/" extends="struts-default"></package>
      
        <package name="users" namespace="/users" extends="default">
       <action name="*_*" class="action.{1}Action" method="{2}">
       <result  name="login_success">/users/Users_login_success.jsp</result>
       <result  name="login_failure">/users/Users_login.jsp</result>
        </action>
     
       </package>
       </struts>
    

    web.xml

             <display-name>xxx</display-name>
              <welcome-file-list>
            <welcome-file>index.jsp</welcome-file>
              </welcome-file-list>
              <filter>
            <filter-name>struts2</filter-name>
            <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
            </filter>
            
            <filter-mapping>
            <filter-name>struts2</filter-name>
            <url-pattern>/*</url-pattern>
            </filter-mapping>
    
  3. 所需要的包

因為包的種類繁多,考慮到有可能會重復(fù)或者漏掉一些包,最好到[struts官網(wǎng)](http://struts.apache.org/download.cgi#struts25)下載樣例的app然后在它的lib目錄下拷貝所有用到的包,將其導(dǎo)入到項目中如果出現(xiàn)問題類似:

java.lang.ClassNotFoundException:org.apache.struts2.dispatcher.FilterDispatcher
一般的解決方式你可以參考這篇文章:the way to slove the problem,簡單點其實就是把所有你引入項目的包,手動的放到Web-INF的lib目錄下,記得是所有,這樣基本上就能解決問題。

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容