- 浏览: 108562 次
- 性别:
- 来自: 成都
文章分类
最新评论
-
lws0402:
sghys 写道附件根本没这代码代码都贴在网上了,很简单。附件 ...
jdk 1.5 搭建 jax-ws -
sghys:
附件根本没这代码
jdk 1.5 搭建 jax-ws -
dukunpeng22:
按照楼主的思路,也可以做一个文字的倒影效果,蛮不错的
ImageView倒影效果 -
yangpanwww:
哈哈。。原因不是你说的这个原因吧
在 tomc ...
Xfire调用webservice出错 -
mapeijie888:
貌似还是不行啊 兄弟贴代码出来看看
javah编译带包class文件出错解决方法
框架整合及开发步骤
一、开发工具 2
二、开发框架 2
2.1、Struts2.0.14 2
2.2、Spring2.5.4 2
2.3、Hibernate3.2.5 3
2.4、jQuery 4
三、所需jar包 4
四、项目目录 5
五、整合步骤 6
5.1、spring 6
5.2、struts 7
5.3、hibernate 7
六、开发步骤 11
6.1、配置xml文件 11
6.2、编写pojo对象并生成与数据库对应的hbm.xml文件。 16
6.3、编写操作数据库的Dao类 16
6.4、编写业务逻辑代码 18
6.5、编写action类 20
6.6、编写JSP页面 23
一、开发工具
MyEclipse7.0
MySql5.0
JDK1.6
Jboss5.0
二、开发框架
2.1、Struts2.0.14
Struts是一个在JSP Model2基础上实现的MVC框架,主要分为模型(Model)、视图(Viewer)和控制器(Controller)三部分,其主要的设计理念是通过控制器将表现逻辑和业务逻辑解耦,以提高系统的可维护性、可扩展性和可重用性。Struts框架的体系结构如图所示:
视图(Viwer):视图部分主要由JSP页面组成,其中不包括业务逻辑和持久化操作,数据由struts标签或jstl标签呈现在页面上。
控制层(Controller):控制层由FilterDispatcher组成,FilterDispatcher负责过滤接收HTTP请求,然后根据配置文件(struts.xml)调用相关的Action。
模型(Model):struts1中有ActionFormBean作为模型,但在struts2中,ActionFormBean与Action合并,就没有严格意义上的模型了。一般由持久化对象作为模型。
2.2、Spring2.5.4
Spring是一个轻量级的MVC框架,在SSH框架整合中,Spring作为一个管理者,管理和提供项目中的所有组件。Spring框架体系结构如图:
2.3、Hibernate3.2.5
Hibernate对数据进行持久化,运用O/R mapping对关系型数据库和java对象进行映射,简化了开发人员对数据库的操作。Hibernate原理图如下:
2.4、jQuery
Jquery是继prototype之后又一个优秀的Javascrīpt框架。它是轻量级的js库,兼容CSS3,还兼容各种浏览器 (IE 6.0+, FF 1.5+, Safari 2.0+, Opera 9.0+)。jQuery使用户能更方便地处理HTML documents、events、实现动画效果,并且方便地为网站提供AJAX交互。jQuery还有一个比较大的优势是,它的文档说明很全,而且各种应用也说得很详细,同时还有许多成熟的插件可供选择。jQuery能够使用户的html页保持代码和html内容分离,也就是说,不用再在html里面插入一堆js来调用命令了,只需定义id即可。
只需要在页面上导入jQuery的包就可以使用该框架了,具体的语法参照http://www.w3school.com.cn/jquery/
三、所需jar包
dom4j.jar
hibernate.jar
log4j.jar
mysql-connectior-java.jar
proxool.jar
proxool-cglib.jar
spring-beans.jar
spring-context.jar
spring-core.jar
spring-orm.jar
spring-tx.jar
spring-web.jar
spring-webmvc-struts.jar
struts2-core.jar
struts2-spring-plugin.jar
xwork.jar
commons-logging.jar
jta.jar
commons-collections.jar
cglib-nodep.jar
antlr.jar
四、项目目录
java目录
说明:
config:存放项目的配置文件
common:存放公共组件,如影片关联的显示,打印,日志导出等等
action:存放action模块
service:存放业务逻辑模块
hibernateDao:存放对数据库的操作类
entity:存放POJO对象及hbm.xml文件
exception:存放自定义的异常类
log:存放项目日志
页面目录
hotelPortal:存放Portal端的页面
hotelmanager:存放管理端的页面
js:存放js文件
css:存放css样式文件
五、整合步骤
5.1、spring
在web.xml中加入下面的监听代码告知项目使用spring框架:
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
同时设置application-context.xml文件的位置以便系统加载,如果不做该设置,系统默认加载/WEB-INF/applicationContext.xml文件:
<context-param>
<param-name>
contextConfigLocation
</param-name>
<param-value>
classpath*:/application/application-context.xml
</param-value>
</context-param>
application-context.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:context="http://www.springframework.org/schema/context"
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/context
http://www.springframework.org/schema/context/spring-context-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 ">
</beans>
现在spring框架就搭建好了。
5.2、struts
struts1与struts2相比,变化比较大,在web.xml文件中, struts1采用<servlet>的方式,而struts2中要配置成<filter>形式,具体配置如下:
<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>
struts1中配置文件为struts-config.xml,路径为/WEB-INF/struts-config.xml,在struts2中配置文件为struts.xml,路径为/WEB-INF/classes/struts.xml,空的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>
</struts>
5.3、hibernate
在这里配合proxool数据库连接池一起使用,首先在web.xml中加入proxool的配置:
<servlet>
<servlet-name>ServletConfigurator</servlet-name>
<servlet-class>
org.logicalcobwebs.proxool.configuration.ServletConfigurator
</servlet-class>
<init-param>
<param-name>xmlFile</param-name>
<param-value>/WEB-INF/proxool.xml</param-value>
</init-param>
<load-on-startup>0</load-on-startup>
</servlet>
<servlet>
<servlet-name>Admin</servlet-name>
<servlet-class>
org.logicalcobwebs.proxool.admin.servlet.AdminServlet
</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>Admin</servlet-name>
<url-pattern>/admin</url-pattern>
</servlet-mapping>
创建proxool.xml文件用来配置数据库连接信息,并将该文件放在/WEB-INF目录下
proxool.xml:
<?xml version="1.0" encoding="UTF-8"?>
<something-else-entirely>
<proxool>
<alias>dbname</alias>
<driver-url>jdbc:mysql://192.168.111.204:3306/lws</driver-url>
<driver-class>com.mysql.jdbc.Driver</driver-class>
<driver-properties>
<property name="user" value="root"/>
<property name="password" value="123456"/>
</driver-properties>
<maximum-connection-count>1000</maximum-connection-count>
<minimum-connection-count>100</minimum-connection-count>
<house-keeping-sleep-time>90000</house-keeping-sleep-time>
<maximum-new-connections>10</maximum-new-connections>
<prototype-count>10</prototype-count>
<simultaneous-build-throttle>20</simultaneous-build-throttle>
<test-before-use>false</test-before-use>
<house-keeping-test-sql>select 1 from dual</house-keeping-test-sql>
</proxool>
</something-else-entirely>
如果将proxool.xml放置在其他位置,需要在<param-value>/WEB-INF/proxool.xml</param-value>处指定位置。
创建hibernate-connection.xml文件,放在与application-context.xml同级的目录下
hibernate-connection.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:context="http://www.springframework.org/schema/context"
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/context
http://www.springframework.org/schema/context/spring-context-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 ">
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="org.logicalcobwebs.proxool.ProxoolDriver" />
<property name="url" value="proxool.dbname" />
</bean>
<bean name="sessionFactory" scope="singleton" lazy-init="true" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="mappingResources" ref="hibernateMappingResources" />
<property name="hibernateProperties" ref="hibernateProperties" />
</bean>
<bean name="hibernateMappingResources" scope="singleton" lazy-init="true"
class="org.springframework.beans.factory.config.ListFactoryBean">
<property name="targetListClass" value="java.util.ArrayList" />
<property name="sourceList">
<list>
<!—- 这里是对象的hbm.xml文件 -->
<value>XXX/XXX.hbm.xml</value>
</list>
</property>
</bean>
<bean name="hibernateProperties" scope="singleton" lazy-init="true"
class="org.springframework.beans.factory.config.PropertiesFactoryBean">
<property name="properties">
<props>
<!-- properties for different database -->
<prop key="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</prop>
<!-- properties for debugging -->
<prop key="hibernate.show_sql">false</prop>
<prop key="hibernate.generate_statistics">true</prop>
<prop key="hibernate.use_sql_comments">true</prop>
</props>
</property>
</bean>
</beans>
Hibernate是根据对象的hbm.xml文件与数据库表信息相对应的,列入数据库中有student表:
CREATE TABLE `student` (
`id` int(10) NOT NULL AUTO_INCREMENT,
`name` varchar(200) DEFAULT NULL,
`name_1` varchar(200) DEFAULT NULL,
`flag` varchar(200) DEFAULT NULL,
PRIMARY KEY (`id`)
)
创建一个名为StudentImpl的javaBean
StudentImpl.java:
public class StudentImpl implements Student
{
private long id;
private String name;
private String name_1;
private String flag;
//get和set方法略去
}
在Student.hbm.xml中将javaBean和数据库student表对应
Student.hbm.xml:
<?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="entity.student.StudentImpl" table="student">
<id name="id" type="java.lang.Long" column="id">
<generator class="native">
<param name="sequence">SQ_Student_Key</param>
</generator>
</id>
<property name="name" type="string" column="name" not-null="true" length="200"/>
<property name="name_1" type="string" column="name_1" not-null="false" length="200"/>
<property name="flag" type="string" column="flag" not-null="false" length="200"/>
</class>
</hibernate-mapping>
将对象hbm.xml文件的相对路径配置在hibernate-connection.xml里。
在spring的配置文件application-context.xml中加入hibernate-connection.xml的配置,使得加载spring同时加载hibernate。
六、开发步骤
6.1、配置xml文件
web.xml:
<?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">
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:com/kt/hotel/config/application.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<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>
<servlet>
<servlet-name>ServletConfigurator</servlet-name>
<servlet-class>
org.logicalcobwebs.proxool.configuration.ServletConfigurator
</servlet-class>
<init-param>
<param-name>xmlFile</param-name>
<param-value>WEB-INF/classes/com/kt/hotel/config/proxool.xml</param-value>
</init-param>
<load-on-startup>0</load-on-startup>
</servlet>
<servlet>
<servlet-name>Admin</servlet-name>
<servlet-class>
org.logicalcobwebs.proxool.admin.servlet.AdminServlet
</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>Admin</servlet-name>
<url-pattern>/admin</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
struts.xml
用struts.xml来管理各个模块的struts配置
<?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>
<include file="com/kt/hotel/config/struts-movie.xml"></include>
</struts>
struts-movie.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="hotel" namespace=""
extends="struts-default">
<action name="movie" class="movieAction">
<result name="movieList">/page/hotelManager/video/movie/movieList.jsp</result>
<result name="addMoviePage">/page/hotelManager/video/movie/addMovie.jsp</result>
<result name="addSuccess">/page/hotelManager/common/addSuccess.jsp</result>
</action>
<action name="fileLink" class="fileLinkAction">
<result name="fileLink">/page/hotelManager/common/fileLink.jsp</result>
</action>
</package>
</struts>
application.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:context="http://www.springframework.org/schema/context"
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/context
http://www.springframework.org/schema/context/spring-context-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 ">
<bean id="requiredChecker" class="org.springframework.beans.factory.annotation.RequiredAnnotationBeanPostProcessor" />
<import resource="classpath:com/kt/hotel/common/common.xml"/>
<import resource="classpath:com/kt/hotel/config/action.xml"/>
<import resource="classpath:com/kt/hotel/config/hibernate-connection.xml"/>
<import resource="classpath:com/kt/hotel/config/hibernateDao.xml"/>
<import resource="classpath:com/kt/hotel/config/service.xml"/>
</beans>
hibernate-connection.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:context="http://www.springframework.org/schema/context"
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/context
http://www.springframework.org/schema/context/spring-context-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 ">
<bean id="dataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="org.logicalcobwebs.proxool.ProxoolDriver" />
<property name="url" value="proxool.dbname" />
</bean>
<bean id="sessionFactory" scope="singleton" lazy-init="true"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="mappingDirectoryLocations">
<list>
<value>/WEB-INF/classes/com/kt/hotel/entity/hbm</value>
</list>
</property>
<property name="hibernateProperties" ref="hibernateProperties" />
</bean>
<bean id="hibernateProperties" scope="singleton" lazy-init="true"
class="org.springframework.beans.factory.config.PropertiesFactoryBean">
<property name="properties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</prop>
<prop key="hibernate.show_sql">false</prop>
<prop key="hibernate.generate_statistics">true</prop>
<prop key="hibernate.use_sql_comments">true</prop>
</props>
</property>
</bean>
</beans>
proxool.xml
<?xml version="1.0" encoding="UTF-8"?>
<something-else-entirely>
<proxool>
<alias>dbname</alias>
<driver-url>jdbc:mysql://192.168.211.204:3306/hoteldatabase</driver-url>
<driver-class>com.mysql.jdbc.Driver</driver-class>
<driver-properties>
<property name="user" value="root"/>
<property name="password" value="123456"/>
</driver-properties>
<maximum-connection-count>500</maximum-connection-count>
<minimum-connection-count>100</minimum-connection-count>
<house-keeping-sleep-time>90000</house-keeping-sleep-time>
<maximum-new-connections>200</maximum-new-connections>
<prototype-count>100</prototype-count>
<simultaneous-build-throttle>200</simultaneous-build-throttle>
<test-before-use>false</test-before-use>
<house-keeping-test-sql>select 1 from dual</house-keeping-test-sql> </proxool>
</something-else-entirely>
6.2、编写pojo对象并生成与数据库对应的hbm.xml文件。
POJO对象和相应的hbm.xml文件已经建好,这里不做过多阐述。
6.3、编写操作数据库的Dao类
具体的数据库连接、释放等操作交给hibernate和数据库连接池去完成,下面以对影片对象进行添加和查询为例:
首先创建EntityDao类,该类为抽象类,继承了HibernateDaoSupport类,且只有一个SessionFactory参数
EntityDao.java:
public abstract class EntityDao extends HibernateDaoSupport
{
@SuppressWarnings("unused")
private SessionFactory sessionFactory;
}
然后在config文件下创建hibernateDao.xml文件来管理dao的配置,在该xml文件中配置EntityDao并且注入sessionFactory
HibernateDao.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:context="http://www.springframework.org/schema/context"
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/context
http://www.springframework.org/schema/context/spring-context-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 ">
<import resource="classpath*:com/kt/hotel/hibernateDao/hibernateDao-movie.xml"/>
<bean id="entityDao" class="com.kt.hotel.hibernateDao.EntityDao" scope="singleton" abstract="true">
<property name="sessionFactory" ref="sessionFactory"/>
</bean>
</beans>
注意:这里EntityDao要用abstract=”true”配置成抽象的。
接下来编写MovieDaoImpl类,该类是movie的dao类,继承抽象类EntityDao
MovieDaoImpl.java:
public class MovieDaoImpl extends EntityDao implements MovieDao{
public void add(MoviePO movie)
{
this.getHibernateTemplate().save(movie);
}
@SuppressWarnings("unchecked")
public List<MoviePO> getList()
{
String hql = "from MoviePO";
List<MoviePO> list = (List<MoviePO>)this.getHibernateTemplate().find(hql);
return list;
}
}
实现类MovieDaoImpl放在文件夹hibernateDao下的Impl目录里,接口类放在hibernateDao目录下,下面创建hibernateDao-movie.xml文件来管理影视的dao类
hibernateDao-movie.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:context="http://www.springframework.org/schema/context"
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/context
http://www.springframework.org/schema/context/spring-context-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 ">
<bean id="movieDao" class="com.kt.hotel.hibernateDao.impl.MovieDaoImpl" scope="singleton" parent="entityDao">
</bean>
</beans>
其中parent="entityDao"表示继承前面所配置的EntityDao类
6.4、编写业务逻辑代码
在service目录下创建impl目录,该目录存放业务逻辑的实现类。创建MovieServiceImpl.java类来调用MovieDao中的添加和显示方法,
MovieServiceImpl.java:
public class MovieServiceImpl implements MovieService {
private MovieDao movieDao;
public void add(MoviePO movie)
{
movieDao.add(movie);
}
public List<MoviePO> getList()
{
List<MoviePO> list = movieDao.getList();
return list;
}
@Required
public void setMovieDao(MovieDao movieDao) {
this.movieDao = movieDao;
}
}
在service目录下创建service-movie.xml管理影视的业务逻辑
service-movie.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:context="http://www.springframework.org/schema/context"
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/context
http://www.springframework.org/schema/context/spring-context-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 ">
<bean id="movieService" class="com.kt.hotel.service.impl.MovieServiceImpl" scope="singleton">
<property name="movieDao" ref="movieDao" />
</bean>
</beans>
接着在config目录下创建service.xml文件管理各个模块的service-xxx.xml文件
service.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:context="http://www.springframework.org/schema/context"
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/context
http://www.springframework.org/schema/context/spring-context-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 ">
<import resource="classpath*:com/kt/hotel/service/service-movie.xml"/>
</beans>
6.5、编写action类
struts2.0与struts1.x差别很大,在struts2.0中action不需要继承任何类,切方法返回值是字符串,通过返回的字符串来找到相应的跳转页面,
MovieAction.java:
public class MovieAction {
private MovieService movieService;
private MoviePO movie;
public String add()
{
MoviePO entity = (MoviePO)movie;
movieService.add(entity);
HttpServletRequest request = ServletActionContext.getRequest();
request.setAttribute("forwardPage", "movie!getList.action");
return "addSuccess";
}
public String getList()
{
List<MoviePO> list = movieService.getList();
HttpServletRequest request = ServletActionContext.getRequest();
request.setAttribute("movieList", list);
return "movieList";
}
public String forwardAddPage()
{
return "addMoviePage";
}
@Required
public void setMovieService(MovieService movieService) {
this.movieService = movieService;
}
public void setMovie(MoviePO movie) {
this.movie = movie;
}
public MoviePO getMovie() {
return movie;
}
}
其中MovieService是要注入的影视业务类,MoviePO是POJO对象,如果用request,需通过ServletActionContext来获取。
在action目录创建action-movie.xml文件来管理影视的action
action-movie.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:context="http://www.springframework.org/schema/context"
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/context
http://www.springframework.org/schema/context/spring-context-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 ">
<bean id="movieAction" class="com.kt.hotel.action.MovieAction" scope="prototype">
<property name="movieService" ref="movieService" />
</bean>
</beans>
注意:这里的id=”movieAction”的值要与struts.xml中配置的class相一致,
如movieAction对应struts.xml中
<action name="movie" class="movieAction">
<result name="movieList">/page/hotelManager/video/movie/movieList.jsp</result>
<result name="addMoviePage">/page/hotelManager/video/movie/addMovie.jsp</result>
<result name="addSuccess">/page/hotelManager/common/addSuccess.jsp</result>
</action>
class=”movieAction”的movieAction,url中使用/movie就对应到movieAction,然后在action-movie.xml中找到movieAction对应的com.kt.hotel.action.MovieAction类。
<result name="movieList">...</result>中的movieList对应于MovieAction文件中getList方法返回的值,以此来确定跳转的页面。如果方法的返回值在struts.xml中没有找到,会报404错误
然后在config下创建一个action.xml文件管理各个模块的action-xxx.xml文件
action.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:context="http://www.springframework.org/schema/context"
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/context
http://www.springframework.org/schema/context/spring-context-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 ">
<import resource="classpath:com/kt/hotel/action/action-movie.xml"/>
</beans>
访问路径是由工程名,struts.xml配置的action对应的name和Action类中的方法名来指定的,如上例,工程名为hotel,struts.xml中action定义的name是movie,对应action类MovieAction.java中的方法名是getList()和add()如果要调用getList()方法,url为:http://<ip>:<port>/hotel/movie!getList.action,如果要调用add方法,路径为
http://<ip>:<port>/hotel/movie!add.action
6.6、编写JSP页面
在JSP页面使用jstl标签来显示内容。首先在JSP页面加入jstl标签库
<%@taglib prefix="c" uri="http://java.sun.com/jstl/core_rt"%>
<%@taglib prefix="fmt" uri="http://java.sun.com/jstl/fmt"%>
<%@taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions"%>
<%@taglib prefix="s" uri="/struts-tags" %>
然后就可以使用这些标签了,下面是在movieList.jsp页面用<c:forEach>来循环显示
<c:forEach items="${movieList}" var="movie">
<tr align="center">
<td width="5%"><input type="checkbox" name="movie"></td>
<td width="10%">${movie.name}</td>
<td width="15%">${movie.actor}</td>
<td width="15%">${movie.price}</td>
<td width="10%"><a href="javascript:void(0);">修改</a></td>
</tr>
</c:forEach>
其中movieList是在action类中,通过request.setAttribute传到页面上的列表。
addMovie.jsp页面中<input type="text" name="movie.name" size="20">的movie.name要注意,这是struts2.0的新写法,movie对应于MovieAction中定义的movie属性,和setMovie和getMovie方法一起构成了formBean,页面上通过movie.name标示,把值映射到MoviePO对象的name属性中。
一、开发工具 2
二、开发框架 2
2.1、Struts2.0.14 2
2.2、Spring2.5.4 2
2.3、Hibernate3.2.5 3
2.4、jQuery 4
三、所需jar包 4
四、项目目录 5
五、整合步骤 6
5.1、spring 6
5.2、struts 7
5.3、hibernate 7
六、开发步骤 11
6.1、配置xml文件 11
6.2、编写pojo对象并生成与数据库对应的hbm.xml文件。 16
6.3、编写操作数据库的Dao类 16
6.4、编写业务逻辑代码 18
6.5、编写action类 20
6.6、编写JSP页面 23
一、开发工具
MyEclipse7.0
MySql5.0
JDK1.6
Jboss5.0
二、开发框架
2.1、Struts2.0.14
Struts是一个在JSP Model2基础上实现的MVC框架,主要分为模型(Model)、视图(Viewer)和控制器(Controller)三部分,其主要的设计理念是通过控制器将表现逻辑和业务逻辑解耦,以提高系统的可维护性、可扩展性和可重用性。Struts框架的体系结构如图所示:
视图(Viwer):视图部分主要由JSP页面组成,其中不包括业务逻辑和持久化操作,数据由struts标签或jstl标签呈现在页面上。
控制层(Controller):控制层由FilterDispatcher组成,FilterDispatcher负责过滤接收HTTP请求,然后根据配置文件(struts.xml)调用相关的Action。
模型(Model):struts1中有ActionFormBean作为模型,但在struts2中,ActionFormBean与Action合并,就没有严格意义上的模型了。一般由持久化对象作为模型。
2.2、Spring2.5.4
Spring是一个轻量级的MVC框架,在SSH框架整合中,Spring作为一个管理者,管理和提供项目中的所有组件。Spring框架体系结构如图:
2.3、Hibernate3.2.5
Hibernate对数据进行持久化,运用O/R mapping对关系型数据库和java对象进行映射,简化了开发人员对数据库的操作。Hibernate原理图如下:
2.4、jQuery
Jquery是继prototype之后又一个优秀的Javascrīpt框架。它是轻量级的js库,兼容CSS3,还兼容各种浏览器 (IE 6.0+, FF 1.5+, Safari 2.0+, Opera 9.0+)。jQuery使用户能更方便地处理HTML documents、events、实现动画效果,并且方便地为网站提供AJAX交互。jQuery还有一个比较大的优势是,它的文档说明很全,而且各种应用也说得很详细,同时还有许多成熟的插件可供选择。jQuery能够使用户的html页保持代码和html内容分离,也就是说,不用再在html里面插入一堆js来调用命令了,只需定义id即可。
只需要在页面上导入jQuery的包就可以使用该框架了,具体的语法参照http://www.w3school.com.cn/jquery/
三、所需jar包
dom4j.jar
hibernate.jar
log4j.jar
mysql-connectior-java.jar
proxool.jar
proxool-cglib.jar
spring-beans.jar
spring-context.jar
spring-core.jar
spring-orm.jar
spring-tx.jar
spring-web.jar
spring-webmvc-struts.jar
struts2-core.jar
struts2-spring-plugin.jar
xwork.jar
commons-logging.jar
jta.jar
commons-collections.jar
cglib-nodep.jar
antlr.jar
四、项目目录
java目录
说明:
config:存放项目的配置文件
common:存放公共组件,如影片关联的显示,打印,日志导出等等
action:存放action模块
service:存放业务逻辑模块
hibernateDao:存放对数据库的操作类
entity:存放POJO对象及hbm.xml文件
exception:存放自定义的异常类
log:存放项目日志
页面目录
hotelPortal:存放Portal端的页面
hotelmanager:存放管理端的页面
js:存放js文件
css:存放css样式文件
五、整合步骤
5.1、spring
在web.xml中加入下面的监听代码告知项目使用spring框架:
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
同时设置application-context.xml文件的位置以便系统加载,如果不做该设置,系统默认加载/WEB-INF/applicationContext.xml文件:
<context-param>
<param-name>
contextConfigLocation
</param-name>
<param-value>
classpath*:/application/application-context.xml
</param-value>
</context-param>
application-context.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:context="http://www.springframework.org/schema/context"
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/context
http://www.springframework.org/schema/context/spring-context-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 ">
</beans>
现在spring框架就搭建好了。
5.2、struts
struts1与struts2相比,变化比较大,在web.xml文件中, struts1采用<servlet>的方式,而struts2中要配置成<filter>形式,具体配置如下:
<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>
struts1中配置文件为struts-config.xml,路径为/WEB-INF/struts-config.xml,在struts2中配置文件为struts.xml,路径为/WEB-INF/classes/struts.xml,空的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>
</struts>
5.3、hibernate
在这里配合proxool数据库连接池一起使用,首先在web.xml中加入proxool的配置:
<servlet>
<servlet-name>ServletConfigurator</servlet-name>
<servlet-class>
org.logicalcobwebs.proxool.configuration.ServletConfigurator
</servlet-class>
<init-param>
<param-name>xmlFile</param-name>
<param-value>/WEB-INF/proxool.xml</param-value>
</init-param>
<load-on-startup>0</load-on-startup>
</servlet>
<servlet>
<servlet-name>Admin</servlet-name>
<servlet-class>
org.logicalcobwebs.proxool.admin.servlet.AdminServlet
</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>Admin</servlet-name>
<url-pattern>/admin</url-pattern>
</servlet-mapping>
创建proxool.xml文件用来配置数据库连接信息,并将该文件放在/WEB-INF目录下
proxool.xml:
<?xml version="1.0" encoding="UTF-8"?>
<something-else-entirely>
<proxool>
<alias>dbname</alias>
<driver-url>jdbc:mysql://192.168.111.204:3306/lws</driver-url>
<driver-class>com.mysql.jdbc.Driver</driver-class>
<driver-properties>
<property name="user" value="root"/>
<property name="password" value="123456"/>
</driver-properties>
<maximum-connection-count>1000</maximum-connection-count>
<minimum-connection-count>100</minimum-connection-count>
<house-keeping-sleep-time>90000</house-keeping-sleep-time>
<maximum-new-connections>10</maximum-new-connections>
<prototype-count>10</prototype-count>
<simultaneous-build-throttle>20</simultaneous-build-throttle>
<test-before-use>false</test-before-use>
<house-keeping-test-sql>select 1 from dual</house-keeping-test-sql>
</proxool>
</something-else-entirely>
如果将proxool.xml放置在其他位置,需要在<param-value>/WEB-INF/proxool.xml</param-value>处指定位置。
创建hibernate-connection.xml文件,放在与application-context.xml同级的目录下
hibernate-connection.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:context="http://www.springframework.org/schema/context"
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/context
http://www.springframework.org/schema/context/spring-context-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 ">
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="org.logicalcobwebs.proxool.ProxoolDriver" />
<property name="url" value="proxool.dbname" />
</bean>
<bean name="sessionFactory" scope="singleton" lazy-init="true" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="mappingResources" ref="hibernateMappingResources" />
<property name="hibernateProperties" ref="hibernateProperties" />
</bean>
<bean name="hibernateMappingResources" scope="singleton" lazy-init="true"
class="org.springframework.beans.factory.config.ListFactoryBean">
<property name="targetListClass" value="java.util.ArrayList" />
<property name="sourceList">
<list>
<!—- 这里是对象的hbm.xml文件 -->
<value>XXX/XXX.hbm.xml</value>
</list>
</property>
</bean>
<bean name="hibernateProperties" scope="singleton" lazy-init="true"
class="org.springframework.beans.factory.config.PropertiesFactoryBean">
<property name="properties">
<props>
<!-- properties for different database -->
<prop key="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</prop>
<!-- properties for debugging -->
<prop key="hibernate.show_sql">false</prop>
<prop key="hibernate.generate_statistics">true</prop>
<prop key="hibernate.use_sql_comments">true</prop>
</props>
</property>
</bean>
</beans>
Hibernate是根据对象的hbm.xml文件与数据库表信息相对应的,列入数据库中有student表:
CREATE TABLE `student` (
`id` int(10) NOT NULL AUTO_INCREMENT,
`name` varchar(200) DEFAULT NULL,
`name_1` varchar(200) DEFAULT NULL,
`flag` varchar(200) DEFAULT NULL,
PRIMARY KEY (`id`)
)
创建一个名为StudentImpl的javaBean
StudentImpl.java:
public class StudentImpl implements Student
{
private long id;
private String name;
private String name_1;
private String flag;
//get和set方法略去
}
在Student.hbm.xml中将javaBean和数据库student表对应
Student.hbm.xml:
<?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="entity.student.StudentImpl" table="student">
<id name="id" type="java.lang.Long" column="id">
<generator class="native">
<param name="sequence">SQ_Student_Key</param>
</generator>
</id>
<property name="name" type="string" column="name" not-null="true" length="200"/>
<property name="name_1" type="string" column="name_1" not-null="false" length="200"/>
<property name="flag" type="string" column="flag" not-null="false" length="200"/>
</class>
</hibernate-mapping>
将对象hbm.xml文件的相对路径配置在hibernate-connection.xml里。
在spring的配置文件application-context.xml中加入hibernate-connection.xml的配置,使得加载spring同时加载hibernate。
六、开发步骤
6.1、配置xml文件
web.xml:
<?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">
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:com/kt/hotel/config/application.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<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>
<servlet>
<servlet-name>ServletConfigurator</servlet-name>
<servlet-class>
org.logicalcobwebs.proxool.configuration.ServletConfigurator
</servlet-class>
<init-param>
<param-name>xmlFile</param-name>
<param-value>WEB-INF/classes/com/kt/hotel/config/proxool.xml</param-value>
</init-param>
<load-on-startup>0</load-on-startup>
</servlet>
<servlet>
<servlet-name>Admin</servlet-name>
<servlet-class>
org.logicalcobwebs.proxool.admin.servlet.AdminServlet
</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>Admin</servlet-name>
<url-pattern>/admin</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
struts.xml
用struts.xml来管理各个模块的struts配置
<?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>
<include file="com/kt/hotel/config/struts-movie.xml"></include>
</struts>
struts-movie.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="hotel" namespace=""
extends="struts-default">
<action name="movie" class="movieAction">
<result name="movieList">/page/hotelManager/video/movie/movieList.jsp</result>
<result name="addMoviePage">/page/hotelManager/video/movie/addMovie.jsp</result>
<result name="addSuccess">/page/hotelManager/common/addSuccess.jsp</result>
</action>
<action name="fileLink" class="fileLinkAction">
<result name="fileLink">/page/hotelManager/common/fileLink.jsp</result>
</action>
</package>
</struts>
application.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:context="http://www.springframework.org/schema/context"
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/context
http://www.springframework.org/schema/context/spring-context-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 ">
<bean id="requiredChecker" class="org.springframework.beans.factory.annotation.RequiredAnnotationBeanPostProcessor" />
<import resource="classpath:com/kt/hotel/common/common.xml"/>
<import resource="classpath:com/kt/hotel/config/action.xml"/>
<import resource="classpath:com/kt/hotel/config/hibernate-connection.xml"/>
<import resource="classpath:com/kt/hotel/config/hibernateDao.xml"/>
<import resource="classpath:com/kt/hotel/config/service.xml"/>
</beans>
hibernate-connection.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:context="http://www.springframework.org/schema/context"
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/context
http://www.springframework.org/schema/context/spring-context-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 ">
<bean id="dataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="org.logicalcobwebs.proxool.ProxoolDriver" />
<property name="url" value="proxool.dbname" />
</bean>
<bean id="sessionFactory" scope="singleton" lazy-init="true"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="mappingDirectoryLocations">
<list>
<value>/WEB-INF/classes/com/kt/hotel/entity/hbm</value>
</list>
</property>
<property name="hibernateProperties" ref="hibernateProperties" />
</bean>
<bean id="hibernateProperties" scope="singleton" lazy-init="true"
class="org.springframework.beans.factory.config.PropertiesFactoryBean">
<property name="properties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</prop>
<prop key="hibernate.show_sql">false</prop>
<prop key="hibernate.generate_statistics">true</prop>
<prop key="hibernate.use_sql_comments">true</prop>
</props>
</property>
</bean>
</beans>
proxool.xml
<?xml version="1.0" encoding="UTF-8"?>
<something-else-entirely>
<proxool>
<alias>dbname</alias>
<driver-url>jdbc:mysql://192.168.211.204:3306/hoteldatabase</driver-url>
<driver-class>com.mysql.jdbc.Driver</driver-class>
<driver-properties>
<property name="user" value="root"/>
<property name="password" value="123456"/>
</driver-properties>
<maximum-connection-count>500</maximum-connection-count>
<minimum-connection-count>100</minimum-connection-count>
<house-keeping-sleep-time>90000</house-keeping-sleep-time>
<maximum-new-connections>200</maximum-new-connections>
<prototype-count>100</prototype-count>
<simultaneous-build-throttle>200</simultaneous-build-throttle>
<test-before-use>false</test-before-use>
<house-keeping-test-sql>select 1 from dual</house-keeping-test-sql> </proxool>
</something-else-entirely>
6.2、编写pojo对象并生成与数据库对应的hbm.xml文件。
POJO对象和相应的hbm.xml文件已经建好,这里不做过多阐述。
6.3、编写操作数据库的Dao类
具体的数据库连接、释放等操作交给hibernate和数据库连接池去完成,下面以对影片对象进行添加和查询为例:
首先创建EntityDao类,该类为抽象类,继承了HibernateDaoSupport类,且只有一个SessionFactory参数
EntityDao.java:
public abstract class EntityDao extends HibernateDaoSupport
{
@SuppressWarnings("unused")
private SessionFactory sessionFactory;
}
然后在config文件下创建hibernateDao.xml文件来管理dao的配置,在该xml文件中配置EntityDao并且注入sessionFactory
HibernateDao.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:context="http://www.springframework.org/schema/context"
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/context
http://www.springframework.org/schema/context/spring-context-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 ">
<import resource="classpath*:com/kt/hotel/hibernateDao/hibernateDao-movie.xml"/>
<bean id="entityDao" class="com.kt.hotel.hibernateDao.EntityDao" scope="singleton" abstract="true">
<property name="sessionFactory" ref="sessionFactory"/>
</bean>
</beans>
注意:这里EntityDao要用abstract=”true”配置成抽象的。
接下来编写MovieDaoImpl类,该类是movie的dao类,继承抽象类EntityDao
MovieDaoImpl.java:
public class MovieDaoImpl extends EntityDao implements MovieDao{
public void add(MoviePO movie)
{
this.getHibernateTemplate().save(movie);
}
@SuppressWarnings("unchecked")
public List<MoviePO> getList()
{
String hql = "from MoviePO";
List<MoviePO> list = (List<MoviePO>)this.getHibernateTemplate().find(hql);
return list;
}
}
实现类MovieDaoImpl放在文件夹hibernateDao下的Impl目录里,接口类放在hibernateDao目录下,下面创建hibernateDao-movie.xml文件来管理影视的dao类
hibernateDao-movie.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:context="http://www.springframework.org/schema/context"
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/context
http://www.springframework.org/schema/context/spring-context-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 ">
<bean id="movieDao" class="com.kt.hotel.hibernateDao.impl.MovieDaoImpl" scope="singleton" parent="entityDao">
</bean>
</beans>
其中parent="entityDao"表示继承前面所配置的EntityDao类
6.4、编写业务逻辑代码
在service目录下创建impl目录,该目录存放业务逻辑的实现类。创建MovieServiceImpl.java类来调用MovieDao中的添加和显示方法,
MovieServiceImpl.java:
public class MovieServiceImpl implements MovieService {
private MovieDao movieDao;
public void add(MoviePO movie)
{
movieDao.add(movie);
}
public List<MoviePO> getList()
{
List<MoviePO> list = movieDao.getList();
return list;
}
@Required
public void setMovieDao(MovieDao movieDao) {
this.movieDao = movieDao;
}
}
在service目录下创建service-movie.xml管理影视的业务逻辑
service-movie.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:context="http://www.springframework.org/schema/context"
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/context
http://www.springframework.org/schema/context/spring-context-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 ">
<bean id="movieService" class="com.kt.hotel.service.impl.MovieServiceImpl" scope="singleton">
<property name="movieDao" ref="movieDao" />
</bean>
</beans>
接着在config目录下创建service.xml文件管理各个模块的service-xxx.xml文件
service.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:context="http://www.springframework.org/schema/context"
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/context
http://www.springframework.org/schema/context/spring-context-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 ">
<import resource="classpath*:com/kt/hotel/service/service-movie.xml"/>
</beans>
6.5、编写action类
struts2.0与struts1.x差别很大,在struts2.0中action不需要继承任何类,切方法返回值是字符串,通过返回的字符串来找到相应的跳转页面,
MovieAction.java:
public class MovieAction {
private MovieService movieService;
private MoviePO movie;
public String add()
{
MoviePO entity = (MoviePO)movie;
movieService.add(entity);
HttpServletRequest request = ServletActionContext.getRequest();
request.setAttribute("forwardPage", "movie!getList.action");
return "addSuccess";
}
public String getList()
{
List<MoviePO> list = movieService.getList();
HttpServletRequest request = ServletActionContext.getRequest();
request.setAttribute("movieList", list);
return "movieList";
}
public String forwardAddPage()
{
return "addMoviePage";
}
@Required
public void setMovieService(MovieService movieService) {
this.movieService = movieService;
}
public void setMovie(MoviePO movie) {
this.movie = movie;
}
public MoviePO getMovie() {
return movie;
}
}
其中MovieService是要注入的影视业务类,MoviePO是POJO对象,如果用request,需通过ServletActionContext来获取。
在action目录创建action-movie.xml文件来管理影视的action
action-movie.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:context="http://www.springframework.org/schema/context"
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/context
http://www.springframework.org/schema/context/spring-context-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 ">
<bean id="movieAction" class="com.kt.hotel.action.MovieAction" scope="prototype">
<property name="movieService" ref="movieService" />
</bean>
</beans>
注意:这里的id=”movieAction”的值要与struts.xml中配置的class相一致,
如movieAction对应struts.xml中
<action name="movie" class="movieAction">
<result name="movieList">/page/hotelManager/video/movie/movieList.jsp</result>
<result name="addMoviePage">/page/hotelManager/video/movie/addMovie.jsp</result>
<result name="addSuccess">/page/hotelManager/common/addSuccess.jsp</result>
</action>
class=”movieAction”的movieAction,url中使用/movie就对应到movieAction,然后在action-movie.xml中找到movieAction对应的com.kt.hotel.action.MovieAction类。
<result name="movieList">...</result>中的movieList对应于MovieAction文件中getList方法返回的值,以此来确定跳转的页面。如果方法的返回值在struts.xml中没有找到,会报404错误
然后在config下创建一个action.xml文件管理各个模块的action-xxx.xml文件
action.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:context="http://www.springframework.org/schema/context"
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/context
http://www.springframework.org/schema/context/spring-context-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 ">
<import resource="classpath:com/kt/hotel/action/action-movie.xml"/>
</beans>
访问路径是由工程名,struts.xml配置的action对应的name和Action类中的方法名来指定的,如上例,工程名为hotel,struts.xml中action定义的name是movie,对应action类MovieAction.java中的方法名是getList()和add()如果要调用getList()方法,url为:http://<ip>:<port>/hotel/movie!getList.action,如果要调用add方法,路径为
http://<ip>:<port>/hotel/movie!add.action
6.6、编写JSP页面
在JSP页面使用jstl标签来显示内容。首先在JSP页面加入jstl标签库
<%@taglib prefix="c" uri="http://java.sun.com/jstl/core_rt"%>
<%@taglib prefix="fmt" uri="http://java.sun.com/jstl/fmt"%>
<%@taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions"%>
<%@taglib prefix="s" uri="/struts-tags" %>
然后就可以使用这些标签了,下面是在movieList.jsp页面用<c:forEach>来循环显示
<c:forEach items="${movieList}" var="movie">
<tr align="center">
<td width="5%"><input type="checkbox" name="movie"></td>
<td width="10%">${movie.name}</td>
<td width="15%">${movie.actor}</td>
<td width="15%">${movie.price}</td>
<td width="10%"><a href="javascript:void(0);">修改</a></td>
</tr>
</c:forEach>
其中movieList是在action类中,通过request.setAttribute传到页面上的列表。
addMovie.jsp页面中<input type="text" name="movie.name" size="20">的movie.name要注意,这是struts2.0的新写法,movie对应于MovieAction中定义的movie属性,和setMovie和getMovie方法一起构成了formBean,页面上通过movie.name标示,把值映射到MoviePO对象的name属性中。
相关推荐
SSH框架搭建的例子,简单入门级别。大神请绕路,菜鸟可以参考。
本教程将详细介绍如何搭建一个SSH框架,并通过实际例子来帮助初学者理解其工作原理。 首先,Struts2是一个基于MVC设计模式的Web应用框架,它处理HTTP请求并将其转化为业务逻辑,然后将结果返回给用户。Struts2提供...
这个小实例提供了一个基本的SSH框架搭建过程,适用于初学者了解和学习这三大框架的协同工作。 首先,Struts2是MVC(Model-View-Controller)架构模式的一个实现,它负责处理HTTP请求并控制应用的流程。Struts2提供...
总的来说,SSH框架的项目搭建涉及到许多IT领域的核心知识点,包括Web开发、数据库管理、对象关系映射、MVC设计模式、依赖注入、前端开发等,对于提升Java Web开发技能大有裨益。通过实践这个项目,新手能够更好地...
这个压缩包文件"SSH框架"可能包含了搭建SSH框架的详细步骤文档,包括每个组件的配置文件示例、实体类、DAO、Service、Action的代码片段,以及如何整合这些组件的指导。下载后,开发者可以通过阅读文档逐步实践,从而...
SSH框架,全称为Struts2、Spring和Hibernate的组合,是Java开发中广泛...通过这个小例子,你可以深入理解SSH框架的整合方式,学习如何在Eclipse中搭建、运行和调试SSH应用,这将为你的Java Web开发能力打下坚实基础。
做这个东西居然花了不少时间,也折腾了不少时间——尽管这并不是一个复杂的东西。 环境:jdk1.8 tomcat8.5 mysql Eclipse 使用的是注解的方式完成的,因为注解的方式可以减少一些配置文件,比较方便的。...
"SSH_Self"这个压缩包文件很可能包含了搭建SSH框架的基本步骤,包括项目的结构、配置文件、源代码以及可能的测试用例。新手可以通过这个实例了解如何配置和使用这三个框架。一般步骤如下: - 创建项目结构:包括...
SSH(Struts+Spring+Hibernate)是Java Web开发中...然而,随着项目的复杂度增加,SSH框架也可能会带来配置繁琐、性能损耗等问题,因此在大型项目中,现代的Java Web框架如Spring Boot和Spring MVC逐渐成为主流选择。
标题中的“亲手搭建基于SSH框架例子”指的是在IT领域中,使用Struts、Spring和Hibernate这三个开源框架构建一个Web应用程序的实践过程。SSH是Java Web开发中常用的三大框架,它们分别负责表现层(Struts)、业务逻辑...
首先,我们来详细了解SSH框架搭建的步骤: 1. **环境准备**:在MyEclipse等集成开发环境中创建一个新的项目,选择合适的Web应用模板。这一步是为后续的框架配置提供基础。 2. **创建Web工程**:创建一个新的Web...
在搭建SSH2框架时,你需要完成以下步骤: 1. 引入相关框架的jar包。 2. 配置Spring的IoC容器,定义Bean的依赖关系。 3. 配置Struts2的配置文件,定义Action及其关联的Service和Controller。 4. 配置Hibernate的持久...
以下是对这些框架及其在"搭建SSH框架的学习例子"中所涉及的知识点的详细说明。 **Struts** Struts是MVC(Model-View-Controller)设计模式的一种实现,主要用于处理HTTP请求和控制应用的流程。它通过Action类来接收...
用SSH框架搭建的一个简单的小项目,用的是struts2,里面有增删改查操作,压缩包中附带了MySQL的数据库脚本。可以把crm2项目直接导入MyEclipse,把employee.sql导入MySQL,想学习SSH框架的朋友可以下载下来看看,方便...
在SSHDemo(XML版本)这个项目中,你可以期待看到一个完整的示例,展示了如何利用XML配置文件搭建SSH框架的应用程序。这个例子可能会包括用户登录、注册等基本功能,通过Struts2接收和响应HTTP请求,Spring处理业务...
对于新手来说,独自完成一个完整正确的SSH框架可能... 最近时间还比较宽裕,为此自己搭建了一个SSH框架,合理和简明的框架结构。并找了几个很详细的例子的代码,这是应该是新手最希望得到的资料,希望大家互相学习哈。
虽然在SSH框架中,我们通常使用Hibernate进行数据库交互,但了解Spring JDBC也是很重要的,因为它可以帮助理解Spring如何处理数据库事务。 对于这个实例,你需要将Pro_ssh_02.rar解压,其中应该包含项目的源代码、...
【SSH+Oracle+MyEclipse框架搭建详解】 SSH框架,由Struts、Spring和Hibernate三个开源框架组成,常用于构建企业级的Java Web应用程序。在MyEclipse6.5环境中,我们将结合Oracle数据库来搭建一个登录框架。以下是...
**SSH框架的搭建** 需要以下步骤: 1. 安装配置IDE:例如MyEclipse,这是Java开发常用的集成开发环境。 2. 引入SSH框架的库:在项目中添加Struts2、Spring和Hibernate的jar包或者依赖。 3. 配置web.xml:设置过滤器...