Struts2+Spring3+Hibernate3配置全过程

Struts2+Spring3+Hibernate3配置全过程最近,使用ssh框架实现了一个简单的用户登录过程,麻雀虽小,五脏俱全。jdk使用的版本是1.6。从头开始说,首先从他们各自的官网上下载sdk开发包,我下的版本是:hibernate-3.5.5、spring-3.1.0.M1、struts-2.2.1.1。如下图所示:   比较大,里边东西很多,文档、源码、jar包、例子啥都有。我们开发时,只需要几个必需的jar包(总共大约30个左右)。

     最近,使用ssh框架实现了一个简单的用户登录过程,麻雀虽小,五脏俱全。jdk使用的版本是1.6。从头开始说,首先从他们各自的官网上下载sdk开发包,我下的版本是:hibernate-3.5.5、spring-3.1.0.M1、struts-2.2.1.1。如下图所示:

Struts2+Spring3+Hibernate3配置全过程

    比较大,里边东西很多,文档、源码、jar包、例子啥都有。我们开发时,只需要几个必需的jar包(总共大约30个左右)。各位如果嫌大不想下载,可以直接下载我的项目,从项目中\WEB-INF\lib目录下拷贝就行了。所需的Jar包如图所示:

Struts2+Spring3+Hibernate3配置全过程

下面是各个jar包的来源:

Hibernate:

  • antlr-2.7.6.jar
  • commons-collections-3.1.jar
  • dom4j-1.6.1.jar
  • hibernate3.jar
  • javassist-3.9.0.GA.jar
  • jta-1.1.jar
  • slf4j-api-1.5.8.jar
  • slf4j-nop-1.5.8.jar(这个jar包需要从slf4j框架中找)

 

Spring:

  • commons-dbcp.jar
  • commons-logging-1.0.4.jar
  • commons-pool.jar
  • org.springframework.asm-3.1.0.M1.jar
  • org.springframework.beans-3.1.0.M1.jar
  • org.springframework.context-3.1.0.M1.jar
  • org.springframework.core-3.1.0.M1.jar
  • org.springframework.expression-3.1.0.M1.jar
  • org.springframework.jdbc-3.1.0.M1.jar
  • org.springframework.transaction-3.1.0.M1.jar
  • org.springframework.web-3.1.0.M1.jar

 

Struts:

  • aopalliance-1.0.jar
  • commons-fileupload-1.2.1.jar
  • commons-io-1.3.2.jar
  • commons-logging-1.0.4.jar
  • freemarker-2.3.16.jar
  • javassist-3.7.ga.jar
  • ognl-3.0.jar
  • struts2-core-2.2.1.1.jar
  • struts2-spring-plugin-2.2.1.1.jar
  • xwork-core-2.2.1.1.jar

 

好,下面准备开始。

打开eclipse,新建一个web,项目取名SSH1,结构如下图所示,建立根据如图所示建立相应的包、文件夹、java文件、jsp文件、xml文件,最后将需要的jar文件拷贝到\WEB-INF\lib下。

Struts2+Spring3+Hibernate3配置全过程

先说配置文件:

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
     <filter>
         <filter-name>struts2</filter-name>
         <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
         <init-param>
              <param-name>config</param-name>
                  <param-value>struts-default.xml,struts-plugin.xml,../struts-conf/struts.xml
              </param-value>
         </init-param>
     </filter>

     <filter-mapping>
         <filter-name>struts2</filter-name>
         <url-pattern>/*</url-pattern>
     </filter-mapping>

     <context-param>
         <param-name>contextConfigLocation</param-name>
         <param-value>/WEB-INF/spring-conf/spring*.xml</param-value>
     </context-param>

     <listener>
         <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>    
     </listener>

  <display-name>SSH1</display-name>

  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>default.html</welcome-file>
    <welcome-file>default.htm</welcome-file>
    <welcome-file>default.jsp</welcome-file>
    <welcome-file>login.jsp</welcome-file>
  </welcome-file-list>
</web-app>

struts.xml

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
    "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
    "http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
    <!-- Add packages here -->
    <!-- <include file="struts-default.xml"/> -->
    <package name="struts2" namespace="/" extends="struts-default">
        <action name="login" class="loginAction">
            <result name="success">/jsp/success.jsp</result>
            <result name="failure">/jsp/failure.jsp</result>
        </action>
    </package>
</struts> 


spring-core.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: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-3.0.xsd
          http://www.springframework.org/schema/aop
          http://www.springframework.org/schema/aop/spring-aop-2.0.xsd
          http://www.springframework.org/schema/tx
          http://www.springframework.org/schema/tx/spring-tx-2.0.xsd">
     <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
         <property name="configLocation">
              <value>classpath:hibernate.cfg.xml</value>
         </property>
     </bean>

     <bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
         <property name="sessionFactory">
              <ref bean="sessionFactory"/>
         </property>
     </bean>

     <tx:annotation-driven transaction-manager="transactionManager" />
</beans>

 

spring-conf.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" 
     xsi:schemaLocation="http://www.springframework.org/schema/beans  
     http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">  
     <bean id="loginAction" class="com.zzb.action.LoginAction" scope="prototype">
         <property name="baseService" ref="baseService" />
     </bean>
     <bean id="baseService" class="com.zzb.service.BaseServiceImpl" >
         <property name="baseDao" ref="baseDAO" />
     </bean>
     <bean id="baseDAO" class="com.zzb.dao.BaseDAOImpl" scope="prototype">
         <property name="sessionFactory" ref="sessionFactory" />
     </bean>
</beans>

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">
<hibernate-configuration>
    <session-factory>
        <!-- Database connection settings -->
        <property name="connection.driver_class">com.mysql.jdbc.Driver</property>
        <property name="connection.url">jdbc:mysql://localhost:3306/hibernate2</property>
        <property name="connection.username">root</property>
        <property name="connection.password">123</property>
        <!-- JDBC connection pool (use the built-in) -->
        <property name="connection.pool_size">2</property>
        <!-- SQL dialect -->
        <property name="dialect">org.hibernate.dialect.MySQL5Dialect</property>
        <!-- Enable Hibernate's current session context -->
        <property name="current_session_context_class">org.hibernate.context.ManagedSessionContext</property>
        <!-- Disable the second-level cache  -->
        <property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property>
        <!-- Echo all executed SQL to stdout -->
        <property name="show_sql">true</property>
        <!-- Drop and re-create the database schema on startup -->
        <property name="hbm2ddl.auto">update</property>
        <mapping resource="com/zzb/vo/User.hbm.xml"/>
    </session-factory>
</hibernate-configuration>

User.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">
<hibernate-mapping package="com.zzb.vo">
    <class name="User" table="user">
        <id name="id">
            <generator class="native"/>
        </id>
        <property name="name" column="name"/>
        <property name="date" column="date"/>
    </class>
</hibernate-mapping>

然后是JSP文件

login.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<%@taglib prefix="s" uri="/struts-tags"%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<s:form action="login.action" namespace="">
    <s:textfield key="username"/>
    <s:password key="password" />
    <s:submit/>
</s:form>
</body>
</html>

success.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
success成功</br>
username:${requestScope.username}</br>
password:${requestScope.password}</br>
</body>
</html>

failure.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
failure失败</br>
username:${requestScope.username}</br>
password:${requestScope.password}</br>
</body>
</html>

最后是java源文件

LoginAction.java

package com.zzb.action;
import com.zzb.service.BaseService;
import com.opensymphony.xwork2.ActionSupport;
import com.zzb.vo.User;

public class LoginActionextends ActionSupport {
     private String username;
     private String password;
     private BaseService baseService;

     public String execute(){
         System.out.println(username);
         System.out.println(password);
         User user =new User();
         user.setId(2);

         user = (User) baseService.getVOById(user);
         System.out.println(">>>>>>>>>>>>>>>>>>"+user.getName());
         System.out.println(">>>>>>>>>>>>>>>>>>"+user.getDate());
         String result = "success";
        return result;
     }

     public String getUsername() {
        return username;
     }

     public void setUsername(String username) {
        this.username = username;
     }

     public String getPassword() {
        return password;
     }

     public void setPassword(String password) {
        this.password = password;
     }

     public BaseService getBaseService() {
        return baseService;
     }

     public void setBaseService(BaseService baseService) {
        this.baseService = baseService;
     }
}

BaseDAO.java

package com.zzb.dao;

import java.util.List;
import com.zzb.vo.BaseVO;

public interface BaseDAO {
     public BaseVO addVO(BaseVO vo);
     public boolean deleteVOById(BaseVO vo);
     public void updateVO(BaseVO vo);
     public BaseVO getVOById(BaseVO id);
     public List<BaseVO> queryVO(BaseVO obj);
     public List<BaseVO> queryVOBySQL(String sql);
     public List<BaseVO> queryAll();
} 

 
BaseDAOImpl.java

package com.zzb.dao;

import java.util.List;
import org.hibernate.SessionFactory;
import com.zzb.vo.BaseVO;

public class BaseDAOImplimplements BaseDAO {
     private SessionFactory sessionFactory;

     public BaseVO addVO(BaseVO vo) {
        return null;
     }

     public boolean deleteVOById(BaseVO vo) {
        return false;
     }

     public BaseVO getVOById(BaseVO vo) {
        assert(vo.getId()!=null);
         BaseVO result = (BaseVO)sessionFactory.openSession().load(vo.getClass(), vo.getId());
        return result;
     }

     public List<BaseVO> queryAll() {
        return null;
     }

     public List<BaseVO> queryVO(BaseVO vo) {
        return null;
     }

     public List<BaseVO> queryVOBySQL(String sql) {
        return null;
     }

     public void updateVO(BaseVO vo) { 

     }

     public SessionFactory getSessionFactory() {
        return sessionFactory;
     }

     public void setSessionFactory(SessionFactory sessionFactory) {
        this.sessionFactory = sessionFactory;
     }
}

BaseService.java

package com.zzb.service;

import java.util.List;
import com.zzb.vo.BaseVO;

public interface BaseService {
     public BaseVO addVO(BaseVO vo);
     public boolean deleteVOById(BaseVO vo);
     public void updateVO(BaseVO vo);
     public BaseVO getVOById(BaseVO user);
     public List<BaseVO> queryVO(BaseVO obj);
     public List<BaseVO> queryVOBySQL(String sql);
     public List<BaseVO> queryAll();
}

BaseServiceImpl.java

package com.zzb.service;

import java.util.List;
import com.zzb.dao.BaseDAO;
import com.zzb.vo.BaseVO;

public class BaseServiceImplimplements BaseService {
     BaseDAO baseDao;

     public BaseVO addVO(BaseVO vo) {
        return null;
     }

     public boolean deleteVOById(BaseVO vo) {
        return false;
     }

     public BaseVO getVOById(BaseVO BaseVO) {
        return (BaseVO)baseDao.getVOById(BaseVO);
     }

      public List<BaseVO> queryAll() {
       return null;
     }

     public List<BaseVO> queryVO(BaseVO obj) {
        return null;
     }

     public List<BaseVO> queryVOBySQL(String sql) {
        return null;
     }

     public void updateVO(BaseVO vo) { 

     }

     public BaseDAO getBaseDao() {
        return baseDao;
     }

     public void setBaseDao(BaseDAO baseDao) {
        this.baseDao = baseDao;
     }
}

BaseVO.java

package com.zzb.vo;

import java.io.Serializable; 

public class BaseVOimplements Serializable {
     private Integer id;

     public Integer getId() {
        return id;
     }

     public void setId(Integer id) {
        this.id = id;
     }
}

User.java

package com.zzb.vo;

import java.util.Date;

public class Userextends BaseVO{
     private String name;
     private Date date;

     public String getName() {
        return name;
     }

     public void setName(String name) {
        this.name = name;
     }

     public Date getDate() {
        return date;
     }

     public void setDate(Date date) {
        this.date = date;
     }
}

 以上就是全部代码,虽然有些简单,很多类方法都没有写,但已经包含了了ssh框架全部的配置过程。

代码都完成了,然后就可以部署了,我使用的服务器是tomcat6.0,数据库是mysql5.0。部署时不要忘了将数据库的驱动mysql-connector-java-5.0.3-bin.jar考到tomcat目录lib下。部署完成后,启动,访问http://127.0.0.1:8080/SSH1,如果出来成功页面,就成功了。好,就这么多。各位如果有什么问题可以问我。

 

项目下载地址:http://download.csdn.net/detail/zhengzhb/3717014

 

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
如需转载请保留出处:https://bianchenghao.cn/36064.html

(0)
编程小号编程小号

相关推荐

发表回复

您的电子邮箱地址不会被公开。 必填项已用*标注