struts2.2+hibernate3.2+spring2整合入门实例

环境:windows 7+MyEclipse 8.6

服务器: Tomcat 6.0

框架版本:struts2.2+hibernate3.2+spring 2.2

 

项目目录结构:

struts2.2+hibernate3.2+spring2整合入门实例

 

步骤一,添加框架支持的包:

struts2.2+hibernate3.2+spring2整合入门实例

struts2.2+hibernate3.2+spring2整合入门实例

struts2.2+hibernate3.2+spring2整合入门实例

步骤二:编写jsp页面:index.jsp:

 

Html代码 
  1. <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>  
  2. <%@ taglib prefix="s" uri="/struts-tags" %>  
  3. <%  
  4. String path = request.getContextPath();  
  5. String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";  
  6. %>  
  7.   
  8. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">  
  9. <html>  
  10.   <head>  
  11.     <base href="<%=basePath%>">  
  12.       
  13.     <title>Login</title>  
  14.       
  15.   </head>   
  16.   <body>  
  17.   <h2 style="color: red">Login</h2>    
  18.    <s:form action="login" namespace="/user">  
  19.           
  20.         <s:textfield name="user.username" label="%{getText('username')}"> </s:textfield>  
  21.           
  22.         <s:password name="user.password" label="%{getText('password')}"></s:password>  
  23.           
  24.         <s:submit name="submit" value="%{getText('submit')}"></s:submit>  
  25.           
  26.    </s:form>  
  27.   </body>  
  28. </html>  
 

result.jsp:

<body>

   Login success!

 </body>

步骤三:编写web.xml文件:

Xml代码 
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <web-app version="2.5"   
  3.     xmlns="http://java.sun.com/xml/ns/javaee"   
  4.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"   
  5.     xsi:schemaLocation="http://java.sun.com/xml/ns/javaee   
  6.     http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">  
  7.     
  8.   <welcome-file-list>  
  9.     <welcome-file>index.jsp</welcome-file>  
  10.   </welcome-file-list>  
  11.     
  12.   <filter>  
  13.     <filter-name>struts2</filter-name>  
  14.     <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>  
  15.   </filter>  
  16.   <filter-mapping>  
  17.     <filter-name>struts2</filter-name>  
  18.     <url-pattern>/*</url-pattern>  
  19.   </filter-mapping>  
  20.     
  21.   <listener>  
  22.         <listener-class>          
  23.         org.springframework.web.context.ContextLoaderListener        
  24.         </listener-class>  
  25.    </listener>  
  26.     
  27. </web-app>  

 步骤四:建数据库,(本例用MySQL),编写映射文件User.hbm.xml:

Xml代码 
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <!DOCTYPE hibernate-mapping  
  3. PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"  
  4. "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">  
  5.   
  6. <hibernate-mapping>  
  7.     <class name="com.gufengxiachen.s2sh.bean.User" table="user"  catalog="test">  
  8.         <id name="id" column="id" type="int">  
  9.             <generator class="increment"></generator>  
  10.         </id>  
  11.           
  12.         <property name="username" column="username" type="string"></property>  
  13.         <property name="password" column="password" type="string"></property>  
  14.           
  15.     </class>  
  16.   
  17. </hibernate-mapping>  

 步骤五:编写struts.xml文件:

Xml代码 
  1. <?xml version="1.0" encoding="UTF-8" ?>  
  2. <!DOCTYPE struts PUBLIC  
  3.     "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"  
  4.     "http://struts.apache.org/dtds/struts-2.0.dtd">  
  5.       
  6.  <struts>  
  7.     --><package name="s2sh" extends="struts-default" namespace="/user" >     
  8.         <action name="login" class="loginAction" >  
  9.           
  10.             <result name="success">/result.jsp</result>  
  11.             <result name="input">/index.jsp</result>  
  12.           
  13.         </action>  
  14.       
  15.     </package>  
  16.  </struts>  
 

步骤六:编写spring配置文件applicationContext.xml:

Xml代码 
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <beans  
  3.     xmlns="http://www.springframework.org/schema/beans"  
  4.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  5.     xmlns:p="http://www.springframework.org/schema/p"  
  6.     xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd">  
  7.   
  8. <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">  
  9.       
  10.     <property name="driverClassName" value="com.mysql.jdbc.Driver"></property>  
  11.     <property name="url" value="jdbc:mysql://localhost:3306/test"></property>  
  12.     <property name="username" value="root"></property>  
  13.     <property name="password" value=""></property>  
  14.     <property name="maxActive" value="100"></property>  
  15.     <property name="defaultAutoCommit" value="true"></property>  
  16. </bean>  
  17.   
  18. <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">  
  19.       
  20.     <property name="dataSource" ref="dataSource"></property>  
  21.     <property name="hibernateProperties">  
  22.         <props>  
  23.             <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>  
  24.             <prop key="hibernate.show_sql">true</prop>  
  25.         </props>  
  26.     </property>  
  27.     <property name="mappingResources">  
  28.         <list>  
  29.             <value>com/gufengxiachen/s2sh/bean/User.hbm.xml</value>  
  30.         </list>  
  31.     </property>  
  32.   
  33. </bean>  
  34.   
  35. <bean id="userDao" class="com.gufengxiachen.s2sh.daoimpl.UserDAOImpl" scope="singleton">  
  36.     <property name="sessionFactory">  
  37.         <ref bean="sessionFactory"/>  
  38.     </property>  
  39. </bean>  
  40.   
  41. <bean id="userService" class="com.gufengxiachen.s2sh.serviceimpl.UserServiceImpl">  
  42.     <property name="userDao" ref="userDao"></property>  
  43. </bean>  
  44.   
  45. <bean id="loginAction" class="com.gufengxiachen.s2sh.action.LoginAction">  
  46.     <property name="userService" ref="userService"></property>  
  47.   
  48. </bean>  
  49.   
  50. </beans>  
 步骤七:编写控制层Action,服务层(业务逻辑)Service,数据访问层,由于代码太多,详细代码见附件!

你可能感兴趣的:(java,spring,Hibernate,struts2)