s2sh整合实例 Struts2.0 Spring2.6 Hibernate3.2


Struts2与Spring2.6、Hibernate3.2三者整合的过程示例:

示例工具:MyEclipse 8.5、Tomcat 6.0、MySql

步骤:

1.创建一个WEB工程,命名为s2sh(名字自己取,此处为示例工程名);

2.导入struts2的核心jar包,也就是在WEB-INF下的lib文件夹中放入jar包文件
commons-fileupload.jar
commons-io.jar
freemarker.jar
javassist.jar
ognl-2.6.11.jar
struts2-core-2.0.14.jar
struts2-spring-plugin-2.0.14.jar
xwork-2.0.7.jar

3.添加Spring支持(项目右键MyEclipse Spring Capabilities …)  
           选择:  
    1) Spring version:Spring 2.5  
    2) Select the libraries to add the buildpath:  
    Spring 2.5 AOP Libraries  
    Spring 2.5 Core Libraries  
    Spring 2.5 Persistence Core Libraries  
    Spring 2.5 Persistence JDBC Libraries  
    Spring 2.5 Web Libraries  
           3) JAR Library Installation:  
    Copy checked Library contents to project folder (TLDs always copied)  
           Next  
    4) Spring配置文件选择目录Folder:WebRoot/WEB-INF  
    Finish  


4.添加Hibernate支持(项目右键MyEclipse Hibernate Capabilities …)  
           选择:  
           1)Hibernate Specification:Hibernate3.2  
           2)Select the libraries to ad to the buildpath:Hibernate 3.2 Core Libraries和Hibernage 3.2 Annoations  
    3)JAR Library Installation:Copy checked Library Jars to project folder and add to build-path  
    Next  
    4)选择:Spring configuration file(applicationContext.xml) Next  
           5)选择:Existing Spring configuration file  
    SessionFactory Id: sessionFactory Next  
           6)Bean Id:dataSource  
    DB Driver:选择配置好的Hibernate。Next  
           7)不建立SessionFactory(不选择Create SessionFactory class?)  
           8)Finish。  
	
	
5.创建struts.xml文件,在src包下新建一个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="struts.objectFactory" value="spring" />
	<package name="default" extends="struts-default">
		<action name="registerAction" class="RegisterAction">
			<result name="fail">/login.jsp </result>
			<result name="success">/success.jsp</result>
		</action>
	</package>

</struts>

6.配置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">
	<!-- Struts2 与 Spring 整合 -->
	<context-param>
		<param-name>contextConfigLocation</param-name>
		<param-value>
			/WEB-INF/applicationContext.xml
		</param-value>
	</context-param>

	<!-- 整合Struts2 -->
	<filter>
		<filter-name>Struts2.0</filter-name>
		<filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>
	</filter>
	<filter-mapping>
		<filter-name>Struts2.0</filter-name>
		<url-pattern>/*</url-pattern>
	</filter-mapping>

	<!-- 整合Spring与Struts2的类自动加载器 -->
	<listener>
		<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
	</listener>
	<welcome-file-list>
		<welcome-file>login.jsp</welcome-file>
	</welcome-file-list>
</web-app>


7.反向生成实体类文件

 1).准备数据库,假如名为s2sh,里面有一个User的表  
      
       
    2).在MyEclipse的右上角切换透视图,切换到MyEclipse Database Explorer  
       
    3).然后在最左边点击右键,选择New(也就是说新建一个数据库连接,Driver Name叫s1sh,方便下面解释,其它的跟平常用的一样),,配置好连接以后点击Next,进入下一个窗体,   选择第三个单选按钮(Display the selected Schemas),然后点击右边的Add按钮,也就是说添加你的数据库,然后点击完成按钮,这样你的数据库连接就创建好了;  
      
      
    在你刚才创建的s1sh上右键,选择Open connection,   一直展开到s2sh下面的TABLE,展开TABLE,在我们创建的User表上点击右键,选择Hibernate Reverse Engineering(这个就是反向生成),出来一个面板   在Java src folder 选择这个工程的src目录,然后在Java Package上选择刚才我们在工程里面创建好的bean包,接下来就是勾中前三个复选框就可以了,点击Next,又会出现一个面板,这个面板可以不用管,然后点击Next,这个面板也不用管,点击Finish,然后就会生成实体类和映射文件了。
	
8.创建Action

在src包下新建一个包,名为"com.beyondsoft.action"(名字仅为示例,只写一个com也可以,示例写法是采取struts中的action包名写法),在该包下新建一个action类RegisterAction.java,其内容如下:

package com.beyondsoft.action;

import java.util.List;

import com.beyondsoft.bean.User;
import com.beyondsoft.bean.UserDAO;
import com.opensymphony.xwork2.ActionSupport;

public class RegisterAction extends ActionSupport {

	private static final long serialVersionUID = 1L;
	private String userName;
	private String password;
	private UserDAO userDAO;
	
	public UserDAO getUserDAO() {
		return userDAO;
	}

	public void setUserDAO(UserDAO userDAO) {
		this.userDAO = userDAO;
	}

	public String getUserName() {
		return userName;
	}

	public void setUserName(String userName) {
		this.userName = userName;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public String execute() {
		userDAO.save(new User(userName,password));
		return "success";
	}
}

10.创建页面,创建登录页面login.jsp,内容如下:

<%@ page language="java" pageEncoding="gb2312"%>

<%@taglib uri="/struts-tags" prefix="s"%>

<html>
	<head>
		<title>用户注册</title>
	</head>
	<body>
		<center>
			<div id="register">
				<s:form action="registerAction">
					<s:textfield label="用户名" name="userName"></s:textfield>
					<s:password label="密码" name="password"></s:password>
					<s:submit value="注册"></s:submit>
				</s:form>
			</div>

		</center>
	</body>
</html>

页面中,先导入struts2标签库,用来创建form表单。form表单提交给与登录相关的RegisterAction.java文件,而RegisterAction.java在struts.xml文件中相应的name值为"registerAction”,所以这里的action值为"loginAction"。至于用户名文本框与密码文本框的name值则与RegisterAction.java文件中的userName与password完全匹配。

11.配置applicationContext.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"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
	
	<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
		<property name="driverClassName" value="com.mysql.jdbc.Driver">
		</property>
		<property name="url" value="jdbc:mysql://localhost:3306/s2sh"></property>
		<property name="username" value="root"></property>
		<property name="password" value="root"></property>
	</bean>
	<bean id="sessionFactory"
		class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
		<property name="dataSource">
			<ref bean="dataSource" />
		</property>
		<property name="hibernateProperties">
			<props>
				<prop key="hibernate.dialect">
					org.hibernate.dialect.MySQLDialect
				</prop>
			</props>
		</property>
		<property name="mappingResources">
			<list>
				<value>com/beyondsoft/bean/User.hbm.xml</value>
			</list>
		</property>
	</bean>

	<bean id="RegisterAction" class="com.beyondsoft.action.RegisterAction">
		<property name="userDAO">
			<ref bean="UserDAO" />
		</property>
	</bean>

	<bean id="UserDAO" class="com.beyondsoft.bean.UserDAO">
		<property name="sessionFactory">
			<ref bean="sessionFactory" />
		</property>
	</bean>
	
</beans>
 
删除sam-2.2.3.jar  cglib-2.1.3.jar 运行
这样一个最简单的Struts2 Spring2.6 Hibernate3.2的实例就出来了,如果你要实现更多的功能可以在这个的基础上面再加。  


你可能感兴趣的:(spring,bean,Hibernate,MyEclipse,struts)