SSH框架的搭建

SSH框架的搭建javascript

   

使用工具:html

IDE:eclipse EE   服務器:Tomcat7.0  瀏覽器:Google 前端

框架:Struts2.3.8+spring+hibernate3.0java

參考過的主要博文():mysql

http://blog.csdn.net/snowwitch/article/details/50925382web

http://blog.csdn.net/yeohcooller/article/details/9316923spring

(首先在此對兩位博主表示深深的感謝,謝謝他們的精彩的分享)sql

我使用的架構包就是第一位博主提供的(在此提醒一下,博主提供的Struts須要的架構包中有個Struts和spring鏈接的架構包(),在搭建Struts時沒搭建spring不要把該加過包導入,否則測試會出錯的)。數據庫

廢話就很少說了如今開始搭建環境了,我就以咱們經理讓咱們事先的功能爲例:apache

 

 

 

Struts的搭建

 

打開eclipse  配置好運行環境  而後建一個 web

Dynamic Web Project  項目

把Struts內apps文件夾下的struts2-blank war包用壓縮工具解壓,而後把WEB-INF/lib內的jar包和WEB-INF下的web.xml文件分別複製到項目下的WEB-INF/lib內和WEB-INF下(這裏的jar包在官網上都有下載的,也可使用我第一位博主提供的jar包)

而後在src 的目錄下創建一個action以下:

內容以下:

而後配置一下web.xml,內容以下:

<?xml version="1.0" encoding="UTF-8"?>
    <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xmlns="http://java.sun.com/xml/ns/javaee"
        xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
        id="WebApp_ID" version="2.5">

  <display-name>Struts 2 Rest Example</display-name>


  <!-- Filters -->
  <!-- START SNIPPET: filter -->
    <!-- <filter>
        <filter-name>action2</filter-name>
        <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
    </filter> -->
     <filter>
        <filter-name>action2</filter-name>
        <filter-class>com.test.util.UeditorFilter</filter-class>
    </filter>
    <!-- END SNIPPET: filter -->

    <filter-mapping>
        <filter-name>action2</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>

    <!-- Welcome file lists -->
    <welcome-file-list>
        <welcome-file>editorOnlinePage.jsp</welcome-file>
    </welcome-file-list>
    
    <!-- 用來定位Spring框架配置文件 -->
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/applicationContext*.xml,classpath*:applicationContext*.xml</param-value>
    </context-param>
    <!-- 配置Spring監聽 -->
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
</web-app>

  

上面代碼中:

   <filter>

        <filter-name>action2</filter-name>

由於須要因此本身建一個攔截器,下面是攔截器的地址

        <filter-class>com.test.util.UeditorFilter</filter-class>

默認攔截器爲:(你只須要默認就好了)

       <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExeuteFilter</filter-class> 

    </filter>

 

 

welcome-file-list  對應是項目打開是進入的JSP頁面

而後再在SRC目錄下創建一個 Struts.xml的配置文件

內容以下:

<?xml version="1.0" encoding="UTF-8" ?>
    <!DOCTYPE struts PUBLIC
            "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
            "http://struts.apache.org/dtds/struts-2.0.dtd">

    <struts>
        <package name="default" extends="struts-default" namespace="/">
        
        
        
        
        name爲前端表單的action的值 class是對應的action地址   有沒有發現上面的和下面的值格式不同,上面是單獨的Struts  
        下面是和spring結合的在一塊兒的,他對應中spring的配置文件裏面的對action管理的ID
        
        
        
        
        
            <action name="save" class="com.test.action.EditorOnlineAction">
             <result name="success">/success.jsp</result>
             <result name="error">/error.jsp</result>
           </action>
           <action name="get" class="get">
             <result name="success">/EditorOnlineGet.jsp</result>
             <result name="error">/error.jsp</result>
           </action>
        </package>
   </struts>

先給放點spring的代碼看一下  比較一下哈

結合spring後的代碼以下:

<?xml version="1.0" encoding="UTF-8" ?>
    <!DOCTYPE struts PUBLIC
            "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
            "http://struts.apache.org/dtds/struts-2.0.dtd">

    <struts>
        <package name="default" extends="struts-default" namespace="/">
            <action name="save" class="EditorOnlineAction">
             <result name="success">/success.jsp</result>
             <result name="error">/error.jsp</result>
           </action>
           <action name="get" class="get">
             <result name="success">/EditorOnlineGet.jsp</result>
             <result name="error">/error.jsp</result>
           </action>
        </package>
   </struts>

對應的spring配置文件的代碼以下:

<!-- Action配置 -->
        <bean id="EditorOnlineAction" class="com.test.action.EditorOnlineAction" scope="prototype">
             <property name="ieditorOnlineService" ref="EditorOnlineService"></property>
        </bean>
         <bean id="get" class="com.test.action.EditorOnlineListGet" scope="prototype">
             <property name="ieditorOnlineService" ref="EditorOnlineService"></property>
        </bean>

再建好須要的JSP頁面就好了  個人   

 歡迎頁面爲:

    <welcome-file-list>
        <welcome-file>editorOnlinePage.jsp</welcome-file>
    </welcome-file-list>

         Struts配置中有

            <result name="success">/success.jsp</result>
             <result name="error">/error.jsp</result>

因此須要創建三個JSP頁面

其中歡迎頁內容爲:

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
 <!--    Struts中須要用引入這個,否則前端沒法傳入數據 -->
<%@taglib uri="/struts-tags" prefix="s" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>html在線編輯</title>
   <!-- 配置文件 -->
    <script type="text/javascript" src="UEditor/ueditor.config.js"></script>
    <!-- 編輯器源碼文件 -->
    <script type="text/javascript" src="UEditor/ueditor.all.js"></script>
    <script type="text/javascript" src="UEditor/lang/zh-cn/zh-cn.js"></script>
</head>
<body>
   <!-- 這裏是跳轉到save   action的 -->
   <s:form action="save" method="post">
     <h1>html在線編輯</h1>
     <s:textarea name="EditorOnlineEntity.content" id="editor" ></s:textarea>
     <!-- 實例化編輯器 -->
    <script type="text/javascript">
        var ue = UE.getEditor('editor');
    </script>
     <s:submit value="保存"></s:submit>
   </s:form>
   <!-- 這裏是跳轉到get  action的 -->
   <s:form action="get" method="get">
          <s:submit value="得到存入數據庫的信息"></s:submit>
    </s:form>
</body>
</html>

注意看代碼中的註釋文字

而後運行,如今Struts就算建完了,結果會調轉到success.jsp頁面

 

 

spring的搭建

將Spring內libs目錄下包含全部的jar包(不須要複製結尾爲sources和javadoc的jar包)到項目的lib目錄下  注意了要把我上面說的鏈接二者的鏈接的插件導進來啦

在src 的目錄下創建一個

applicationContext.xml文件以下:

配置文件的內容以下:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://www.springframework.org/schema/beans  
               http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">  
  
  <!-- 配置數據源 MYSQL-->
  <!--   <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
        destroy-method="close">
        指定鏈接數據庫的驅動
        <property name="driverClassName" value="com.mysql.jdbc.Driver" />
        指定鏈接數據庫的URL
        <property name="url" value="jdbc:mysql://localhost:3306/test" />
        指定鏈接數據庫的用戶名
        <property name="username" value="root" />
        指定鏈接數據庫的密碼
        <property name="password" value="" />
    </bean> -->
    <!-- 配置數據源 ORACLE-->
     <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
        >
        <!-- 指定鏈接數據庫的驅動 -->
        <property name="driverClassName" value="oracle.jdbc.driver.OracleDriver" />
        <!-- 指定鏈接數據庫的URL -->
        <property name="url" value="jdbc:oracle:thin:@192.168.1.251/orcl" />
        <!-- 指定鏈接數據庫的用戶名 -->
        <property name="username" value="BMCS" />
        <!-- 指定鏈接數據庫的密碼 -->
        <property name="password" value="BMCS" />
    </bean>
    <!-- 配置SessionFactory -->
    <bean id="sessionFactory"
        class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
        <property name="dataSource">
            <ref bean="dataSource" />
        </property>
        <property name="hibernateProperties">
            <props>
              <!-- MYSQL -->
               <!--  <prop key="hibernate.dialect">
                    org.hibernate.dialect.MySQLDialect
                </prop> -->
               <!--  ORCLE -->
                  <prop key="hibernate.dialect">
                      org.hibernate.dialect.Oracle9iDialect
                </prop>
                <prop key="hibernate.hbm2ddl.auto">update</prop>
                <prop key="hibernate.connection.autocommit">true </prop>
                <prop key="hibernate.show_sql">true</prop>
                <prop key="sql_format">true</prop>
            </props>
        </property>
        <property name="mappingResources">
            <!-- 指定hibernate映射文件 -->
            <list>
                <value>com/test/entities/EditorOnline.hbm.xml</value>
            </list>
        </property>
    </bean>
    
        <!-- Dao配置 -->
        <bean id="editorOnlineDao" class="com.test.daoImpl.EditorOnlineDaoImpl">
              <property name="sessionFactory" ref="sessionFactory"></property>
        </bean>
               
        <!-- Service配置 -->
        <bean id="EditorOnlineService" class="com.test.service.EditorOnlineService">
              <property name="editorOnlineDao" ref="editorOnlineDao"></property>
        </bean>
action中property  ref=""對應的是service 的ID  (若是不理解的話查一下ref 的做用就能夠知道了)
        <!-- Action配置 -->
        <bean id="EditorOnlineAction" class="com.test.action.EditorOnlineAction" 
      scope="prototype">
             <property name="ieditorOnlineService" ref="EditorOnlineService"></property>
        </bean>
         <bean id="get" class="com.test.action.EditorOnlineListGet" scope="prototype">
             <property name="ieditorOnlineService" ref="EditorOnlineService"></property>
        </bean>
    </beans>

action中property  ref=""對應的是service 的ID  (若是不理解的話查一下ref 的做用就能夠知道了)

如今只須要創建  DAO  配置   service配置 action  配置就好了  其餘的事hibernate的配置先別管

而後你須要創建對應的DAO  service 以下:

每一個的代碼:

DAO:

接口BaseDao

package com.test.dao;

import java.util.List;

import org.hibernate.HibernateException;

import com.test.entities.EditorOnlineEntity;



public interface BaseDao {
	
	public void saveObject(Object obj) throws HibernateException; 
	public List<EditorOnlineEntity> getOeList()throws HibernateException;
}

實現類:

EditorOnlineDaoImpl.java

package com.test.daoImpl;


import java.util.List;

import org.hibernate.HibernateException;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import com.test.dao.BaseDao;
import com.test.entities.EditorOnlineEntity;


public class EditorOnlineDaoImpl extends HibernateDaoSupport implements BaseDao {
	@Override
	public void saveObject(Object obj) throws HibernateException {
		getHibernateTemplate().save(obj);
	}
	@Override
	public List<EditorOnlineEntity> getOeList() throws HibernateException {
		return getHibernateTemplate().find("from EditorOnlineEntity");
	}
    
	
	
}

service  :

接口:IEditorOnlineService.java

package com.test.service;

import java.util.List;

import com.test.entities.EditorOnlineEntity;

public interface IEditorOnlineService {
    public void saveContent(EditorOnlineEntity ee);
    public List<EditorOnlineEntity> getOeList();
}

實現類:EditorOnlineService.java

package com.test.service;

import java.util.List;

import com.test.dao.BaseDao;
import com.test.entities.EditorOnlineEntity;

public class EditorOnlineService implements IEditorOnlineService {
    private BaseDao editorOnlineDao;
    
	public BaseDao getEditorOnlineDao() {
		return editorOnlineDao;
	}

	public void setEditorOnlineDao(BaseDao editorOnlineDao) {
		this.editorOnlineDao = editorOnlineDao;
	}

	@Override
	public void saveContent(EditorOnlineEntity ee){
		editorOnlineDao.saveObject(ee);
	}

	@Override
	public List<EditorOnlineEntity> getOeList() {
		return editorOnlineDao.getOeList();
	}

	
}

spring就算搭建完成了,能夠執行一下 ,看是否獲得Struts搭建完的結果

 

hibernate的搭建:

先創建實體類以及對應的配置文件:

裏面的內容爲:

實體類:

配置文件:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC  
                            "-//Hibernate/Hibernate Mapping DTD 3.0//EN"  
                            "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd" > 
<hibernate-mapping>
   <class name="com.test.entities.EditorOnlineEntity" table="editoronline">
       <id name="id">
            <column name="ID"/>
            <!-- 主鍵生成器 -->
            <generator class="native" />
        </id>
        <property name="content" type="java.lang.String">
            <column name="content" length="1000" not-null="false" />
        </property> 
   </class>
</hibernate-mapping>

配置文件的的ID地方要注意一下 <generator class="native">這裏是指自動增加對應的實體類型是Interger型(數值型)

而後再在spring的配置文件applicationContext.xml的裏面配置一下數據源和session工廠

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://www.springframework.org/schema/beans  
               http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">  
  
  <!-- 配置數據源 MYSQL-->
  <!--   <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
        destroy-method="close">
        指定鏈接數據庫的驅動
        <property name="driverClassName" value="com.mysql.jdbc.Driver" />
        指定鏈接數據庫的URL
        <property name="url" value="jdbc:mysql://localhost:3306/test" />
        指定鏈接數據庫的用戶名
        <property name="username" value="root" />
        指定鏈接數據庫的密碼
        <property name="password" value="" />
    </bean> -->
    <!-- 配置數據源 ORACLE-->
     <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
        >
        <!-- 指定鏈接數據庫的驅動 -->
        <property name="driverClassName" value="oracle.jdbc.driver.OracleDriver" />
        <!-- 指定鏈接數據庫的URL -->
        <property name="url" value="jdbc:oracle:thin:@192.168.1.251/orcl" />
        <!-- 指定鏈接數據庫的用戶名 -->
        <property name="username" value="BMCS" />
        <!-- 指定鏈接數據庫的密碼 -->
        <property name="password" value="BMCS" />
    </bean>
    <!-- 配置SessionFactory -->
    <bean id="sessionFactory"
        class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
        <property name="dataSource">
            <ref bean="dataSource" />
        </property>
        <property name="hibernateProperties">
            <props>
              <!-- MYSQL的方言 -->
               <!--  <prop key="hibernate.dialect">
                    org.hibernate.dialect.MySQLDialect
                </prop> -->
               <!--  ORCLE的方言 這裏我參考第一個博文時出錯了org.hibernate.dialect.Oracle9Dialect這個方言被淘汰了無法解析-->
                  <prop key="hibernate.dialect">
                      org.hibernate.dialect.Oracle9iDialect
                </prop>
                <prop key="hibernate.hbm2ddl.auto">update</prop>
                <prop key="hibernate.connection.autocommit">true </prop>
                <prop key="hibernate.show_sql">true</prop>
                <prop key="sql_format">true</prop>
            </props>
        </property>
        <property name="mappingResources">
            <!-- 指定hibernate映射文件 -->
            <list>
                <value>com/test/entities/EditorOnline.hbm.xml</value>
            </list>
        </property>
    </bean>
    
        <!-- Dao配置 -->
        <bean id="editorOnlineDao" class="com.test.daoImpl.EditorOnlineDaoImpl">
              <property name="sessionFactory" ref="sessionFactory"></property>
        </bean>
               
        <!-- Service配置 -->
        <bean id="EditorOnlineService" class="com.test.service.EditorOnlineService">
              <property name="editorOnlineDao" ref="editorOnlineDao"></property>
        </bean>
        <!-- Action配置 -->
        <bean id="EditorOnlineAction" class="com.test.action.EditorOnlineAction" scope="prototype">
             <property name="ieditorOnlineService" ref="EditorOnlineService"></property>
        </bean>
         <bean id="get" class="com.test.action.EditorOnlineListGet" scope="prototype">
             <property name="ieditorOnlineService" ref="EditorOnlineService"></property>
        </bean>
    </beans>

這樣咱們的SSH就搭建完成了,不是很難的,主要是細節上的問題,下面附上我在搭建過程當中遇到的一些問題已經解決的方案:

 

錯誤一:Unable to load configuration.   在輸出太會提示那個文件出錯了,出錯的緣由是配置文件名不正確,本身校對一下

錯誤二:notfount  少包

錯誤三:ognl.OgnlException: target is null for setProperty(null, "content", [Ljava.lang.String;@1fec007)    :Action 中的實體類沒有new一個對象

錯誤四: 數據沒有傳過來:沒有使用 <%@taglib uri="/struts-tags" prefix="s" %>

錯誤五:'sessionFactory' or 'hibernateTemplate' is required   少了:紅色部位

<!-- Dao配置 -->

        <bean id="editorOnlineDao" class="com.test.daoImpl.EditorOnlineDaoImpl">

              <property name="sessionFactory" ref="sessionFactory"></property>

        </bean>

錯誤六:not found  org/hibernate/cache/CacheProvider:配置是hibernate3  導入的包是hibernate4(我配置的時候用的是第一博主提供的包,最後想用本身下載本身下了4)

你能夠改下文件的DTD把3改爲4

錯誤七:Line 5 in XML document from file [E:\吳兵兵\project\.metadata\.plugins\org.eclipse.wst.server.core\tmp0\wtpwebapps\editorOnline\WEB-INF\classes\applicationContext.xml] is invalid; nested exception is org.xml.sax.SAXParseException; lineNumber: 5; columnNumber: 83; cvc-elt.1: 找不到元素 'beans' 的聲明。

網絡的問題鏈接不上 

錯誤八:hbm.xml文件不存在   :spring配置文件的對應的名字錯誤

錯誤九:Could not parse mapping document from invalid mapping

 

org.xml.sax.SAXParseException; lineNumber: 5; columnNumber: 19; 文檔根元素 "hibernate-maping" 必須匹配 DOCTYPE 根 "hibernate-mapping"。

單詞寫錯了@@@@@@@@@@@@

 

錯誤十:Cannot resolve reference to bean 'ieditorOnlineService' while setting bean property 'ieditorOnlineService'; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'ieditorOnlineService' is defined

這個是ref引用問題 引用的bean  不存在,問題在於名字錯誤

 

 

 

錯誤十二:Dialect class not found: org.hibernate.dialect.Orale9Dialect

解決方案:Hibernate operation: Cannot open connection; uncategorized SQLException for SQL [???]; SQL state [null]; error code [0]; Cannot create PoolableConnectionFactory (ORA-01017: invalid username/password; logon denied  用戶名和密碼無效:確定是用戶或密碼寫錯了!!!!!!

 

錯誤十三:

org.springframework.orm.hibernate3.HibernateSystemException: Unknown integral data type for ids : java.lang.String; nested exception is org.hibernate.id.IdentifierGenerationException: Unknown integral data type for ids : java.lang.String

配置文件對應字段類型錯誤

 

錯誤十四:Io 異常: Invalid number format for port number無效端口號   這裏是數據庫的URL錯了!!!!

 

 

錯誤十五:IllegalArgumentException occurred while calling setter of com.test.entities.EditorOnlineEntity.id; nested exception is org.hibernate.PropertyAccessException: IllegalArgumentException occurred while calling setter of com.test.entities.EditorOnlineEntity.id

 

http://blog.csdn.net/u011457627/article/details/49991407

 

ck富文本使用錯誤:沒有圖片上傳按鈕  改爲UE

 

上傳圖片: 

沒找到數據:Struts的攔截器的問題

http://m.blog.csdn.net/article/details?id=51685114

圖片沒保存到上傳的目錄下:

 

 

 

是上傳到服務器裏面去了,

解決方法:

http://blog.csdn.net/hcysoul/article/details/38959281

 

 

錯誤十六:HQL對應的是實體類不是表

 

錯誤十七:Action的映射的集合無法再JSP上面顯現出來 :   問題不明確,本身搞了折中的方法,把內容存一個list<String>再傳到JSP中。

錯誤十八:沒法找到JQ文件:若是路徑沒錯的話,確定是衝突問題—你的若是引入外部插件,看一下插件裏面是否是有JQ包

 

錯誤十九: java.io.IOException: tmpFile.renameTo(classFile) failed   加了一個 escape="false"而後出現的錯誤

有些特殊字符須要轉義一下

aa.replaceAll("&lt;", "<");

                    aa.replaceAll("&gt;", ">");

                    aa.replaceAll("&quot;", "「");

                    aa.replaceAll("&amp;", "&");

相關文章
相關標籤/搜索