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;", "&");

相关文章
相关标签/搜索