SSH框架搭建测试项目实例代码演示



SSH框架搭建测试实例代码演示。

工程目录图:

数据库表:

 

  1. /*
  2.  * 创建测试用户表
  3.  */
  4. create table test_user(
  5.     u_id number, –自增编号
  6.     u_loginname varchar2(30) ,  –登录用户名
  7.     u_loginpass varchar2(30) , –密码
  8.     u_username  varchar2(30)  –用户姓名
  9. );
  10. /*
  11.  * 创建测试文章表
  12.  */
  13. create table test_article(
  14.    a_id number, –自增编号
  15.    u_id number ,  –用户编号
  16.    a_title varchar2(30) –标题
  17. );
  18. /*用户表跟文章表主键*/
  19. alter table test_user
  20.   add constraint tu_PK_u_ID primary key (u_id);
  21. alter table test_article
  22.   add constraint ta_PK_a_ID primary key (a_id);
  23. /*
  24.  * 创建测试用户表自增Sequence
  25.  */
  26. create sequence test_user_SEQ
  27. minvalue 1
  28. maxvalue 999999999999999999
  29. start with 1
  30. increment by 1
  31. cache 20;
  32. /*
  33.  * 创建测试文章自增Sequence
  34.  */
  35. create sequence test_article_SEQ
  36. minvalue 1
  37. maxvalue 999999999999999999
  38. start with 1
  39. increment by 1
  40. cache 20;

一、搭建struts应用
引入需要的jar包
commons-fileupload-1.2.2.jar   文件上传组件,2.1.6版本后必须加入此文件。
commons-io-2.0.1.jar                  IO输入输出流组件,主要完成文件的读写功能。
commons-lang3-3.1.jar              为java.lang包提供扩展
freemarker-2.3.19.jar                   Struts 2的UI标签的模板使用FreeMarker编写。
javassist-3.11.0.GA.jar                Javassist是一个开源的分析、编辑和创建Java字节码的类库。
ognl-3.0.6.jar                                 对象图导航语言(Object Graph Navigation Language),它是一种功能强大的表达式语言(Expression Language,简称为EL),通                                                                        过它简单一致的表达式语法,可以存取对象的任意属性,调用对象的方法,遍历整个对象的结构图,实现字段类型转化等功能。
struts2-core-2.3.8.jar                   Struts2框架的核心类库。
xwork-core-2.3.8.jar                     Xwork核心类库,Struts2在其上构建。

加入配置文件struts.xml

  1. <!DOCTYPE struts PUBLIC
  2.     ”-//Apache Software Foundation//DTD Struts Configuration 2.3//EN”
  3.     ”http://struts.apache.org/dtds/struts-2.3.dtd”>
  4. <struts>
  5.     <constant name=”struts.i18n.encoding” value=”UTF-8″></constant>
  6.      <package name=”test” extends=”struts-default”>
  7.         <action name=”testAction” class=”com.hzw.shh.web.action.TestAction” converter=”">
  8.             <result name=”hzw”>/index.jsp</result>
  9.         </action>
  10.      </package>
  11. </struts>

 

将struts加入到web.xml的配置中,将请求交由struts来处理

 

  1. <?xml version=”1.0″ encoding=”UTF-8″?>
  2. <web-app version=”2.5″
  3.     xmlns=”http://java.sun.com/xml/ns/javaee”
  4.     xmlns:xsi=”http://www.w3.org/2001/XMLSchema-instance”
  5.     xsi:schemaLocation=”http://java.sun.com/xml/ns/javaee
  6.     http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd”>
  7.     <!– <<<<<<<<<<Stauts2 配置 –>
  8.     <filter>
  9.         <filter-name>struts2</filter-name>
  10.         <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
  11.     </filter>
  12.     <filter-mapping>
  13.         <filter-name>struts2</filter-name>
  14.         <url-pattern>/*</url-pattern>
  15.     </filter-mapping>
  16.     <!– Stauts2 配置>>>>>>>>> –>
  17.   <welcome-file-list>
  18.     <welcome-file>index.jsp</welcome-file>
  19.   </welcome-file-list>
  20. </web-app>

编写Action类,无需继承任何类和实现任何接口

  1. public class TestAction {
  2.     private String testName;
  3.     public String getTestName() {
  4.         return testName;
  5.     }
  6.     public void setTestName(String testName) {
  7.         this.testName = testName;
  8.     }
  9.     public String testActionHzw(){
  10.         System.out.println(testName);
  11.         this.testName = testName + ”————进入Action” ;
  12.         return ”hzw”;
  13.     }
  14. }

jsp

  1. <%@ page language=”java” import=”java.util.*” pageEncoding=”UTF-8″%>
  2. <%@taglib prefix=”s” uri=”/struts-tags” %>
  3. <%
  4. String path = request.getContextPath();
  5. %>
  6. <!DOCTYPE HTML PUBLIC ”-//W3C//DTD HTML 4.01 Transitional//EN”>
  7. <html>
  8.   <head>
  9.     <title>胡汉三</title>
  10.     <meta http-equiv=”pragma” content=”no-cache”>
  11.     <meta http-equiv=”cache-control” content=”no-cache”>
  12.     <meta http-equiv=”expires” content=”0″>
  13.     <meta http-equiv=”keywords” content=”keyword1,keyword2,keyword3″>
  14.     <meta http-equiv=”description” content=”This is my page”>
  15.   </head>
  16.   <body>
  17.     我真的是胡汉三
  18.     <form action=”testAction!testActionHzw.action” method=”post”>
  19.         <input name=”testName” value=”<s:property value=”testName” />”>
  20.         <input type=”submit” value=”submit” >
  21.     </form>
  22.   </body>
  23. </html>

二、搭建spring的框架:
加入jar包
spring.jar
commons-logging.jar
struts2-spring-plugin-2.3.8.jar     将Struts交给Spring托管就需要这个jar
aspectjweaver-1.5.3.jar                 要拥有 Spring 事务支持, 必须包含 Spring 核心库和 aspectjweaver.jar 文件
commons-collections-3.1.jar       Apache Commons包中的一个,包含了一些Apache开发的集合类,功能比java.util.*强大。必须使用的jar包。
dom4j-1.6.1.jar d                            om4j是一个Java的XML API,类似于jdom,用来读写XML文件的。dom4j是一个非常非常优秀的Java XML

配置文件:暂时不配置AOP、跟事务管理、只配置Struts2的托管

  1. <?xml version=”1.0″ encoding=”UTF-8″?>
  2. <beans xmlns=”http://www.springframework.org/schema/beans”
  3.     xmlns:xsi=”http://www.w3.org/2001/XMLSchema-instance” xmlns:aop=”http://www.springframework.org/schema/aop”
  4.     xmlns:tx=”http://www.springframework.org/schema/tx”
  5.     xsi:schemaLocation=”http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
  6.            http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
  7.            http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd”>
  8.     <!– Spring 默认scope为单例模式:singleton
  9.         这样只会创建一个Action对象
  10.         每次访问都是同一个Action对象,数据不安全
  11.         struts2 是要求 每次次访问 都对应不同的Action
  12.          scope=”prototype” 可以保证 当有请求的时候 都创建一个Action对象 –>
  13.     <bean id=”testActionService” class=”com.hzw.shh.web.action.TestAction” scope=”prototype”>
  14.     </bean>
  15. </beans>
  16. <!–更改Struts配置:testActionService–>
  17. <action name=”testAction” class=”testActionService” >
  18.     <result name=”hzw”>/index.jsp</result>
  19. </action>
  20. <!–Web.xml增加Spring配置 –>
  21. <!– <<<<<<<<<<Spring 配置 –>
  22. <context-param>
  23.     <!– contextConfigLocation参数名称是系统默认解析的参数  –>
  24.     <param-name>contextConfigLocation</param-name>
  25.     <param-value>/WEB-INF/applicationContext*.xml</param-value>
  26. </context-param>
  27. <!– 注册spring监听器 –>
  28. <listener>
  29.     <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  30. </listener>
  31. <!– Spring 配置>>>>>>>>> –>


三、hibernate框架搭建
加入jar包
hibernate3.jar          hibernate核心包
ojdbc14.jar               数据库的jar包
jta-1.1.jar                  JTA规范,JTA(Java Transaction API)是一种高层的,与实现无关的,与协议无关的API,应用程序和应用服务器可以使用JTA来访问事务。
slf4j-api-1.6.0.jar    整合各种日志框架的工具
antlr-2.7.6.jar           在用hibernate3.0进行查询时,出现java.lang.NoClassDefFoundError: antlr/ANTLRException异常. 所以必须导入

框架搭建好了、下面得用它来做事情、那么贴出所有完整的配置

  1. hibernate.cfg.xml配置文件
  2. <?xml version=’1.0′ encoding=’UTF-8′?>
  3. <!DOCTYPE hibernate-configuration PUBLIC
  4.           ”-//Hibernate/Hibernate Configuration DTD 3.0//EN”
  5.           ”http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd”>
  6. <hibernate-configuration>
  7. <session-factory>
  8.     <property name=”dialect”>
  9.         org.hibernate.dialect.Oracle9Dialect
  10.     </property>
  11.     <property name=”connection.url”>
  12.         jdbc:oracle:thin:@172.16.0.162:1521:ORCL
  13.     </property>
  14.     <property name=”connection.username”>m_order</property>
  15.     <property name=”connection.password”>morder</property>
  16.     <property name=”connection.driver_class”>
  17.         oracle.jdbc.driver.OracleDriver
  18.     </property>
  19.     <mapping resource=”com/hzw/shh/dao/bean/TestArticle.hbm.xml” />
  20.     <mapping resource=”com/hzw/shh/dao/bean/TestUser.hbm.xml” />
  21. </session-factory>
  22. </hibernate-configuration>
  23. Struts.xml配置文件
  24. <!DOCTYPE struts PUBLIC
  25.     ”-//Apache Software Foundation//DTD Struts Configuration 2.3//EN”
  26.     ”http://struts.apache.org/dtds/struts-2.3.dtd”>
  27. <struts>
  28.     <constant name=”struts.i18n.encoding” value=”UTF-8″></constant>
  29.      <package name=”test” extends=”struts-default”>
  30.         <action name=”testAction” class=”testActionService” >
  31.             <result name=”hzw”>/index.jsp</result>
  32.             <result name=”Article”>/articles.jsp</result>
  33.         </action>
  34.      </package>
  35. </struts>
  36. Spring.xml配置文件
  37. <?xml version=”1.0″ encoding=”UTF-8″?>
  38. <beans xmlns=”http://www.springframework.org/schema/beans”
  39.     xmlns:xsi=”http://www.w3.org/2001/XMLSchema-instance” xmlns:aop=”http://www.springframework.org/schema/aop”
  40.     xmlns:tx=”http://www.springframework.org/schema/tx”
  41.     xsi:schemaLocation=”http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
  42.            http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
  43.            http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd”>
  44.     <!– sessionFactory –>
  45.     <bean id=”MysessionFactory”
  46.         class=”org.springframework.orm.hibernate3.LocalSessionFactoryBean”>
  47.         <property name=”configLocation” value=”classpath:hibernate.cfg.xml”>
  48.         </property>
  49.     </bean>
  50.     <!– 配置事务管理器 –>
  51.     <bean id=”transactionManager”
  52.         class=”org.springframework.orm.hibernate3.HibernateTransactionManager”>
  53.         <property name=”sessionFactory”>
  54.             <ref bean=”MysessionFactory” />
  55.         </property>
  56.     </bean>
  57.     <tx:advice id=”smAdvice” transaction-manager=”transactionManager”>
  58.         <tx:attributes>
  59.             <tx:method name=”get*” read-only=”true” />
  60.             <tx:method name=”find*” read-only=”true” />
  61.             <tx:method name=”insert*” propagation=”REQUIRED” />
  62.             <tx:method name=”del*” propagation=”REQUIRED” />
  63.             <tx:method name=”update*” propagation=”REQUIRED” />
  64.         </tx:attributes>
  65.     </tx:advice>
  66.     <!– aop –>
  67.     <aop:config>
  68.         <aop:pointcut id=”smBizMethod”
  69.             expression=”execution(* com.hzw.shh.service.impl.*.*(..))” />
  70.         <aop:advisor pointcut-ref=”smBizMethod” advice-ref=”smAdvice” />
  71.     </aop:config>
  72.     <!– dao –>
  73.     <bean id=”baseDaoImp” class=”com.hzw.shh.dao.impl.BaseDaoImpl”>
  74.         <property name=”sessionFactory”>
  75.             <ref bean=”MysessionFactory” />
  76.         </property>
  77.     </bean>
  78.     <bean id=”testService” class=”com.hzw.shh.service.impl.TestSshImpl”>
  79.         <property name=”dao” ref=”baseDaoImp”></property>
  80.     </bean>
  81.     <!– Spring 默认scope为单例模式:singleton
  82.         这样只会创建一个Action对象
  83.         每次访问都是同一个Action对象,数据不安全
  84.         struts2 是要求 每次次访问 都对应不同的Action
  85.          scope=”prototype” 可以保证 当有请求的时候 都创建一个Action对象 –>
  86.     <bean id=”testActionService” class=”com.hzw.shh.web.action.TestAction” scope=”prototype”>
  87.         <property name=”tests” ref=”testService”></property>
  88.     </bean>
  89. </beans>

 

搭建时可能存在异常
java.lang.NoClassDefFoundError: org/apache/commons/lang/StringUtils
缺少commons-lang3-3.1.jar

java.lang.RuntimeException: java.lang.reflect.InvocationTargetException
缺少 javassist-3.11.0.GA.jar

nested exception is java.lang.NoClassDefFoundError: org/aspectj/weaver/reflect/ReflectionWorld$Reflection
缺少aspectjweaver-1.5.3.jar

java.lang.NoClassDefFoundError: org/apache/commons/logging/LogFactory
缺少commons-logging.jar

nested exception is java.lang.NoClassDefFoundError: org/apache/commons/collections/SequencedHashMap
缺少commons-collections-3.1.jar

java.lang.NoClassDefFoundError: org/dom4j/DocumentException
缺少dom4j-1.6.1.jar

工程源代码:里面有一个登录跟分页——点击下载

记录下来的目的是以备不时之需!有一次面试居然有面试官让我搭建SSH框架!当时我就蒙了、千辛万苦搭建好了、人家下班了、那次面试也就……现在哥哥弄一个放着、你妹要就直接下载了然后……哈哈哈!!!

http://blog.csdn.net/hzw2312/article/details/8477867