struts2+hibernate+spring配置版框架搭建以及简单测试(方便脑补)

时间:2021-11-29 07:42:59

为了之后学习的日子里加深对框架的理解和使用,这里将搭建步奏简单写一下,目的主要是方便以后自己回来脑补;

1:File--->New--->Other--->Maven--->Maven Project--->Next(之后界面如下所示:)

struts2+hibernate+spring配置版框架搭建以及简单测试(方便脑补)

--->Next(点击next之后出现如下界面:选择最后一个 maven-archetype-webapp,然后点击next)

struts2+hibernate+spring配置版框架搭建以及简单测试(方便脑补)

--->Next(点击next之后出现如下界面,然后选择好组织号,工程号,版本号即可),最后点击Finish

struts2+hibernate+spring配置版框架搭建以及简单测试(方便脑补)

2:maven项目创建好之后会报错,这里先在pom.xml中导入jar包,导入方式如下所示:这个pom配置好之后点击保存会将jar包下载到本地仓库,这个学习maven的时候都配置过,这个自己下去脑补吧;

 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.bie.maven</groupId>
<artifactId>ssh_xml</artifactId>
<packaging>war</packaging>
<version>0.0.1-SNAPSHOT</version>
<name>ssh_xml Maven Webapp</name>
<url>http://maven.apache.org</url> <!-- 配置一下jar包 -->
<properties>
<java-version>1.8</java-version>
<junit-version>4.8.2</junit-version>
<org.springframework-version>3.2.4.RELEASE</org.springframework-version>
<jackson-mapper-asl-version>1.9.12</jackson-mapper-asl-version>
<jackson-core-asl-version>1.9.12</jackson-core-asl-version>
<mysql-connector-java-version>5.1.8</mysql-connector-java-version> <opensymphony-version>2.4.2</opensymphony-version>
<freemarker-version>2.3.9</freemarker-version>
<c3p0-version>0.9.1.2</c3p0-version>
<commons-collections-version>1.0</commons-collections-version>
<commons-fileupload-version>1.2.2</commons-fileupload-version>
<org.apache.commons-version>3.1</org.apache.commons-version>
<commons-codec-version>1.6</commons-codec-version>
<dom4j-version>1.6.1</dom4j-version>
<javax.servlet-version>1.2</javax.servlet-version>
<aspectjweaver-version>1.6.6</aspectjweaver-version>
<slf4j-log4j12-version>1.6.6</slf4j-log4j12-version>
<log4j-version>1.2.16</log4j-version>
<javax.servlet-jsp-version>2.0</javax.servlet-jsp-version>
<cglib-version>2.2.2</cglib-version>
<slf4j-api-version>1.6.6</slf4j-api-version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<!-- 配置 junit4.8.2 -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>${junit-version}</version>
<type>jar</type>
</dependency>
<!-- 配置 spring -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>${org.springframework-version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>${org.springframework-version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>${org.springframework-version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aspects</artifactId>
<version>${org.springframework-version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context-support</artifactId>
<version>${org.springframework-version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>${org.springframework-version}</version>
</dependency> <dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>${org.springframework-version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>${org.springframework-version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<version>${org.springframework-version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-instrument</artifactId>
<version>${org.springframework-version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-instrument-tomcat</artifactId>
<version>${org.springframework-version}</version>
</dependency> <dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-orm</artifactId>
<version>${org.springframework-version}</version>
</dependency>
<!--springmvc 转换为 Json -->
<dependency>
<groupId>org.codehaus.jackson</groupId>
<artifactId>jackson-mapper-asl</artifactId>
<version>${jackson-mapper-asl-version}</version>
</dependency>
<dependency>
<groupId>org.codehaus.jackson</groupId>
<artifactId>jackson-core-asl</artifactId>
<version>${jackson-core-asl-version}</version>
</dependency>
<!--配置 freemarker -->
<dependency>
<groupId>freemarker</groupId>
<artifactId>freemarker</artifactId>
<version>${freemarker-version}</version>
</dependency> <!-- jdbc driver -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>${mysql-connector-java-version}</version>
</dependency>
<!-- connect pool with c3p0 -->
<dependency>
<groupId>c3p0</groupId>
<artifactId>c3p0</artifactId>
<version>${c3p0-version}</version>
</dependency> <dependency>
<groupId>commons-fileupload</groupId>
<artifactId>commons-fileupload</artifactId>
<version>${commons-fileupload-version}</version>
</dependency> <dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>${org.apache.commons-version}</version>
</dependency> <dependency>
<groupId>commons-codec</groupId>
<artifactId>commons-codec</artifactId>
<version>${commons-codec-version}</version>
</dependency>
<!-- analyze xml use dom4j -->
<dependency>
<groupId>dom4j</groupId>
<artifactId>dom4j</artifactId>
<version>${dom4j-version}</version>
</dependency>
<!-- about servlet -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
<version>${javax.servlet-version}</version>
</dependency> <dependency>
<groupId>taglibs</groupId>
<artifactId>standard</artifactId>
<version>1.1.2</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.0.1</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>1.6.6</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency> <dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.6.6</version>
</dependency>
<dependency>
<groupId>javax.activation</groupId>
<artifactId>activation</artifactId>
<version>1.1.1</version>
</dependency> <!-- memcached -->
<dependency>
<groupId>com.google.code.maven-play-plugin.spy</groupId>
<artifactId>memcached</artifactId>
<version>2.4.2</version>
</dependency>
<dependency>
<groupId>cglib</groupId>
<artifactId>cglib</artifactId>
<version>${cglib-version}</version>
</dependency>
<dependency>
<groupId>net.fckeditor</groupId>
<artifactId>java-core</artifactId>
<version>2.6</version>
</dependency>
<dependency>
<groupId>org.json</groupId>
<artifactId>json</artifactId>
<version>20131018</version>
</dependency> <dependency>
<groupId>com.sun.jersey</groupId>
<artifactId>jersey-client</artifactId>
<version>1.18.1</version>
</dependency>
<dependency>
<groupId>jsptags</groupId>
<artifactId>pager-taglib</artifactId>
<version>2.0</version>
</dependency> <dependency>
<groupId>net.sourceforge.jexcelapi</groupId>
<artifactId>jxl</artifactId>
<version>2.6.10</version>
</dependency> <dependency>
<groupId>opensymphony</groupId>
<artifactId>oscache</artifactId>
<version>2.4.1</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>2.4</version>
<scope>provided</scope>
</dependency> <dependency>
<groupId>org.apache.struts</groupId>
<artifactId>struts2-core</artifactId>
<version>2.3.8</version>
<exclusions>
<exclusion>
<artifactId>javassist</artifactId>
<groupId>javassist</groupId>
</exclusion>
</exclusions>
</dependency> <!-- spring和struts之间的联系 -->
<dependency>
<groupId>org.apache.struts</groupId>
<artifactId>struts2-spring-plugin</artifactId>
<version>2.3.8</version>
</dependency> <!-- struts2的注解 -->
<dependency>
<groupId>org.apache.struts</groupId>
<artifactId>struts2-convention-plugin</artifactId>
<version>2.3.8</version>
</dependency> <dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>4.1.7.Final</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-ehcache</artifactId>
<version>4.1.7.Final</version>
</dependency>
<dependency>
<groupId>displaytag</groupId>
<artifactId>displaytag</artifactId>
<version>1.2</version>
</dependency> </dependencies> <build>
<finalName>ssh_xml</finalName>
</build> </project>

3:web.xml里面的dtd默认是2.3的,不过2.3的不支持el表达式,所以这里修改为2.5的。配置如下所示:

 <?xml version="1.0" encoding="UTF-8"?>
<web-app 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_2_5.xsd" version="2.5">
<display-name>Archetype Created Web Application</display-name> <display-name>Archetype Created Web Application</display-name> </web-app>

4:项目配置好上面的,接下来开始写代码,首先从web.xml的配置开始,比如struts2的核心过滤器,spring核心配置文件,监听器;

 <?xml version="1.0" encoding="UTF-8"?>
<web-app 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_2_5.xsd" version="2.5">
<display-name>Archetype Created Web Application</display-name> <display-name>Archetype Created Web Application</display-name> <!-- 配置spring的监听器 ,加载本地的springp配置文件-->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:bean-*.xml</param-value>
</context-param> <!--
spring的监听器 spring-web-3.2.4.RELEASE.jar ==》org.springframework
==》context ==》ContextLoaderListener
-->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener> <!--
struts2的核心过滤器
1:filter-class ==》 struts2-core-2.3.8.jar ==》org.apache.struts2
==》 dispatcher ==》 ng ==》filter ==》StrutsPrepareAndExecuteFilter
-->
<filter>
<filter-name>struts2Filter</filter-name>
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2Filter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping> </web-app>

5:配置好之后就需要配置一下maven标准格式:在src/main下面创建一个java的文件夹,在src/下面创建一个test,在test下面创建java文件夹,配置如下所示:

struts2+hibernate+spring配置版框架搭建以及简单测试(方便脑补)

6:简单测试一下,将项目发布到tomcat中,启动tomcat,运行之后tomcat不报错,说明项目正常;

7:将spring的配置文件bean-common.xml配置文件放到src/main/resources下面即可(src/main/resources也是根目录):配置如下所示:

 <?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:p="http://www.springframework.org/schema/p"
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.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd"> </beans>
 <?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:p="http://www.springframework.org/schema/p"
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.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd"> <!-- 1. 连接池实例 -->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="jdbcUrl" value="jdbc:mysql://localhost:3306/ssh"></property>
<property name="driverClass" value="com.mysql.jdbc.Driver"></property>
<property name="user" value="root"></property>
<property name="password" value="123456"></property>
<property name="initialPoolSize" value="3"></property>
<property name="maxPoolSize" value="6"></property>
</bean> <!-- 2. Spring管理SessionFactory 【全部配置都写到spring中】 -->
<!-- # 注入DataSource、 注入常用配置属性、映射配置属性 -->
<bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource"></property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
<prop key="hibernate.show_sql">true</prop>
<!-- <prop key="hibernate.hbm2ddl.auto">update</prop> -->
</props>
</property>
<property name="mappingLocations">
<list>
<value>classpath:com/bie/po/*.hbm.xml</value>
</list>
</property>
</bean> <!-- 3. 事务相关配置 -->
<!-- 3.1 事务管理器类 -->
<bean id="txManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean> <!-- 3.2 事务增强(如何管理事务)-->
<tx:advice id="txAdvice" transaction-manager="txManager">
<tx:attributes>
<tx:method name="get*" read-only="true"/>
<tx:method name="find*" read-only="true"/>
<tx:method name="select*" read-only="true"/>
<tx:method name="*" read-only="false"/>
</tx:attributes>
</tx:advice> <!-- 3.3 Aop配置 = 切入点表达式(拦截目标对象,生成代理) + 事务增强应用-->
<aop:config>
<aop:pointcut expression="execution(* com.bie.service.impl.*.*(..))" id="pointcut"/>
<aop:advisor advice-ref="txAdvice" pointcut-ref="pointcut"/>
</aop:config> <!-- ioc容器注入 dao层的注入-->
<bean id="adminDao" class="com.bie.dao.impl.AdminDaoImpl">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean> <!-- Service 需要注入 Dao -->
<bean id="adminService" class="com.bie.service.impl.AdminServiceImpl">
<property name="adminDao" ref="adminDao"></property>
</bean> <!-- Service 需要注入 Dao -->
<bean id="adminAction" class="com.bie.action.AdminAction">
<property name="adminService" ref="adminService"></property>
</bean> </beans>

8:将struts的配置文件struts.xml配置文件放到src/main/resources下面即可(src/main/resources也是根目录):配置如下所示:

 <?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
"http://struts.apache.org/dtds/struts-2.3.dtd"> <struts> </struts>
 <?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
"http://struts.apache.org/dtds/struts-2.3.dtd"> <struts> <package name="admin_package" extends="struts-default"> <!-- Ation实例交给spring容器创建 -->
<action name="select_*" class="adminAction" method="{1}"> <!-- 列表展示 -->
<result name="success">index.jsp</result> </action>
</package> </struts>

9:配置到第八步,基本结束,开始写代码,创建一个实体类Admin;如下所示:

 package com.bie.po;

 /**
* @author 作者:别先生
* @version 创建时间:2017年6月10日 下午2:08:17
* 类说明
*/ public class Admin { private Integer adminId;//管理员编号
private String adminName;//管理员姓名
private String adminPwd;//管理员密码 public Integer getAdminId() {
return adminId;
}
public void setAdminId(Integer adminId) {
this.adminId = adminId;
}
public String getAdminName() {
return adminName;
}
public void setAdminName(String adminName) {
this.adminName = adminName;
}
public String getAdminPwd() {
return adminPwd;
}
public void setAdminPwd(String adminPwd) {
this.adminPwd = adminPwd;
}
@Override
public String toString() {
return "Admin [adminId=" + adminId + ", adminName=" + adminName + ", adminPwd=" + adminPwd + "]";
} }

10:创建好实体类之后配置一个hibernate的映射文件;Admin.hbm.xml,代码如下所示:

 <?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"> <!-- hibernate的实体类映射文件的配置,package包指出总包所在 -->
<hibernate-mapping package="com.bie.po"> <!-- class的name是实体类的名字,table是数据表名 -->
<class name="Admin" table="admin">
<id name="adminId">
<!-- native设置自增 -->
<generator class="native"></generator>
</id> <!-- 非主键的映射 -->
<property name="adminName" length="20"></property>
<property name="adminPwd" length="20"></property>
</class> </hibernate-mapping>

11:创建好实体类之后开始创建dao层的接口和实现类,如AdminDao接口和AdminDaoImpl实现类;

 package com.bie.dao;

 import java.util.List;

 import com.bie.po.Admin;

 /**
* @author 作者:别先生
* @version 创建时间:2017年6月10日 下午2:25:01
* 类说明
*/ public interface AdminDao { /**
* 查询管理员的信息
* @param admin
* @return
*/
public List<Admin> selectAdmin();
}
 package com.bie.dao.impl;

 import java.util.List;

 import org.hibernate.SessionFactory;

 import com.bie.dao.AdminDao;
import com.bie.po.Admin; /**
* @author 作者:别先生
* @version 创建时间:2017年6月10日 下午2:25:15
* 类说明
*/ public class AdminDaoImpl implements AdminDao{ //IOC容器注入sessionFactory对象
private SessionFactory sessionFactory;
public void setSessionFactory(SessionFactory sessionFactory) {
this.sessionFactory = sessionFactory;
} public List<Admin> selectAdmin() { List<Admin> list = sessionFactory.getCurrentSession().createQuery(" from Admin ").list(); return list;
} }

12:创建好dao层接口和实现类之后开始创建service层的接口和实现类,如AdminService接口和AdminServiceImpl实现类;

 package com.bie.service;

 import java.util.List;

 import com.bie.po.Admin;

 /**
* @author 作者:别先生
* @version 创建时间:2017年6月10日 下午2:29:20
* 类说明
*/ public interface AdminService { public List<Admin> selectAdmin();
}
 package com.bie.service.impl;

 import java.util.List;

 import com.bie.dao.AdminDao;
import com.bie.po.Admin;
import com.bie.service.AdminService; /**
* @author 作者:别先生
* @version 创建时间:2017年6月10日 下午2:29:40
* 类说明
*/ public class AdminServiceImpl implements AdminService{ //ioc容器注入
private AdminDao adminDao;
public void setAdminDao(AdminDao adminDao) {
this.adminDao = adminDao;
} public List<Admin> selectAdmin(){ List<Admin> list = adminDao.selectAdmin(); return list;
}
}

13:当写好service层之后开始写action层AdminAction,这里传递前台页面的参数;代码如下所示:

 package com.bie.action;

 import java.util.List;
import java.util.Map; import org.apache.struts2.interceptor.RequestAware; import com.bie.po.Admin;
import com.bie.service.AdminService;
import com.opensymphony.xwork2.ActionSupport; /**
* @author 作者:别先生
* @version 创建时间:2017年6月10日 下午2:36:07
* 类说明
*/ public class AdminAction extends ActionSupport implements RequestAware{ private static final long serialVersionUID = 1L; //ioc容器注入
private AdminService adminService;
public void setAdminService(AdminService adminService) {
this.adminService = adminService;
} // 接收框架运行时候传入的代表request对象的map
private Map<String, Object> request;
public void setRequest(Map<String, Object> request) {
this.request = request;
} //ioc容器注入
private Admin admin;
public void setAdmin(Admin admin) {
this.admin = admin;
}
public Admin getAdmin() {
return admin;
} public String selectAdmin(){ List<Admin> list = adminService.selectAdmin(); //将信息保存到request中
request.put("list", list); return "success";
} }

14:当后台写完之后开始写前台测试页面:页面如下所示

 <%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>主页显示管理员的信息</title>
</head>
<body> <a href="select_selectAdmin.action">查询所有用户信息</a> <center>
<table cellspacing="20" cellpadding="20">
<tr>
<th>管理员编号</th>
<th>管理员姓名</th>
<th>管理员密码</th>
</tr>
<c:forEach items="${list }" var="list">
<tr>
<td>${list.adminId}</td>
<td>${list.adminName}</td>
<td>${list.adminPwd }</td>
</tr>
</c:forEach>
</table>
</center> </body>
</html>

直接访问index.jsp页面,测试结果如下所示:

struts2+hibernate+spring配置版框架搭建以及简单测试(方便脑补)

struts2+hibernate+spring配置版框架搭建以及简单测试(方便脑补)

ssh配置版框架搭建先到这吧,以后有机会再深学吧。时间过的挺快的,马上就该出去找工作了......

增删改查功能日后有时间再写吧......