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