struts1.3 + spring + hibernate 全手工整合教程
- 博客分类:
- Java Web
struts1.3 + spring + hibernate 全手工整合教程
项目整体机构图:
1 准备相关的jar 包 (struts1.3 ,spring ,hibernate sqlserver 驱动包 )
1.1 struts1.3
1.2 spring
1.3 hibernate
1.4 sqljdbc4.0.jar
2. 代码展示
2.1 hibernate.cfg.xml
<?xml version='1.0' encoding='UTF-8'?> <!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd"> <!-- Generated by MyEclipse Hibernate Tools. --> <hibernate-configuration> <session-factory> <property name="dialect"> org.hibernate.dialect.SQLServerDialect </property> <property name="connection.url"> jdbc:sqlserver://localhost:1433;databaseName=test </property> <property name="connection.username">sa</property> <property name="connection.password">rao520zhou</property> <property name="connection.driver_class"> com.microsoft.sqlserver.jdbc.SQLServerDriver </property> <property name="myeclipse.connection.profile"> sqlserver </property> <mapping resource="com/svse/entity/TUser.hbm.xml" /> </session-factory> </hibernate-configuration>
2.2 实体类 TUser .java
package com.svse.entity; public class TUser implements java.io.Serializable { // Fields private Integer uid; private String uname; private String usex; private Integer uage; // Constructors /** default constructor */ public TUser() { } /** full constructor */ public TUser(String uname, String usex, Integer uage) { this.uname = uname; this.usex = usex; this.uage = uage; } // Property accessors public Integer getUid() { return this.uid; } public void setUid(Integer uid) { this.uid = uid; } public String getUname() { return this.uname; } public void setUname(String uname) { this.uname = uname; } public String getUsex() { return this.usex; } public void setUsex(String usex) { this.usex = usex; } public Integer getUage() { return this.uage; } public void setUage(Integer uage) { this.uage = uage; } }
和 映射文件 TUser.hbm.xml
<?xml version="1.0" encoding="utf-8"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> <!-- Mapping file autogenerated by MyEclipse Persistence Tools --> <hibernate-mapping> <class name="com.svse.entity.TUser" table="t_user" schema="dbo" catalog="test"> <id name="uid" type="java.lang.Integer"> <column name="uid" /> <generator class="native" /> </id> <property name="uname" type="java.lang.String"> <column name="uname" length="20" /> </property> <property name="usex" type="java.lang.String"> <column name="usex" length="10" /> </property> <property name="uage" type="java.lang.Integer"> <column name="uage" /> </property> </class> </hibernate-mapping>
2.3 接口 UserService.java
package com.svse.service; import java.util.List; import com.svse.entity.TUser; public interface UserService { /** * 添加 */ public void addUser(TUser user); /** * 查看所有 */ public List getAll(); /** * 删除 */ public void delUser(int uid); /** * 查看一个 */ public TUser getOneById(int uid); /** * 修改 */ public void motify(TUser user); }
2.4 DAO层 TUserDAO.java
package com.svse.dao; import java.util.List; import org.springframework.orm.hibernate3.support.HibernateDaoSupport; import com.svse.entity.TUser; public class TUserDAO extends HibernateDaoSupport { /** * 添加 */ public void add(TUser user) { this.getHibernateTemplate().save(user); } /** * 删除 */ public void delete(int uid) { this.getHibernateTemplate().delete(this.getOneById(uid)); } /** * 查找所有 */ public List getAll() { return this.getHibernateTemplate().loadAll(TUser.class); } /** * 查找一个 */ public TUser getOneById(int uid) { return (TUser) this.getHibernateTemplate().get(TUser.class, uid); } /** * 修改 */ public void update(TUser user) { this.getHibernateTemplate().update(user); } }
2.5 实现 接口 impl 层
package com.svse.impl; import java.util.List; import com.svse.dao.TUserDAO; import com.svse.entity.TUser; import com.svse.service.UserService; public class UserServiceImpl implements UserService { private TUserDAO userDao = null; public TUserDAO getUserDao() { return userDao; } public void setUserDao(TUserDAO userDao) { this.userDao = userDao; } /** * 添加 */ public void addUser(TUser user) { this.userDao.add(user); } /** * 删除 */ public void delUser(int uid) { this.userDao.delete(uid); } /** * 查找所有 */ public List getAll() { return this.userDao.getAll(); } /** * 查找一个 */ public TUser getOneById(int uid) { return this.userDao.getOneById(uid); } /** * 修改 */ public void motify(TUser user) { this.userDao.update(user); } }
2.6 Form类 UserForm.java
/* * Generated by MyEclipse Struts * Template path: templates/java/JavaClass.vtl */ package com.svse.struts.form; import org.apache.struts.action.ActionForm; import com.svse.entity.TUser; public class UserForm extends ActionForm { private TUser user = new TUser(); public TUser getUser() { return user; } public void setUser(TUser user) { this.user = user; } }
2.7 action 层 UserAction.java
package com.svse.struts.action; import java.util.List; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.apache.struts.action.ActionForm; import org.apache.struts.action.ActionForward; import org.apache.struts.action.ActionMapping; import org.apache.struts.actions.DispatchAction; import com.svse.service.UserService; import com.svse.struts.form.UserForm; /** * MyEclipse Struts * Creation date: 03-20-2011 * * XDoclet definition: * @struts.action path="/user" name="userForm" input="/user/adduser.jsp" parameter="method" scope="request" validate="true" * @struts.action-forward name="update" path="/user/updateUser.jsp" * @struts.action-forward name="all" path="/user/alluser.jsp" */ public class UserAction extends DispatchAction { private UserService userService = null; public UserService getUserService() { return userService; } public void setUserService(UserService userService) { this.userService = userService; } /** * 增加 * @param mapping * @param form * @param request * @param response * @return */ public ActionForward add(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) { UserForm userForm = (UserForm) form; this.userService.addUser(userForm.getUser()); return this.all(mapping, userForm, request, response); } /** * 删除 * @param mapping * @param form * @param request * @param response * @return */ public ActionForward del(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) { UserForm userForm = (UserForm) form; /*userForm.setUser(this.userService.getOneById(Integer.parseInt(request.getParameter("uid")))); this.userService.delUser(userForm.getUser());*/ this.userService.delUser(Integer.parseInt(request.getParameter("uid"))); return this.all(mapping, userForm, request, response); } /** * 查看所有 * @param mapping * @param form * @param request * @param response * @return */ public ActionForward all(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) { List list = this.userService.getAll(); request.setAttribute("list", list); return mapping.findForward("all"); } /** * 查看一个 * @param mapping * @param form * @param request * @param response * @return */ public ActionForward one(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) { UserForm userForm = (UserForm) form; userForm.setUser(this.userService.getOneById(Integer.parseInt(request.getParameter("uid")))); return mapping.findForward("update"); } /** * 修改 */ public ActionForward upp(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) { UserForm userForm = (UserForm) form; this.userService.motify(userForm.getUser()); return this.all(mapping, userForm, request, response); } }
2.7 web.xml
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="2.5" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"> <servlet> <servlet-name>action</servlet-name> <servlet-class>org.apache.struts.action.ActionServlet</servlet-class> <init-param> <param-name>config</param-name> <param-value>/WEB-INF/struts-config.xml</param-value> </init-param> <init-param> <param-name>debug</param-name> <param-value>3</param-value> </init-param> <init-param> <param-name>detail</param-name> <param-value>3</param-value> </init-param> <load-on-startup>0</load-on-startup> </servlet> <servlet-mapping> <servlet-name>action</servlet-name> <url-pattern>*.do</url-pattern> </servlet-mapping> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:applicationContext-*.xml</param-value> </context-param> <filter> <filter-name>CharacterEncodingFilter</filter-name> <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class> <init-param> <param-name>encoding</param-name> <param-value>UTF-8</param-value> </init-param> </filter> <filter-mapping> <filter-name>CharacterEncodingFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> </web-app>
2.8 struts-config.xml
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE struts-config PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 1.2//EN" "http://struts.apache.org/dtds/struts-config_1_2.dtd"> <struts-config> <data-sources /> <form-beans > <form-bean name="userForm" type="com.svse.struts.form.UserForm" /> </form-beans> <global-exceptions /> <global-forwards /> <action-mappings > <action attribute="userForm" input="/user/adduser.jsp" name="userForm" parameter="method" path="/user" scope="request" type="org.springframework.web.struts.DelegatingActionProxy"> <set-property property="cancellable" value="true" /> <forward name="update" path="/user/updateUser.jsp" /> <forward name="all" path="/user/alluser.jsp" /> </action> </action-mappings> <message-resources parameter="com.svse.struts.ApplicationResources" /> </struts-config>
2.9 applicationContext-beans.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" xmlns:p="http://www.springframework.org/schema/p" 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-2.5.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd "> <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"> <property name="configLocation" value="classpath:hibernate.cfg.xml"> </property> </bean> <bean id="TUserDAO" class="com.svse.dao.TUserDAO"> <property name="sessionFactory"> <ref bean="sessionFactory" /> </property> </bean> <bean id="userServiceImpl" class="com.svse.impl.UserServiceImpl"> <property name="userDao" ref="TUserDAO"></property> </bean> </beans>
2.10 applicationContext-commons.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" xmlns:p="http://www.springframework.org/schema/p" 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-2.5.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd "> <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"> <property name="configLocation" value="classpath:hibernate.cfg.xml"> </property> </bean> <!-- 配置事务管理器 --> <bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager"> <property name="sessionFactory" ref="sessionFactory"></property> </bean> <!-- 配置事物传播特性 --> <tx:advice id="txAdvice" transaction-manager="transactionManager"> <tx:attributes> <tx:method name="add*" propagation="REQUIRED" /> <tx:method name="del*" propagation="REQUIRED" /> <tx:method name="update*" propagation="REQUIRED" /> <tx:method name="motify*" propagation="REQUIRED" /> <tx:method name="*" read-only="true" /> </tx:attributes> </tx:advice> <!-- 配置哪些共有的需在参与事务的类与方法 --> <aop:config> <aop:pointcut id="allMethods" expression="execution(* com.svse.impl.*.*(..))" /> <aop:advisor advice-ref="txAdvice" pointcut-ref="allMethods" /> </aop:config> </beans>
2.11 applicationContext-actions.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" xmlns:p="http://www.springframework.org/schema/p" 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-2.5.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd "> <bean name="/user" class="com.svse.struts.action.UserAction"> <property name="userService" ref="userServiceImpl"></property> </bean> </beans>
2.12 log4j.properties 日志文件
log4j.rootLogger=INFO, stdout, logfile log4j.appender.stdout=org.apache.log4j.ConsoleAppender log4j.appender.stdout.layout=org.apache.log4j.PatternLayout log4j.appender.stdout.layout.ConversionPattern=%d %p [%c] - %m%n
2.13 com.svse.struts 包中 ApplicationResources.properties 文件为空文件,可以不要,如果没有这个文件,那么需要把 struts-config.xml 中 <message-resources parameter="com.svse.struts.ApplicationResources" /> 去掉。
2.14 index.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> <base href="<%=basePath%>"> <title>My JSP 'index.jsp' starting page</title> <meta http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="expires" content="0"> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> <meta http-equiv="description" content="This is my page"> <!-- <link rel="stylesheet" type="text/css" href="styles.css"> --> </head> <body> <a href="${pageContext.request.contextPath }/user.do?method=all">用户管理</a> </body> </html>
2.15 adduser.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> <head> <title>add</title> <style type="text/css"> body { text-align: center; } table { border-collapse: collapse; width: 60%; text-align: center; } table tr td { font-size: 12px; text-align: center; border: solid 1px; line-height: 25px; } </style> </head> <body> <html:form action="/user.do?method=add"> <table> <tr> <td> 姓名: </td> <td> <html:text property="user.uname"></html:text> </td> </tr> <tr> <td> 性别: </td> <td> <html:text property="user.usex"></html:text> </td> </tr> <tr> <td> 年龄: </td> <td> <html:text property="user.uage"></html:text> </td> </tr> <tr> <td colspan="10"> <html:submit value="确定" /> </td> </tr> </table> </html:form> </body> </html>
2.16 alluser.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"%> <%@ taglib uri="http://struts.apache.org/tags-logic" prefix="logic"%> <%@ taglib uri="http://struts.apache.org/tags-tiles" prefix="tiles"%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html:html lang="true"> <head> <html:base /> <title>alluser.jsp</title> <meta http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="expires" content="0"> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> <meta http-equiv="description" content="This is my page"> <!-- <link rel="stylesheet" type="text/css" href="styles.css"> --> <style type="text/css"> body { text-align: center; } table { border-collapse: collapse; width: 60%; text-align: center; } table tr td { font-size: 12px; text-align: center; border: solid 1px; line-height: 25px; } </style> </head> <body> <table> <tr> <td> 编号 </td> <td> 姓名 </td> <td> 性别 </td> <td> 年龄 </td> <td> 修改 </td> <td> 删除 </td> </tr> <logic:iterate id="x" name="list"> <tr> <td> ${x.uid } </td> <td> ${x.uname } </td> <td> ${x.usex } </td> <td> ${x.uage } </td> <td> <a href="${pageContext.request.contextPath }/user.do?method=one&uid=${x.uid }">修改</a> </td> <td> <a href="${pageContext.request.contextPath }/user.do?method=del&uid=${x.uid }">删除</a> </td> </tr> </logic:iterate> <tr> <td colspan="10"> <a href="${pageContext.request.contextPath }/user/adduser.jsp">增加</a> </td> </tr> </table> </body> </html:html>
2.17 updateUser.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> <head> <title>update</title> <style type="text/css"> body { text-align: center; } table { border-collapse: collapse; width: 60%; text-align: center; } table tr td { font-size: 12px; text-align: center; border: solid 1px; line-height: 25px; } </style> </head> <body> <html:form action="/user.do?method=upp"> <table> <tr> <td> 姓名: </td> <td> <html:text property="user.uname"></html:text> </td> </tr> <tr> <td> 性别: </td> <td> <html:text property="user.usex"></html:text> </td> </tr> <tr> <td> 年龄: </td> <td> <html:text property="user.uage"></html:text> </td> </tr> <html:hidden property="user.uid" /> <tr> <td colspan="10"> <html:submit value="确定" /> </td> </tr> </table> </html:form> </body> </html>
2.18 test.sql 脚本
create database test go use test go create table t_user ( uid int primary key identity(1,1), uname varchar(20), usex varchar(10), uage int ) go select * from t_user
希望对你有帮助,这是本人用Eclipse 手动配置,经测试可成功运行,如有问题可与本人沟通,联系QQ:76198830
相关推荐
在深入探讨Struts2.1.6+Spring2.5.6+Hibernate3.3.1全注解的实现细节之前,我们先回顾一下这三个框架的基本概念及其在JavaEE企业级开发中的作用。 ### Struts2框架 Struts2是一个开源的Web应用框架,它遵循MVC...
Spring还提供了对DAO(Data Access Object)的支持,与各种持久层框架(如JDBC、Hibernate、MyBatis)良好集成,以及对Web框架(如Struts2)的整合。 **Struts2** Struts2是一个基于MVC(Model-View-Controller)...
使用Struts、Spring和Hibernate这三大框架来开发物资管理系统,可以实现高效的MVC模式,Spring负责业务逻辑的管理,Hibernate则作为持久层框架,负责数据的CRUD操作,而Struts则作为表现层框架,协调控制整个应用...
《Java Web开发技术大全:JSP+Servlet+Struts+Hibernate+Spring+Ajax》特别介绍了Struts 2对AjAX的支持,还重点剖析了SSH框架的整合开发,并给出了两个综合案例来展示整合SSH框架开发Web应用。 和已经出版的同类图书...
### Struts2 SSH手工配置全过程及登录示例详解 #### 一、背景介绍 在Java Web开发领域,Struts2框架以其简洁、易用而受到广泛欢迎。而在实际项目开发中,为了更好地实现业务逻辑与界面展示的分离,通常会采用SSH...
6. **集成主流应用框架**:Spring框架对于如Struts、Hibernate等主流的应用框架提供了良好的集成支持,使得基于Spring的应用开发变得更加便捷高效。 三、Spring框架的模块组成 Spring框架由多个模块组成,每个模块...
通过丰富的实战案例与详细的步骤指导,使读者能够迅速掌握 MyEclipse 的核心功能,并学会如何构建基于 Struts2+Spring2+Hibernate3 的企业级应用。 #### 二、系统需求与安装配置 **1.1 系统需求** - **操作系统:*...
1. **主流技术栈**:采用Struts2、Spring3、Hibernate4、EasyUI1.3及SpringJDBC作为主要技术组件,易于学习和上手,且具备良好的扩展性,支持二次开发。 2. **代码生成器**:能够生成符合统一风格的前端页面和后端...
本教程聚焦于MyEclipse 6这一集成开发环境(IDE),特别强调了最新开源架构——Struts2、Spring2和Hibernate3的运用,以及通过精选实战案例和全程视频讲解来加深理解。以下是对该教程核心知识点的详细解析: ### 第一...
JEECG(Java EE Code Generator)是一个开源的基于代码生成器的智能开发框架,它利用现代Web UI框架(如EasyUi、JqueryUI、Ext、Dwz等)日益成熟和界面的统一化趋势,提出了代码自动生成加手工MERGE的半智能开发模式...
- **配置环境**:设置Spring、Struts和Hibernate所需的库文件和配置文件。 **15.2 开发综合应用** - **编写Action类**:实现Struts Action。 - **配置Struts环境**:定义Action映射。 - **编写Hibernate实体类**:...
本文主要围绕一个基于Java的酒店管理系统进行讨论,该系统采用Struts(S)、Hibernate(H)和Spring(S)的SSH框架进行开发,结合JSP进行前端展示,并利用MySQL作为数据库存储数据。系统的架构为B/S(Browser/Server...
该系统主要利用了Spring、Struts2和Hibernate框架来设计并实现自动化的考试座位编排功能,从而替代了传统的人工排座方式。系统的设计和实现可以有效避免人工操作中可能出现的错误,减轻教务人员的工作负担,并且提升...
本系统采用SSH(Struts2、Spring、Hibernate)框架进行开发,这是一种常见的Java Web应用程序架构,具有良好的分层设计和松耦合特性,可以提高开发效率和系统的可维护性。Struts2作为MVC(模型-视图-控制器)框架...
使用Java编程语言,结合Spring、Struts2和Hibernate(SSH框架)实现业务逻辑和数据处理。 4.2 前端界面设计 利用HTML、CSS和JavaScript创建用户界面,提供直观、便捷的操作体验。 4.3 测试 系统开发完成后,进行...
SSH是Struts、Hibernate和Spring三个开源框架的首字母缩写,它们共同构建了一个强大的Java Web开发框架。Struts提供了MVC(Model-View-Controller)架构,用于处理用户的请求和展示数据;Hibernate则是一个对象关系...
- **Spring+Struts+Hibernate** 这些章节将分别介绍如何使用这些流行框架进行开发,每种框架都有其独特的优势和应用场景。例如,Spring框架因其轻量级和灵活性而被广泛应用于企业级应用开发中;Hibernate则以其强大...
SSH框架在此类系统中扮演着核心角色,它由Spring、Struts和Hibernate三个开源组件组成,为系统开发提供了一个强大而灵活的基础。 一、系统概述 1.1 目的 人力资源管理系统的主要目的是集成和自动化人力资源相关的...