Myeclipse10整合ssh2(struts2.1+spring3.1+hibernate4.1)+零注解零配置

时间:2022-06-23 04:59:11

【前言】

说来惭愧,使用ssh2很长时间了,但是还是第一次利用myeclipse整合ss2,就是搭建ssh2框架,

另外根据struts2的约定,实现了正真的零配置,没有使用注解。


【参考文章】

网上浏览了好多文章,帮助最大的有:

ssh2整合:http://xly3000.blog.163.com/blog/static/1324247201231163828803/

struts2约定:http://xiaoxuejie.iteye.com/blog/1563944


【准备】

1.Myeclipse10

2.数据库(本文使用firebird):http://www.firebirdsql.org/


【ssh2整合】

1.创建项目

新建一个Web Project(File--New--Web Project),Project Name为ssh2,选择Java EE 6.0,如图:

Myeclipse10整合ssh2(struts2.1+spring3.1+hibernate4.1)+零注解零配置

目录结构:

Myeclipse10整合ssh2(struts2.1+spring3.1+hibernate4.1)+零注解零配置

2.添加Spring3.1

右键项目ssh2--Myeclipse--Add Spring Capabilities,选择Spring3.1,选择core,persistence和web三个jar,选择copy到lib folder,如图:

Myeclipse10整合ssh2(struts2.1+spring3.1+hibernate4.1)+零注解零配置

点击next,

去掉aop的勾,将xml文件路径选为webroot/web-inf下,如图:

Myeclipse10整合ssh2(struts2.1+spring3.1+hibernate4.1)+零注解零配置

点击Finish

3.添加Hibernate4.1

同上,Add Hibernate Capabilities,选择Hibernate4.1,选择core和advanced的两个jar,选择copy到lib folder下,如图:

Myeclipse10整合ssh2(struts2.1+spring3.1+hibernate4.1)+零注解零配置

点击Next,选择spring的配置文件,如图:

Myeclipse10整合ssh2(struts2.1+spring3.1+hibernate4.1)+零注解零配置

点击Next,选择已存在的spring配置文件,如图:

Myeclipse10整合ssh2(struts2.1+spring3.1+hibernate4.1)+零注解零配置

点击Next,去掉创建datasource的选项,如图:

Myeclipse10整合ssh2(struts2.1+spring3.1+hibernate4.1)+零注解零配置

点击Nexr,去掉创建SessionFactory的选项,如图:

Myeclipse10整合ssh2(struts2.1+spring3.1+hibernate4.1)+零注解零配置

点击Finish。

4.添加Struts2.1

同上,Add Struts Capabilities,选择Struts2.1和/*匹配,如图:

Myeclipse10整合ssh2(struts2.1+spring3.1+hibernate4.1)+零注解零配置

点击Next,选择core的jar,点击Finish。

5.修改applicationContext.xml

由于之前没有配置数据源和设置SessionFactory,所以这里要修改一下spring的配置文件,修改成下面这样,

当然也可以有其他形式,具体参见http://hi.baidu.com/wolf55/item/528315092fab6834a3332a8f

<?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"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd">


	<bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
		<property name="configLocation">
			<value>classpath:hibernate.cfg.xml</value>
		</property>
	</bean>
</beans>

6.添加hibernate.cfg.xml文件到src下,以FireBird为例,代码:

<?xml version='1.0' encoding='UTF-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
          "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
          "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<!-- Generated by MyEclipse Hibernate Tools.                   -->
<hibernate-configuration>
    <session-factory>
    	<!-- jdbc for firebird -->
    	<property name="connection.driver_class">org.firebirdsql.jdbc.FBDriver</property>
    	<property name="connection.url">jdbc:firebirdsql:localhost:E:\Documents\FireBird\test.fdb</property>
    	<property name="connection.username">uikoo9</property>
    	<property name="connection.password">uikoo7</property>   
    	<!-- dialect for firebird --> 	
    	<property name="dialect">org.hibernate.dialect.FirebirdDialect</property>
    	
    	<!-- 打印SQL -->
    	<property name="show_sql">true</property>
    	
    	<!-- 创建表 -->
    	<property name="hbm2ddl.auto">update</property>
    	
    	<!-- 线程 -->
    	<property name="current_session_context_class">thread</property>
    	
		<property name="javax.persistence.validation.mode">none</property>
    	
    	<!-- 映射实体类 -->
		<mapping class="com.uikoo9.bean.Cat"/>
    </session-factory>
</hibernate-configuration>

7.添加数据库jdbc包,这里以FireBird为例,添加jaybird-full-2.2.1.jar

8.部署项目到Myeclipse自带的Tomcat上,如图:

Myeclipse10整合ssh2(struts2.1+spring3.1+hibernate4.1)+零注解零配置

run server,正常启动。

至此ssh2的Myeclipse整合就实现了,整个项目的目录结构:

Myeclipse10整合ssh2(struts2.1+spring3.1+hibernate4.1)+零注解零配置


【零注解实现Struts2零配置】

1.src下新建代码,目录结构如下:

Myeclipse10整合ssh2(struts2.1+spring3.1+hibernate4.1)+零注解零配置

其中UserAction.java的代码:

package com.uikoo9.action;

public class UserAction {
	public String execute(){
		return "success";
	}
}

2.在webroot/web-inf下新建文件夹content,在content下新建user-success.jsp,代码:

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <title>零注解实现Struts2零配置</title>
  </head>
  
  <body>
	这是user-success.jsp页面!!
  </body>
</html>

3.重新部署到tomcat之后run server,访问:http://localhost:8080/ssh2/user.action,页面如下:

Myeclipse10整合ssh2(struts2.1+spring3.1+hibernate4.1)+零注解零配置

ok,零配置讲的有点粗略,实现原理和其他内容参考上面的文章。