[转载]Struts+Spring+Hibernate--SSH整合实例
开发工具:MyEclipse+MySQL+Tomcat
说明:本实例是简单注册程序(只有两个属性)
数据库脚本:user.sql
DROP TABLE IF EXISTS `user`; CREATE TABLE `user` ( `Id` int(11) NOT NULL AUTO_INCREMENT, `username` varchar(255) DEFAULT NULL, `password` varchar(255) DEFAULT NULL, PRIMARY KEY (`Id`) ) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8; |
开发步骤:
一
1建立Web项目
2添加Struts支持(项目右键àMyEclipse Struts Capabilities …)
1)Struts specification: Struts 1.3
2)Base package for 那位classes:edu.syict.struts
3添加Spring支持(项目右键àMyEclipse Spring Capabilities …)
1) Spring version:Spring 2.0
2) Select the libraries to add the buildpath:
Spring 2.0 AOP Libraries
Spring 2.0 Core Libraries
Spring 2.0 Persistence Core Libraries
Spring 2.0 Persistence JDBC Libraries
Spring 2.0 Web Libraries
Copy checked Library contents to project folder (TLDs always copied)
4) Spring配置文件选择目录Folder:WebRoot/WEB-INF
Finish
4 添加Hibernate支持(项目右键àMyEclipse Hibernate Capabilities …)
3)JAR Library Installation:Copy checked Library Jars to project folder and add to build-path
Next
4)选择:Spring configuration file(applicationContext.xml) Next
SessionFactory Id: sessionFactory àNext
DB Driver:选择配置好的Hibernate。àNext
二 数据库方面
Hibernate反向工程:
Create POJO<>DB Table mapping information复选框
Id Generator:native àFinish
package edu.syict.dao; import edu.syict.pojo.User; public interface UserDao {
} |
package edu.syict.dao; import org.springframework.orm.hibernate3.support.HibernateDaoSupport; import edu.syict.pojo.User; public class UserDaoImpl extends HibernateDaoSupport implements UserDao {
} |
三 建立JSP 页面,Action类
3) Action 默认就可以了 àFinish
<plug-in className="org.springframework.web.struts.ContextLoaderPlugIn">
</plug-in> |
b) 将Action类移交(message-resources标记上)
<controller
processorClass="org.springframework.web.struts.DelegatingRequestProcess |
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE struts-config PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 1.3//EN" "http://struts.apache.org/dtds/struts-config_1_3.dtd"> <struts-config> <form-beans >
</form-beans> <global-exceptions /> <global-forwards /> <action-mappings >
</action-mappings> <controller
processorClass="org.springframework.web.struts.DelegatingRequestProcess <message-resources parameter="edu.syict.struts.ApplicationResources" /> <plug-in className="org.springframework.web.struts.ContextLoaderPlugIn">
</plug-in> </struts-config> |
5) register.jsp页面(WebRoot/form/register.jsp)
<%@ page language="java" pageEncoding="UTF-8"%> <%@ taglib uri="http://struts.apache.org/tags-bean" prefix="bean"%> <%@ taglib uri="http://struts.apache.org/tags-html" prefix="html"%> <html>
</html> |
6)修改RegisterAction.java
package edu.syict.struts.action; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.apache.struts.action.Action; import org.apache.struts.action.ActionForm; import org.apache.struts.action.ActionForward; import org.apache.struts.action.ActionMapping; import org.apache.struts.action.DynaActionForm; import edu.syict.dao.UserDao; import edu.syict.pojo.User; public class RegisterAction extends Action {
} |
四 修改Spring配置文件applicationContext.xml
右键àSpringàNew Bean:
右键àSpringàNew Bean:
applicationContext.xml全部
<?xml version="1.0" encoding="UTF-8"?> <beans
|
</beans> |
五 修改web .xml文件
web.xml
<filter>
</filter> <context-param>
</context-param> <filter-mapping>
</filter-mapping> <listener>
</listener> <!-- Spring提供的过滤器_End --> <!-- 欢迎页面 -->
</welcome-file-list> </web-app> |