2019独角兽企业重金招聘Python工程师标准>>>
Struts2+Spring+Hibernate是J2EE的最新流行框架。本篇是我搭建这个框架的经验总结,有很多人搭建这个框架总会遇到
大大小小的问题,网上也没有什么行之有效的方案或成体系的介绍,所以我就决定总结一下我的搭建过程。给一些搭
建尚存问题的朋友提供帮助。
我用这个框架,实现的是基本的CRUD功能的一个雇员管理系统,本来打算丰富一下功能,但是一直没能抽出空去搞。
目前版本暂定为1.0,除了CRUD外还配置了表单验证框架JSValidation。功能都能很顺利的实现。
现在分享部分源码,来说明一些注意事项。
以下是部分搭建过程及源码:
1.先组合实现Hibernate3.2+Spring2.5支持,删除hibernate.cfg.xml文件,修改applicationContext.xml文件的内容,增加SessionFactory和dataSource的设置。
2.通过MyEclipse的向导方式,生成POJO类和对应的映射文件。
3.修改applicationContext.xml文件中
4.编写DAO接口和实现类。
5.修改applicationContext.xml文件,增加对Dao实现类的配置。
6.组合Struts2和Spring2.5,修改web.xml文件,增加struts2的所需要的过滤器配置。
7.增加struts2相应类库,增加struts2与spring的配置jar包。
8.拷贝struts.xml文件到src根目录下,再修改struts.xml文件,进行常量配置。
9.修改web.xml文件,配置Spring监听器,和上下文变量。并增加OpenSessionInViewFilter的设置。
10.写入action类。
11.配置struts.xml文件。
12.修改applicationContext.xml
13.编写Jsp文件。
14.加载运行项目。
下面是关键文件的源码:
struts.xml源码:
DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd" >
< struts >
< constant name &#61;"struts.objectFactory" value &#61;"spring" />
< package name &#61;"crm_employee" extends &#61;"struts-default" namespace &#61;"/emp" >
< action name &#61;"add" class &#61;"addBean" method &#61;"add" >
< result > add.action result >
< result > /emp/add_suc.jsp result >
action >
< action name &#61;"list" class &#61;"listBean" method &#61;"list" >
< result > /emp/list.jsp result >
action >
< action name &#61;"delete" class &#61;"deleteBean" method &#61;"delete" >
< result > delete.action result >
< result > /emp/delete_suc.jsp result >
action >
< action name &#61;"update" class &#61;"updateBean" method &#61;"update" >
< result > update.action result >
< result > /emp/edit_suc.jsp result >
action >
< action name &#61;"edit" class &#61;"editBean" method &#61;"edit" >
< result > /emp/edit.jsp result >
action >
package >
struts >
web.xml源码&#xff1a;
< web-app version &#61;"2.5" xmlns &#61;"http://java.sun.com/xml/ns/javaee"
xmlns:xsi &#61;"http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation &#61;"http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" >
< context-param >
< param-name > contextConfigLocation param-name >
< param-value > /WEB-INF/applicationContext*.xml param-value >
context-param >
< listener >
< listener-class >
org.springframework.web.context.ContextLoaderListener
listener-class >
listener >
< filter >
< filter-name > lazyLoadingFilter filter-name >
< filter-class >
org.springframework.orm.hibernate3.support.OpenSessionInViewFilter
filter-class >
filter >
< filter >
< filter-name > struts2 filter-name >
< filter-class >
org.apache.struts2.dispatcher.FilterDispatcher
filter-class >
filter >
< filter-mapping >
< filter-name > lazyLoadingFilter filter-name >
< url-pattern > *.action url-pattern >
filter-mapping >
< filter-mapping >
< filter-name > struts2 filter-name >
< url-pattern > /* url-pattern >
filter-mapping >
< welcome-file-list >
< welcome-file > index.jsp welcome-file >
welcome-file-list >
web-app >
applicationContext.xml源码&#xff1a;
< beans xmlns &#61;"http://www.springframework.org/schema/beans"
xmlns:xsi &#61;"http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop &#61;"http://www.springframework.org/schema/aop"
xmlns:tx &#61;"http://www.springframework.org/schema/tx"
xsi:schemaLocation &#61;"
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-2.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-2.0.xsd" >
< bean id &#61;"dataSource"
class &#61;"org.apache.commons.dbcp.BasicDataSource" >
< property name &#61;"driverClassName"
value &#61;"com.mysql.jdbc.Driver" >
property >
< property name &#61;"url"
value &#61;"jdbc:mysql://localhost:3306/tables" >
property >
< property name &#61;"username" value &#61;"root" > property >
< property name &#61;"password" value &#61;"hicc" > property >
bean >
< bean id &#61;"sessionFactory"
class &#61;"org.springframework.orm.hibernate3.LocalSessionFactoryBean" >
< property name &#61;"dataSource" >
< ref bean &#61;"dataSource" />
property >
< property name &#61;"hibernateProperties" >
< props >
< prop key &#61;"hibernate.dialect" >
org.hibernate.dialect.MySQLDialect
prop >
< prop key &#61;"hibernate.show_sql" > true prop >
props >
property >
< property name &#61;"mappingResources" >
< list >
< value > com/sy/crm/model/Employee.hbm.xml value >
list >
property >
bean >
< bean id &#61;"employeeDao"
class &#61;"com.sy.crm.dao.hibernate.EmployeeDaoHibernate" >
< property name &#61;"sessionFactory" >
< ref bean &#61;"sessionFactory" />
property >
bean >
< bean id &#61;"employeeManager"
class &#61;"com.sy.crm.service.impl.EmployeeManagerImpl" >
< property name &#61;"employeeDao" >
< ref bean &#61;"employeeDao" />
property >
bean >
< bean id &#61;"addBean" class &#61;"com.sy.crm.action.EmployeeAction" scope &#61;"prototype" >
< property name &#61;"employeeManager" >
< ref bean &#61;"employeeManager" />
property >
bean >
< bean id &#61;"listBean" class &#61;"com.sy.crm.action.EmployeeAction" scope &#61;"prototype" >
< property name &#61;"employeeManager" >
< ref bean &#61;"employeeManager" />
property >
bean >
< bean id &#61;"deleteBean" class &#61;"com.sy.crm.action.EmployeeAction" scope &#61;"prototype" >
< property name &#61;"employeeManager" >
< ref bean &#61;"employeeManager" />
property >
bean >
< bean id &#61;"updateBean" class &#61;"com.sy.crm.action.EmployeeAction" scope &#61;"prototype" >
< property name &#61;"employeeManager" >
< ref bean &#61;"employeeManager" />
property >
bean >
< bean id &#61;"editBean" class &#61;"com.sy.crm.action.EmployeeAction" scope &#61;"prototype" >
< property name &#61;"employeeManager" >
< ref bean &#61;"employeeManager" />
property >
bean >
< bean id &#61;"transactionManager"
class &#61;"org.springframework.orm.hibernate3.HibernateTransactionManager" >
< property name &#61;"sessionFactory" >
< ref local &#61;"sessionFactory" />
property >
bean >
< tx:advice id &#61;"txAdvice" transaction-manager &#61;"transactionManager" >
< tx:attributes >
< tx:method name &#61;"add*" propagation &#61;"REQUIRED" />
< tx:method name &#61;"delete*" propagation &#61;"REQUIRED" />
< tx:method name &#61;"update*" propagation &#61;"REQUIRED" />
< tx:method name &#61;"*" read-only &#61;"true" />
tx:attributes >
tx:advice >
< aop:config >
< aop:pointcut id &#61;"allManagerMethod" expression &#61;"execution(*
com.sy.crm.service.*.*(..))" />
< aop:advisor advice-ref &#61;"txAdvice" pointcut-ref &#61;"allManagerMethod" />
aop:config >
beans >
<% &#64; taglib uri &#61; " /struts-tags " prefix &#61; " s " %>
DOCTYPE HTML PUBLIC " -//W3C//DTD HTML 4.01 Transitional//EN " >
< html >
< head >
< title > add page title >
< script language &#61; " Javascript " src &#61; " validation-framework.js " > script >
< meta http - equiv &#61; " pragma " content &#61; " no-cache " >
< meta http - equiv &#61; " cache-control " content &#61; " no-cache " >
< meta http - equiv &#61; " expires " content &#61; " 0 " >
< meta http - equiv &#61; " keywords " content &#61; " keyword1,keyword2,keyword3 " >
< meta http - equiv &#61; " description " content &#61; " This is my page " >
head >
< body >
< center >
< h3 > 雇员注册&#xff1a; h3 >< br >
< h4 >< a href &#61; " ../emp/list.action " > 查看所有雇员 a > h4 >
< div id &#61; " error " style &#61; " color:blue; font-weight:bold; " > div >
< s:form action &#61; " add " method &#61; " post " onsubmit &#61; " return doValidate(&#39;form&#39;) " name &#61; " form " id &#61; " form " >
< s:textfield name &#61; " employee.name " label &#61; " 姓名 " id &#61; " name " />
< s:textfield name &#61; " employee.address " label &#61; " 地址 " />
< s:textfield name &#61; " employee.phone " label &#61; " 电话 " />
< s:submit value &#61; " 员工注册 " />
s:form >
center >
body >
html >
list.jsp源码&#xff1a;
<% &#64; taglib uri &#61; " /struts-tags " prefix &#61; " s " %>
DOCTYPE HTML PUBLIC " -//W3C//DTD HTML 4.01 Transitional//EN " >
< html >
< head >
< title > list employee page title >
< meta http - equiv &#61; " pragma " content &#61; " no-cache " >
< meta http - equiv &#61; " cache-control " content &#61; " no-cache " >
< meta http - equiv &#61; " expires " content &#61; " 0 " >
< meta http - equiv &#61; " keywords " content &#61; " keyword1,keyword2,keyword3 " >
< meta http - equiv &#61; " description " content &#61; " This is my page " >
< style type &#61; " text/css " >
table {
border: 1px solid black;
border - collapse: collapse;
}
table thead tr th {
border: 1px solid black;
padding: 3px;
background - color: #cccccc;
}
table tbody tr td {
border: 1px solid black;
padding: 3px;
}
style >
head >
< body >
< center >
< h3 >
雇员管理&#xff1a;
h3 >
< br >
< h4 >
< a href &#61; " ../emp/add.jsp " > 员工注册 a >
h4 >
< s:form action &#61; " delete " theme &#61; " simple " >
< table >
< thead >
< tr >
< th >
选择
th >
< th >
编号
th >
< th >
姓名
th >
< th >
电话
th >
< th >
地址
th >
< th >
操作
th >
tr >
thead >
< tbody >
< s:iterator value &#61; " employees " >
< tr >
< td >
< input type &#61; " checkbox " name &#61; " id "
value &#61; &#39;
td >
< td >
< s:property value &#61; " id " />
td >
< td >
< s:property value &#61; " name " />
td >
< td >
< s:property value &#61; " phone " />
td >
< td >
< s:property value &#61; " address " />
td >
< td >
< a
href &#61; &#39;
修改 a > & nbsp;
< a
href &#61; &#39;
删除 a >
td >
tr >
s:iterator >
tbody >
table >
< s:submit value &#61; " delete " />
s:form >
center >
body >
html >