本教程是在Idea中使用Maven搭建Struts2+Spring3+hibernate4的开发环境
做Java开发有一年多了,几个项目中使用最多的还是这套SSH框架,在这里记录一下
Maven的安装与配置以及创建一个web项目可以看下面这篇博文
Java框架基础教程 : Maven安装与配置以及在IDEA中使用Maven创建一个web项目
这里使用的是IDEA,虽然最早做Java开发时用的是Eclipse,也用了很久,但后来用了IDEA后,再用Eclipse总觉得很不方便.
一.创建一个Maven项目,将Create from archetype(原型)勾选上
然后next
然后next
接下来点击Finish,等待maven下载所需的依赖包,项目就已经搭建好了
二.配置SSH环境.
1.配置pom.xml (下载所需的Jar包)
更改pom.xml后,Maven会自动从中心仓库下载所需的Jar包,我用的是阿里的仓库.
UTF-8 1.7 1.7 UTF-8 UTF-8 junit junit 4.11 test log4j log4j 1.2.17 javax.servlet javax.servlet-api 3.1-b09 provided mysql mysql-connector-java 5.1.24 com.mchange c3p0 0.9.5-pre2 junit junit 4.11 test com.alibaba fastjson 1.1.29 net.sf.json-lib json-lib 2.4 jdk15 org.apache.struts struts2-core 2.3.14 javassist javassist org.apache.struts struts2-spring-plugin 2.3.14 org.apache.struts struts2-convention-plugin 2.3.14 org.springframework spring-core 3.2.2.RELEASE org.springframework spring-context 3.2.2.RELEASE org.springframework spring-context-support 3.2.2.RELEASE org.springframework spring-jdbc 3.2.2.RELEASE org.springframework spring-beans 3.2.2.RELEASE org.springframework spring-web 3.2.2.RELEASE org.springframework spring-expression 3.2.2.RELEASE org.springframework spring-orm 3.2.2.RELEASE org.aspectj aspectjweaver 1.7.2 org.hibernate hibernate-core 4.2.0.Final
2.更改项目结构 (增加所需的文件夹)
src/main/java:存放java源文件
src/main/resources:存放项目配置文件,如struts.xml,spring.xml,hibernate.cfg.xml等
3.编写配置文件
web.xml
xml version="1.0" encoding="UTF-8"?>xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" version="3.0"> contextConfigLocation classpath:spring.xml,classpath:spring-hibernate.xml org.springframework.web.context.ContextLoaderListener encodingFilter org.springframework.web.filter.CharacterEncodingFilter encoding UTF-8 encodingFilter /* openSessionInView org.springframework.orm.hibernate4.support.OpenSessionInViewFilter openSessionInView /* struts2 org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter struts2 /* index.jsp
spring.xml
xml version="1.0" encoding="UTF-8"?>xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <context:property-placeholder location="classpath:database.properties" /> <context:component-scan base-package="com.ljb" />
spring-hibernate.xml
xml version="1.0" encoding="UTF-8"?>xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd "> id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close"> name="driverClass" value="${driverClassName}"> name="jdbcUrl" value="${url}"> name="user" value="${username}"> name="password" value="${password}"> name="maxPoolSize" value="40"> name="minPoolSize" value="1"> name="initialPoolSize" value="1"> name="maxIdleTime" value="20"> id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean"> name="dataSource" ref="dataSource" /> name="packagesToScan" value="com.ljb.*.entity" /> name="hibernateProperties"> key="hibernate.hbm2ddl.auto">${hibernate.hbm2ddl.auto} key="hibernate.dialect">${hibernate.dialect} key="hibernate.show_sql">${hibernate.show_sql} key="hibernate.format_sql">${hibernate.format_sql} name="txManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager"> <tx:annotation-driven transaction-manager="txManager" />name="sessionFactory" ref="sessionFactory">
struts.xml
xml version="1.0" encoding="UTF-8"?> struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN" "http://struts.apache.org/dtds/struts-2.3.dtd">配置完成name="struts.objectFactory" value="spring" /> name="struts.convention.package.locators" value="ljb" /> name="struts.convention.package.locators.basePackage" value="com" /> name="struts.devMode" value="true" /> name="struts.configuration.xml.reload" value="true" /> name="struts.i18n.encoding" value="utf-8" /> name="defaultPackage" namespace="/" extends="struts-default">