Ssh整合开发介绍和简单的登入案例实现

Ssh整合开发介绍和简单的登入案例实现


  1. Ssh整合开发介绍和简单的登入案例实现  
  2. 一  介绍:  
  3. Ssh是strtus2-2.3.1.2+ spring-2.5.6+hibernate-3.6.8整合的开发,这是目前我的整合开发的使用技术和版本,使用的数据库为mySql。使用的开发工具是eclipse,eplipse的版本为Indigo Service Release 2  
  4. 二  搭建环境  
  5. 1.  首先要先引入struts2和sping和hibernate所需要的包  
  6. 1)struts2的包为:  
  7. struts-2.3.1.2\lib\ struts2-core-2.3.1.2.jar  
  8. struts-2.3.1.2\lib\ognl-3.0.4.jar  
  9. struts-2.3.1.2\lib\ xwork-core-2.3.1.2.jar  
  10. struts-2.3.1.2\lib\ freemarker-2.3.18.jar  
  11. struts-2.3.1.2\lib\commons-fileupload-1.2.2.jar  
  12. struts-2.3.1.2\lib\ commons-io-2.0.1.jar  
  13. struts-2.3.1.2\lib\commons-lang-2.5.jar  
  14. struts-2.3.1.2\lib\commons-logging-1.1.1.jar  
  15. struts-2.3.1.2\lib \struts2-json-plugin-2.3.1.2.jar  
  16. struts-2.3.1.2\lib \struts2-spring-plugin-2.3.1.2.jar  
  17. (2)引入hibernate的包  
  18. hibernate-distribution-3.6.8.Final\lib\required\*.jar  所有的jar包  
  19. hibernate-distribution-3.6.8.Final\lib\jpa\hibernate-jpa-2.0-api-1.0.1.Final.jar  
  20. hibernate-distribution-3.6.8.Final\ hibernate3.jar  
  21. (3)spring的包为:  
  22.     spring-framework-2.5.6\dist\ spring.jar  
  23.     spring-framework-2.5.6\lib\c3p0\c3p0-0.9.1.2.jar  
  24.     spring-framework-2.5.6\lib\aspectj\*.jar  
  25.     spring-framework-2.5.6\lib\j2ee\common-annotations.jar  
  26.     spring-framework-2.5.6\lib\log4j\log4j-1.2.15.jar  
  27.     spring-framework-2.5.6\lib\jakarta-commons\ commons-logging.jar  
  28. spring-framework-2.5.6\lib\cglib\cglib-nodep-2.1_3.jar    
  29. spring-framework-2.5.6\lib\dom4j\dom4j-1.6.1.jar  
  30. 2.  配置spring下所需要的文件  
  31. 1)首先配置spring所需要的xml文件  
  32.     我们在class下,即在src下创建一个applicationContext.xml的xml文件,文件的头文件因为要用到各种标签,所以我们在引入头文件的时候尽量引的比较多点,代码为:  
  33. <?xml version="1.0" encoding="UTF-8"?>  
  34. <beans xmlns="http://www.springframework.org/schema/beans"  
  35.     xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context"  
  36.     xmlns:tx="http://www.springframework.org/schema/tx" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  37.     xsi:schemaLocation="http://www.springframework.org/schema/beans           http://www.springframework.org/schema/beans/spring-beans-2.5.xsd   
  38.            http://www.springframework.org/schema/aop  
  39.            http://www.springframework.org/schema/aop/spring-aop-2.5.xsd   
  40.            http://www.springframework.org/schema/context  
  41.            http://www.springframework.org/schema/context/spring-context-2.5.xsd  
  42.            http://www.springframework.org/schema/tx  
  43.            http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">  
  44. </beans>  
  45. 2)在xml中配置和数据库相关联,并用c3p0来配置数据库连接池  
  46.     <!-- 数据库的连接池 -->  
  47.     <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"  
  48.         destroy-method="close">  
  49.         <property name="driverClass" value="com.mysql.jdbc.Driver"/>  
  50.         <property name="jdbcUrl"  
  51.     value="jdbc:mysql://localhost:3306/spring?useUnicode=true&characterEncoding=UTF-8" />  
  52.         <property name="user" value="root" />  
  53.         <property name="password" value="1234" />  
  54.         <!--初始化时获取的连接数,取值应在minPoolSize与maxPoolSize之间。Default: 3 -->  
  55.         <property name="initialPoolSize" value="1" />  
  56.         <!--连接池中保留的最小连接数。 -->  
  57.         <property name="minPoolSize" value="1" />  
  58.         <!--连接池中保留的最大连接数。Default: 15 -->  
  59.         <property name="maxPoolSize" value="300" />  
  60.         <!--最大空闲时间,60秒内未使用则连接被丢弃。若为0则永不丢弃。Default: 0 -->  
  61.         <property name="maxIdleTime" value="60" />  
  62.         <!--当连接池中的连接耗尽的时候c3p0一次同时获取的连接数。Default: 3 -->  
  63.         <property name="acquireIncrement" value="5" />  
  64.         <!--每60秒检查所有连接池中的空闲连接。Default: 0 -->  
  65.         <property name="idleConnectionTestPeriod" value="60" />  
  66.     </bean>  
  67.   
  68. 当然,这样所写的可以在一个properties中读取。读取外部文件在xml中的使用为:  
  69. <!-- 读取外部的文件 -->  
  70. <context:property-placeholder location="jdbc.properties" />  
  71.   
  72. (3)配置sessionFactory工厂,相当于是hibernate.cfg.xml里面的配置  
  73.     <!-- sessionFactory工厂 -->  
  74.     <bean id="sessionFactory"  
  75.         class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">  
  76.         <!-- 注入数据源 -->  
  77.         <property name="dataSource" ref="dataSource"></property>  
  78.         <!-- hibernate映射文件的引入 -->  
  79.         <property name="mappingResources">  
  80.             <list>  
  81.                 <value>cn/csdn/hr/s2sh/domain/Admin.hbm.xml</value>  
  82.             </list>  
  83.         </property>  
  84.         <!-- 配置hibernate属性的设置 -->  
  85.         <property name="hibernateProperties">  
  86.             <props>  
  87.                 <prop key="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</prop>  
  88.                 <prop key="hibernate.hbm2ddl.auto">update</prop>  
  89.                 <prop key="hibernate.show_sql">true</prop>  
  90.                 <prop key="hibernate.format_sql">true</prop>  
  91.             </props>  
  92.         </property>  
  93.     </bean>  
  94. 3.配置struts2.xml中需要的内容  
  95. (1)配置基本的struts2.xml   
  96.  <!DOCTYPE struts PUBLIC  
  97.     "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"  
  98.     "http://struts.apache.org/dtds/struts-2.3.dtd">  
  99. <struts>  
  100.     <!--使用spring创建管理struts2的action操作 -->  
  101.     <constant name="struts.objectFactory" value="spring"/>  
  102.     <include file="struts-admin.xml"></include>  
  103. </struts>  
  104. 注:其中include是引入的文件,一下的语句是非常重要的:  
  105. <constant name="struts.objectFactory" value="spring"/>,它是struts2和spring的连接的关键  
  106. (2)引入的struts-admin.xml文件,意在检测并作出一个简单的登入的实现  
  107.   <!DOCTYPE struts PUBLIC  
  108.     "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"  
  109.     "http://struts.apache.org/dtds/struts-2.3.dtd">  
  110. <struts>  
  111.     <package name="admin" extends="struts-default" namespace="/csdn">  
  112.         <!-- class的名称  等于spring中action配置文件中的id名称 -->  
  113.         <action name="loginAdmin" class="adminAction" method="login">  
  114.             <result name="success">../index.jsp</result>  
  115.         </action>  
  116.     </package>  
  117. </struts>  
  118. 4.  Hibernate.cfg.xml中的内容可以省略,它已经在spring中的xml中配置了  
  119. 5.搭建层之间的关系  
  120. (1)首先是domain,包为cn.csdn.hr.s2sh.domain,我们以admin为例  
  121. 属性为id和name和pass  
  122. 封装的实体类为:  
  123. package cn.csdn.hr.s2sh.domain;  
  124. import java.io.Serializable;  
  125. public class Admin implements Serializable {  
  126.     private static final long serialVersionUID = 1L;  
  127.     private Integer id;  
  128.     private String name;  
  129.     private String pass;  
  130.   
  131.     public Admin() {  
  132.         super();  
  133.         // TODO Auto-generated constructor stub  
  134.     }  
  135.   
  136.     public Admin(Integer id, String name, String pass) {  
  137.         super();  
  138.         this.id = id;  
  139.         this.name = name;  
  140.         this.pass = pass;  
  141.     }  
  142.   
  143.     public Integer getId() {  
  144.         return id;  
  145.     }  
  146.   
  147.     public void setId(Integer id) {  
  148.         this.id = id;  
  149.     }  
  150.   
  151.     public String getName() {  
  152.         return name;  
  153.     }  
  154.   
  155.     public void setName(String name) {  
  156.         this.name = name;  
  157.     }  
  158.   
  159.     public String getPass() {  
  160.         return pass;  
  161.     }  
  162.   
  163.     public void setPass(String pass) {  
  164.         this.pass = pass;  
  165.     }  
  166.   
  167.     @Override  
  168.     public String toString() {  
  169.         return "Admin [id=" + id + ", name=" + name + ", pass=" + pass + "]";  
  170.     }  
  171. }  
  172. (2)在同一个包下的实体类的映射文件  
  173. <?xml version="1.0" encoding="UTF-8"?>  
  174. <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"  
  175.                                    "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">  
  176. <hibernate-mapping package="cn.csdn.hr.s2sh.domain">  
  177.     <class name="Admin" table="admin">  
  178.         <id name="id" column="id">  
  179.             <generator class="native"></generator>  
  180.         </id>  
  181.         <property name="name" column="name"></property>  
  182.         <property name="pass" column="pass"></property>  
  183.     </class>  
  184. </hibernate-mapping>  
  185.   
  186. (3)创建dao层,创建的包名为cn.csdn.hr.s2sh.dao,创建的接口名为AdminDao,类名为AdminDaoImpl  
  187. 下面是创建的接口 AdminDao.java  
  188. package cn.csdn.hr.s2sh.dao;  
  189.   
  190. import java.util.List;  
  191.   
  192. import cn.csdn.hr.s2sh.domain.Admin;  
  193.   
  194. public interface AdminDao {  
  195.     public Admin login(final String name,final String pass);  
  196. }  
  197.   
  198. 创建的类为AdminDaoImpl.java  
  199. package cn.csdn.hr.s2sh.dao;  
  200. import java.sql.SQLException;  
  201. import java.util.List;  
  202. import org.hibernate.HibernateException;  
  203. import org.hibernate.Session;  
  204. import org.springframework.orm.hibernate3.HibernateCallback;  
  205. import org.springframework.orm.hibernate3.support.HibernateDaoSupport;  
  206.   
  207. import cn.csdn.hr.s2sh.domain.Admin;  
  208.   
  209. @SuppressWarnings("unchecked")  
  210. public class AdminDaoImpl extends HibernateDaoSupport implements AdminDao {  
  211.     public Admin login(final String name, final String pass) {  
  212.   
  213.         return (Admin) this.getHibernateTemplate().execute(  
  214.                 new HibernateCallback() {  
  215.   
  216.                     public Object doInHibernate(Session session)  
  217.                             throws HibernateException, SQLException {  
  218.                         // TODO Auto-generated method stub  
  219.   
  220.                         Object obj = session  
  221.                                 .createQuery(  
  222.                                         "from Admin where name=:name and pass=:pass")  
  223.                                 .setString("name", name)  
  224.                                 .setString("pass", pass).uniqueResult();  
  225.                         return obj;  
  226.                     }  
  227.                 });  
  228.     }  
  229. }  
  230.   
  231. (4)创建service层,创建的包名为cn.csdn.hr.s2sh.service  
  232. 创建的AdminService.java接口  
  233. package cn.csdn.hr.s2sh.service;  
  234.   
  235. import cn.csdn.hr.s2sh.dao.AdminDao;  
  236.   
  237.   
  238. public interface AdminService extends AdminDao{  
  239.   
  240. }  
  241. 创建的AdminServiceImpl.java  
  242. package cn.csdn.hr.s2sh.service;  
  243. import java.util.List;  
  244. import org.springframework.transaction.TransactionStatus;  
  245. import org.springframework.transaction.support.TransactionCallback;  
  246. import org.springframework.transaction.support.TransactionTemplate;  
  247. import cn.csdn.hr.s2sh.dao.AdminDao;  
  248. import cn.csdn.hr.s2sh.domain.Admin;  
  249. public class AdminServiceImpl implements AdminService {  
  250.   
  251.     private AdminDao adminDao;  
  252.     public void setAdminDao(AdminDao adminDao) {  
  253.         this.adminDao = adminDao;  
  254.     }  
  255.     public Admin login(String name, String pass) {  
  256.         // TODO Auto-generated method stub  
  257.         return adminDao.login(name, pass);  
  258.     }  
  259. }  
  260.   
  261.   
  262. 6.创建访问struts2的时候的action,我们把action放到一个bean-action.xml中  
  263. <?xml version="1.0" encoding="UTF-8"?>  
  264. <beans xmlns="http://www.springframework.org/schema/beans"  
  265.     xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context"  
  266.     xmlns:tx="http://www.springframework.org/schema/tx" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  267.     xsi:schemaLocation="http://www.springframework.org/schema/beans  
  268.            http://www.springframework.org/schema/beans/spring-beans-2.5.xsd   
  269.            http://www.springframework.org/schema/aop  
  270.            http://www.springframework.org/schema/aop/spring-aop-2.5.xsd   
  271.            http://www.springframework.org/schema/context  
  272.            http://www.springframework.org/schema/context/spring-context-2.5.xsd  
  273.            http://www.springframework.org/schema/tx  
  274.            http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">  
  275.   
  276.     <!-- 创建struts2中的action -->  
  277.     <!-- 配置admin的action -->  
  278.     <bean id="adminAction" class="cn.csdn.hr.s2sh.action.AdminAction"  
  279.         scope="prototype">  
  280.         <property name="adminService" ref="adminService"></property>  
  281.     </bean>  
  282. </beans>  
  283.   
  284. 而ref的adminServiceImpl是在bean-service.xml中  
  285. <?xml version="1.0" encoding="UTF-8"?>  
  286. <beans xmlns="http://www.springframework.org/schema/beans"  
  287.     xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context"  
  288.     xmlns:tx="http://www.springframework.org/schema/tx" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  289.     xsi:schemaLocation="http://www.springframework.org/schema/beans  
  290.            http://www.springframework.org/schema/beans/spring-beans-2.5.xsd   
  291.            http://www.springframework.org/schema/aop  
  292.            http://www.springframework.org/schema/aop/spring-aop-2.5.xsd   
  293.            http://www.springframework.org/schema/context  
  294.            http://www.springframework.org/schema/context/spring-context-2.5.xsd  
  295.            http://www.springframework.org/schema/tx  
  296.            http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">  
  297.   
  298.     <!-- admin的业务操作 -->  
  299.     <bean id="adminServiceImpl" class="cn.csdn.hr.s2sh.service.AdminServiceImpl">  
  300.         <property name="adminDao" ref="adminDao"></property>  
  301.     </bean>  
  302. </beans>  
  303.   
  304. 上面的ref是adminDaoImpl,在beans-dao.xml  
  305. <?xml version="1.0" encoding="UTF-8"?>  
  306. <beans xmlns="http://www.springframework.org/schema/beans"  
  307.     xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context"  
  308.     xmlns:tx="http://www.springframework.org/schema/tx" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  309.     xsi:schemaLocation="http://www.springframework.org/schema/beans  
  310.            http://www.springframework.org/schema/beans/spring-beans-2.5.xsd   
  311.            http://www.springframework.org/schema/aop  
  312.            http://www.springframework.org/schema/aop/spring-aop-2.5.xsd   
  313.            http://www.springframework.org/schema/context  
  314.            http://www.springframework.org/schema/context/spring-context-2.5.xsd  
  315.            http://www.springframework.org/schema/tx  
  316.            http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">  
  317.   
  318.   
  319.     <!-- hibernatedao的模板类 HibernateDaoSuppor 抽象类不可以实例化 加上abstract="true" -->  
  320.     <bean id="hibernateDaoSupport"  
  321.         class="org.springframework.orm.hibernate3.support.HibernateDaoSupport"  
  322.         abstract="true">  
  323.         <property name="sessionFactory" ref="sessionFactory"></property>  
  324.     </bean>  
  325.     <!-- admin的dao对象 -->  
  326.     <bean id="adminDao" class="cn.csdn.hr.s2sh.dao.AdminDaoImpl"  
  327.         parent="hibernateDaoSupport" />  
  328. </beans>  
  329.   
  330. 然后我们把上面的三个beans.xml导入到applicationContext.xml中  
  331. <!-- 导入其他的配置文件 -->  
  332. <import resource="beans-dao.xml" />  
  333. <import resource="beans-service.xml" />  
  334. <import resource="beans-action.xml" />  
  335.   
  336. 7.配置web.xml  
  337. <?xml version="1.0" encoding="UTF-8"?>  
  338. <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  339.     xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"  
  340.     xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"  
  341.     id="WebApp_ID" version="2.5">  
  342.     <display-name>s2sh</display-name>  
  343.     <welcome-file-list>  
  344.         <welcome-file>index.html</welcome-file>  
  345.         <welcome-file>index.htm</welcome-file>  
  346.         <welcome-file>index.jsp</welcome-file>  
  347.         <welcome-file>default.html</welcome-file>  
  348.         <welcome-file>default.htm</welcome-file>  
  349.         <welcome-file>default.jsp</welcome-file>  
  350.     </welcome-file-list>  
  351.   
  352.     <!-- 指定spring的配置文件,默认从web根目录寻找配置文件,我们可以通过spring提供的classpath:前缀指定从类路径下寻找 -->  
  353.     <context-param>  
  354.         <param-name>contextConfigLocation</param-name>  
  355.         <param-value>classpath:applic*.xml</param-value>  
  356.     </context-param>  
  357.     <!-- 对Spring容器进行实例化 -->  
  358.     <listener>  
  359.         <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>  
  360.     </listener>  
  361.   
  362.     <!-- struts2 的配置 -->  
  363.     <filter>  
  364.         <filter-name>struts2</filter-name>  
  365.         <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>  
  366.     </filter>  
  367.   
  368.     <filter-mapping>  
  369.         <filter-name>struts2</filter-name>  
  370.         <url-pattern>/*</url-pattern>  
  371.     </filter-mapping>  
  372.   
  373.     <!-- 配置 使用spring解决hibernate因session关闭导致的延迟加载例外问题 -->  
  374.     <filter>  
  375.         <filter-name>OpenSessionInViewFilter</filter-name>  
  376.         <filter-class>org.springframework.orm.hibernate3.support.OpenSessionInViewFilter</filter-class>  
  377.         <init-param>  
  378.             <!-- 指定org.springframework.orm.hibernate3.LocalSessionFactoryBean在spring配置文件中的名称,默认值为sessionFactory.如果LocalSessionFactoryBean在spring中的名称不是sessionFactory,该参数一定要指定,否则会出现找不到sessionFactory的例外 -->  
  379.             <param-name>sessionFactoryBeanName</param-name>  
  380.             <param-value>sessionFactory</param-value>  
  381.         </init-param>  
  382.     </filter>  
  383.     <filter-mapping>  
  384.         <filter-name>OpenSessionInViewFilter</filter-name>  
  385.         <url-pattern>/*</url-pattern>  
  386.     </filter-mapping>  
  387.   
  388.     <!-- 使用spring解决struts2的中文乱码的问题 -->  
  389.     <filter>  
  390.         <filter-name>encoding</filter-name>     <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>  
  391.         <init-param>  
  392.             <param-name>encoding</param-name>  
  393.             <param-value>UTF-8</param-value>  
  394.         </init-param>  
  395.     </filter>  
  396.     <filter-mapping>  
  397.         <filter-name>encoding</filter-name>  
  398.         <url-pattern>/*</url-pattern>  
  399.     </filter-mapping>  
  400. </web-app>  
  401.   
  402. 8.我们来做一个简单的登入,使用的jsp页面为:  
  403. <body>  
  404.     <div>  
  405.         <form action="/s2sh/csdn/loginAdmin.action" method="get">  
  406.             用户名:<input type="text" name="admin.name"/><br/>  
  407.             密  码:<input type="password" name="admin.pass"/><br/>  
  408.             <input type="submit" value="登入"/>  
  409.             <input type="reset" value="重置"/>  
  410.         </form>  
  411.     </div>  
  412.     <div>  
  413.         用户名为:${admin.name}  
  414.     </div>  
  415. </body>  
  416.   
  417. 访问到的action的处理为:  
  418. package cn.csdn.hr.s2sh.action;  
  419. import cn.csdn.hr.s2sh.domain.Admin;  
  420. import cn.csdn.hr.s2sh.service.AdminService;  
  421. import com.opensymphony.xwork2.ActionSupport;  
  422. public class AdminAction extends ActionSupport {  
  423.     private static final long serialVersionUID = 1L;  
  424.   
  425.     private AdminService adminService;  
  426.   
  427.     private Admin admin;  
  428.   
  429.     public Admin getAdmin() {  
  430.         return admin;  
  431.     }  
  432.   
  433.     public void setAdmin(Admin admin) {  
  434.         this.admin = admin;  
  435.     }  
  436.   
  437.     // 注入  
  438.     public void setAdminService(AdminService adminService) {  
  439.         this.adminService = adminService;  
  440.     }  
  441.   
  442.     public String login() {  
  443.         System.out.println("用户名:" + admin.getName());  
  444.         admin = adminService.login(admin.getName(), admin.getPass());  
  445.         return SUCCESS;  
  446.     }  
  447. }  
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值