SSH

 

看张冰老师的SSH2整合视频和网上其它高手手动整合SSH2,终于在晚上吃饭前整合成功了。下面把自己第一次整合SSH2的过程记录下(以简单的用户注册为示例):

这里所用的是struts-2.0.11.2,hibernate3.3.1,spring2.5.5不包版本之间可能有些包不同。

step1:首先新建一个webproject项目,项目名为ssh2。

step2:首先把ssh2整合的环境搭建好再来写代码。

把所需要的JAR包复制到WebRoot/WEB-INF/lib目录下

struts2所需要的包:

struts2-core-2.0.11.jar

xwork-2.0.4.jar

ognl-2.6.11.jar

freemarker-2.3.8.jar

commons-logging-1.0.4.jar

struts2-spring-plugin-2.0.11.2.jar(这个是struts2和spring整合的包)

hibernate3.3所需要的包:

hibernate3.jar(这个是hibernate的核心包)

以及hibernate3/lib目录下所有的包复制进来,lib目录下有三个目录required,optional,bytecode其中required是hibernate必须的包,防为防止漏包,我们把另二个目录下的所有包也复制进来

Spring2.5所需要的包:

spring-framework-2.5.5/dist目录下的spring.jar这个是spring的核心包

spring-framework-2.5.5/lib/aspectj/aspectjrt.jar

spring-framework-2.5.5/lib/aspectj/aspectjweaver.jar

spring-framework-2.5.5/lib/c3p0/c3p0-0.9.1.2.jar

spring-framework-2.5.5/lib/log4j/log4j-1.2.15.jar

spring-framework-2.5.5/lib/jakarta-commons/commons-dbcp.jar

spring-framework-2.5.5/lib/jakarta-commons/commons-pool.jar

spring-framework-2.5.5/lib/jakarta-commons/commons-collections.jar

spring-framework-2.5.5/lib/jakarta-commons/commons-logging.jar

spring-framework-2.5.5/lib/jakarta-commons/commons-collections.jar

最后别忘了把mysql的数据库驱动包引进来,这里用的是MYSQL数据库

不知道写的时候有没有写漏,可能有一些包有重复,造成有冲突,解决方法,可以先把tomcat停止,然后在进到webapps/工程所有目录下的WEB-INF/lib下查看所有引入进来的JAR包,看到有复制的就删掉,只留下最新的。
step3:最后配置好的web.xml如下:

<?xmlversion="1.0"encoding="UTF-8"?>
<web-appversion="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">
<!--配置Spring-->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
classpath:application-*.xml
</param-value>
</context-param>
<!--配置Struts2-->
<filter>
<filter-name>struts2</filter-name>
<filter-class>
org.apache.struts2.dispatcher.FilterDispatcher
</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<!--配置Spring的监听-->
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
<!--配置Spring的过滤器,解决乱码问题-->
<filter>
<filter-name>encoding</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>encoding</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<welcome-file-list>
<welcome-file>reg.jsp</welcome-file>
</welcome-file-list>
</web-app> src下struts.xml的最终代码如下: <?xmlversion="1.0"encoding="UTF-8"?>
<!DOCTYPEstrutsPUBLIC
"-//ApacheSoftwareFoundation//DTDStrutsConfiguration2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">


<struts>
<packagename="default"extends="struts-default">
<actionname="reg"class="reg">
<resultname="success">/regSuc.jsp</result>
<resultname="error">/regFail.jsp</result>
</action>
</package>
</struts>
在src下把hibernate相应的配置文件复制进来,hibernate.cfg.xml最终代码如下: <?xmlversion='1.0'encoding='UTF-8'?>
<!DOCTYPEhibernate-configurationPUBLIC
"-//Hibernate/HibernateConfigurationDTD3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd"> <!--GeneratedbyMyEclipseHibernateTools.-->
<hibernate-configuration> <session-factory>
<propertyname="hbm2ddl.auto">update</property>
<propertyname="show_sql">true</property>
<mappingresource="vo/User.hbm.xml"/>
</session-factory>
</hibernate-configuration> 把hibernate缓存的配置也复制进来ehcache.xml代码如下: <ehcache>
<diskStorepath="java.io.tmpdir"/>
<defaultCache
maxElementsInMemory="10000"
eternal="false"
timeToIdleSeconds="120"
timeToLiveSeconds="120"
overflowToDisk="true"
/>
<cachename="sampleCache1"
maxElementsInMemory="10000"
eternal="false"
timeToIdleSeconds="300"
timeToLiveSeconds="600"
overflowToDisk="true"
/>
<cachename="sampleCache2"
maxElementsInMemory="1000"
eternal="true"
timeToIdleSeconds="0"
timeToLiveSeconds="0"
overflowToDisk="false"
/>--> </ehcache>
把hibernate下的日志文件配置复制过来log4j.properties: #ForJBoss:AvoidtosetupLog4Joutside$JBOSS_HOME/server/default/deploy/log4j.xml!
#Forallotherservers:CommentouttheLog4Jlistenerinweb.xmltoactivateLog4J.
log4j.rootLogger=INFO,stdout,logfile log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d%p[%c]-%m%n log4j.appender.logfile=org.apache.log4j.RollingFileAppender
log4j.appender.logfile.File=${petstore.root}/WEB-INF/petstore.log
log4j.appender.logfile.MaxFileSize=512KB
#Keepthreebackupfiles.
log4j.appender.logfile.MaxBackupIndex=3
#Patterntooutput:datepriority[category]-message 把spring的配置文件复制过来applicationContext.xml 这里我们把他改名为:applicaton-beans.xml最后代码为: <?xmlversion="1.0"encoding="UTF-8"?> <beansxmlns="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/beanshttp://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/aophttp://www.springframework.org/schema/aop/spring-aop-2.5.xsd
http://www.springframework.org/schema/txhttp://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
<beanname="userDaoImpl"class="dao.impl.UserDaoImpl"
scope="prototype">
<propertyname="sessionFactory"ref="sf"></property>
</bean>
<beanname="reg"class="action.UserAction"scope="prototype">
<propertyname="userDaoImpl"ref="userDaoImpl"></property>
</bean>
</beans>
再建立一个事务控制的application-transaction.xml代码最终如下: <?xmlversion="1.0"encoding="UTF-8"?> <beansxmlns="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/beanshttp://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/aophttp://www.springframework.org/schema/aop/spring-aop-2.5.xsd
http://www.springframework.org/schema/txhttp://www.springframework.org/schema/tx/spring-tx-2.5.xsd"> <beanid="propertyConfigurer"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<propertyname="locations">
<list>
<value>classpath:jdbc.properties</value>
</list>
</property>
</bean> <!--配置数据源-->
<beanid="dataSource"
class="org.apache.commons.dbcp.BasicDataSource">
<propertyname="driverClassName"
value="${jdbc.driverClassName}"/>
<propertyname="url"value="${jdbc.url}"/>
<propertyname="username"value="${jdbc.username}"/>
<propertyname="password"value="${jdbc.password}"/>
</bean> <!--配置SessionFactory-->
<beanid="sf"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<propertyname="dataSource"ref="dataSource"/>
<propertyname="configLocation"
value="classpath:hibernate.cfg.xml"/>
</bean> <aop:config>
<aop:advisorpointcut="execution(**..PetStoreFacade.*(..))"
advice-ref="txAdvice"/>
</aop:config>
<tx:adviceid="txAdvice"transaction-manager="txManager">
<tx:attributes>
<tx:methodname="insert*"/>
<tx:methodname="update*"/>
<tx:methodname="delete*"/>
<tx:methodname="modify*"/>
<tx:methodname="*"read-only="true"/>
</tx:attributes>
</tx:advice>
<beanid="txManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<propertyname="dataSource"ref="dataSource"/>
</bean>
</beans>
到此,所有的配置都整合好了,下面开始写代码 step4:建立相应的jsp页面 注册页面reg.jsp代码如下:

<%@pagelanguage="java"pageEncoding="UTF-8"%>
<%
Stringpath=request.getContextPath();
StringbasePath=request.getScheme() "://" request.getServerName() ":" request.getServerPort() path "/";
%>

<!DOCTYPEHTMLPUBLIC"-//W3C//DTDHTML4.01Transitional//EN">
<html>
<head>
<basehref="<%=basePath%>">

<title>SSH2用户注册</title>

<metahttp-equiv="pragma"content="no-cache">
<metahttp-equiv="cache-control"content="no-cache">
<metahttp-equiv="expires"content="0">
<metahttp-equiv="keywords"content="keyword1,keyword2,keyword3">
<metahttp-equiv="description"content="Thisismypage">
<!--
<linkrel="stylesheet"type="text/css"href="styles.css">
-->

</head>

<body>
<h2>用户注册</h2>
<formaction="reg.action"method="post">
用户名:<inputtype="text"name="user.username"/><br/>
密&nbsp;&nbsp;码:<inputtype="password"name="user.password"><br/>
<inputtype="submit"value="注册"/><inputtype="reset"value="重置"/>
</form>
</body>
</html>
注册成功显示页面regSuc.jsp代码如下:

<%@pagelanguage="java"pageEncoding="UTF-8"%>
<%
Stringpath=request.getContextPath();
StringbasePath=request.getScheme() "://" request.getServerName() ":" request.getServerPort() path "/";
%>

<!DOCTYPEHTMLPUBLIC"-//W3C//DTDHTML4.01Transitional//EN">
<html>
<head>
<basehref="<%=basePath%>">

<title>注册成功</title>

<metahttp-equiv="pragma"content="no-cache">
<metahttp-equiv="cache-control"content="no-cache">
<metahttp-equiv="expires"content="0">
<metahttp-equiv="keywords"content="keyword1,keyword2,keyword3">
<metahttp-equiv="description"content="Thisismypage">
<!--
<linkrel="stylesheet"type="text/css"href="styles.css">
-->

</head>

<body>
恭喜你注册成功了<br>
</body>
</html>
注册失败regFail.jsp代码如下:

<%@pagelanguage="java"pageEncoding="UTF-8"%>
<%
Stringpath=request.getContextPath();
StringbasePath=request.getScheme() "://" request.getServerName() ":" request.getServerPort() path "/";
%>

<!DOCTYPEHTMLPUBLIC"-//W3C//DTDHTML4.01Transitional//EN">
<html>
<head>
<basehref="<%=basePath%>">

<title>注册失败</title>

<metahttp-equiv="pragma"content="no-cache">
<metahttp-equiv="cache-control"content="no-cache">
<metahttp-equiv="expires"content="0">
<metahttp-equiv="keywords"content="keyword1,keyword2,keyword3">
<metahttp-equiv="description"content="Thisismypage">
<!--
<linkrel="stylesheet"type="text/css"href="styles.css">
-->

</head>

<body>
对不起注册失败<br>
</body>
</html>

在src下新建一个vo包,在vo包下新建一个User类,User.java代码如下:

packagevo;

publicclassUser{
privateintid;
privateStringusername;
privateStringpassword;
publicStringgetUsername(){
returnusername;
}
publicvoidsetUsername(Stringusername){
this.username=username;
}
publicStringgetPassword(){
returnpassword;
}
publicvoidsetPassword(Stringpassword){
this.password=password;
}
publicintgetId(){
returnid;
}
publicvoidsetId(intid){
this.id=id;
}

}
在vo包下新建hibernate的映射文件User.hbm.xml

<?xmlversion="1.0"?>
<!DOCTYPEhibernate-mappingPUBLIC
"-//Hibernate/HibernateMappingDTD3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mappingpackage="vo">
<classname="User"table="t_user">
<idname="id">
<generatorclass="native"></generator>
</id>
<propertyname="username"column="username"not-null="true"/>
<propertyname="password"column="password"not-null="true"/>
</class>
</hibernate-mapping>


step5:编写struts2的action类,UserAction.java代码如下:

packageaction;

importvo.User;
importcom.opensymphony.xwork2.ActionSupport;

importdao.UserDao;

publicclassUserActionextendsActionSupport{


privatestaticfinallongserialVersionUID=-8534550171421612227L;
privateUseruser;
privateUserDaouserDaoImpl;
publicUsergetUser(){
returnuser;
}
publicvoidsetUser(Useruser){
this.user=user;
}
publicstaticlonggetSerialVersionUID(){
returnserialVersionUID;
}
@Override
publicStringexecute()throwsException{
booleanflag=false;
flag=userDaoImpl.insertUser(user);
if(flag){
returnSUCCESS;
}else{
returnERROR;
}
}
publicUserDaogetUserDaoImpl(){
returnuserDaoImpl;
}
publicvoidsetUserDaoImpl(UserDaouserDaoImpl){
this.userDaoImpl=userDaoImpl;
}
}
在src下新建一个dao包,在dao下新建一个UserDao.java

packagedao;

importvo.User;

publicinterfaceUserDao{
publicbooleaninsertUser(Useruser);
}
在src/dao下新建一个impl包在下面新建一个UserDao的实现类UserDaoImpl.java

packagedao.impl;

importorg.springframework.orm.hibernate3.support.HibernateDaoSupport;

importvo.User;
importdao.UserDao;

publicclassUserDaoImplextendsHibernateDaoSupportimplementsUserDao{

publicbooleaninsertUser(Useruser){
booleanb=false;
this.getHibernateTemplate().save(user);
b=true;
returnb;
}
}

现在全部代码也写好了,下面看一下整个工程的目录结构:

 

所有JAR包如下图所示:

 

下面我们部署好测试一下,测试前得先把数据库建好,数据库名字和jdbc.properties里的jdbc.url里的数据库名字一样jdbc.url=jdbc:mysql://localhost:3306/ssh2,所以这里我们新建一个ssh2的数据库,

启动服务器进行测试:

http://localhost:8088/ssh2/

 

 

 

我们进入到MYSQL,里面也有我们插入的数据。

到此全部整合成功了。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值