SSH框架搭建测试
工程目录图:

数据库表:
/*
* 创建测试用户表
*/
create table test_user(
u_id number, --自增编号
u_loginname varchar2(30) , --登录用户名
u_loginpass varchar2(30) , --密码
u_username varchar2(30) --用户姓名
);
/*
* 创建测试文章表
*/
create table test_article(
a_id number, --自增编号
u_id number , --用户编号
a_title varchar2(30) --标题
);
/*用户表跟文章表主键*/
alter table test_user
add constraint tu_PK_u_ID primary key (u_id);
alter table test_article
add constraint ta_PK_a_ID primary key (a_id);
/*
* 创建测试用户表自增Sequence
*/
create sequence test_user_SEQ
minvalue 1
maxvalue 999999999999999999
start with 1
increment by 1
cache 20;
/*
* 创建测试文章自增Sequence
*/
create sequence test_article_SEQ
minvalue 1
maxvalue 999999999999999999
start with 1
increment by 1
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
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN" "http://struts.apache.org/dtds/struts-2.3.dtd"> <struts> <constant name="struts.i18n.encoding" value="UTF-8"></constant> <package name="test" extends="struts-default"> <action name="testAction" class="com.hzw.shh.web.action.TestAction" converter=""> <result name="hzw">/index.jsp</result> </action> </package> </struts>
将struts加入到web.xml的配置中,将请求交由struts来处理
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<!-- <<<<<<<<<<Stauts2 配置 -->
<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>
<!-- Stauts2 配置>>>>>>>>> -->
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>编写Action类,无需继承任何类和实现任何接口public class TestAction {
private String testName;
public String getTestName() {
return testName;
}
public void setTestName(String testName) {
this.testName = testName;
}
public String testActionHzw(){
System.out.println(testName);
this.testName = testName + "————进入Action" ;
return "hzw";
}
}jsp<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@taglib prefix="s" uri="/struts-tags" %>
<%
String path = request.getContextPath();
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>胡汉三</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
</head>
<body>
我真的是胡汉三
<form action="testAction!testActionHzw.action" method="post">
<input name="testName" value="<s:property value="testName" />">
<input type="submit" value="submit" >
</form>
</body>
</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的托管
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
<!-- Spring 默认scope为单例模式:singleton
这样只会创建一个Action对象
每次访问都是同一个Action对象,数据不安全
struts2 是要求 每次次访问 都对应不同的Action
scope="prototype" 可以保证 当有请求的时候 都创建一个Action对象 -->
<bean id="testActionService" class="com.hzw.shh.web.action.TestAction" scope="prototype">
</bean>
</beans>
<!--更改Struts配置:testActionService-->
<action name="testAction" class="testActionService" >
<result name="hzw">/index.jsp</result>
</action>
<!--Web.xml增加Spring配置 -->
<!-- <<<<<<<<<<Spring 配置 -->
<context-param>
<!-- contextConfigLocation参数名称是系统默认解析的参数 -->
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/applicationContext*.xml</param-value>
</context-param>
<!-- 注册spring监听器 -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- 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异常. 所以必须导入
框架搭建好了、下面得用它来做事情、那么贴出所有完整的配置
hibernate.cfg.xml配置文件
<?xml version='1.0' encoding='UTF-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="dialect">
org.hibernate.dialect.Oracle9Dialect
</property>
<property name="connection.url">
jdbc:oracle:thin:@172.16.0.162:1521:ORCL
</property>
<property name="connection.username">m_order</property>
<property name="connection.password">morder</property>
<property name="connection.driver_class">
oracle.jdbc.driver.OracleDriver
</property>
<mapping resource="com/hzw/shh/dao/bean/TestArticle.hbm.xml" />
<mapping resource="com/hzw/shh/dao/bean/TestUser.hbm.xml" />
</session-factory>
</hibernate-configuration>
Struts.xml配置文件
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
"http://struts.apache.org/dtds/struts-2.3.dtd">
<struts>
<constant name="struts.i18n.encoding" value="UTF-8"></constant>
<package name="test" extends="struts-default">
<action name="testAction" class="testActionService" >
<result name="hzw">/index.jsp</result>
<result name="Article">/articles.jsp</result>
</action>
</package>
</struts>
Spring.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" xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
<!-- sessionFactory -->
<bean id="MysessionFactory"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="configLocation" value="classpath:hibernate.cfg.xml">
</property>
</bean>
<!-- 配置事务管理器 -->
<bean id="transactionManager"
class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory">
<ref bean="MysessionFactory" />
</property>
</bean>
<tx:advice id="smAdvice" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="get*" read-only="true" />
<tx:method name="find*" read-only="true" />
<tx:method name="insert*" propagation="REQUIRED" />
<tx:method name="del*" propagation="REQUIRED" />
<tx:method name="update*" propagation="REQUIRED" />
</tx:attributes>
</tx:advice>
<!-- aop -->
<aop:config>
<aop:pointcut id="smBizMethod"
expression="execution(* com.hzw.shh.service.impl.*.*(..))" />
<aop:advisor pointcut-ref="smBizMethod" advice-ref="smAdvice" />
</aop:config>
<!-- dao -->
<bean id="baseDaoImp" class="com.hzw.shh.dao.impl.BaseDaoImpl">
<property name="sessionFactory">
<ref bean="MysessionFactory" />
</property>
</bean>
<bean id="testService" class="com.hzw.shh.service.impl.TestSshImpl">
<property name="dao" ref="baseDaoImp"></property>
</bean>
<!-- Spring 默认scope为单例模式:singleton
这样只会创建一个Action对象
每次访问都是同一个Action对象,数据不安全
struts2 是要求 每次次访问 都对应不同的Action
scope="prototype" 可以保证 当有请求的时候 都创建一个Action对象 -->
<bean id="testActionService" class="com.hzw.shh.web.action.TestAction" scope="prototype">
<property name="tests" ref="testService"></property>
</bean>
</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框架!当时我就蒙了、千辛万苦搭建好了、人家下班了、那次面试也就......现在哥哥弄一个放着、你妹要就直接下载了然后......哈哈哈!!!

浙公网安备 33010602011771号