SSH+oracle开发环境搭建。MyEclipse 10+Struts2+Hibernate3.3+Spring3.2.3+tomcat-6.0.35

我的工程导出来的jar包的下载地址为点击打开链接

一,开发环境准备工作。

0,oracle数据库的表名和表结构如图:

       1,我这里用的各个软件版本如下:MyEclipse 10,tomcat-6.0.35 ,struts-2.2.1.1,spring-framework-3.2.3.RELEASE-dist,这里struts-2.2.1.1因为超过60M,传不了CSDN,所以大家自己去下载一下,spring-framework-3.2.3.RELEASE-dist下载地址:http://download.csdn.net/detail/liujianian/8612689,Hibernate3.3我是直接在MyEclipse中加载的,操作步骤如下:

               ①:建立一个新的web project工程,选择Java EE6.0,点击完成。

               ②:这里我们需要引入外部的驱动jar包(ojdbc14.jar),下载地址:http://download.csdn.net/detail/liujianian/8612779。将ojdbc14.jar包拷入“test”工程中WebRoot/WEB-INF/lib下

               ③:选择“test”工程,右击选择“myEclipse”-->"Add Hibernate Capabilities",选择Hibernate3.3,

               ④:选择“next”

               ⑤:选择“next”,填写数据库连接的相关信息,这个以后在hibernate.cfg.xml中还可以修改的。

               ⑥:选择“next”,,我们不创建Session Factory类(将上面的勾去掉),等后面工程里面我自己新建,点击“finish”完成hibernate包的加载。完成后如图会出现以下jar包:参照后面的目录截图

        2,外部引入我们下载的Struts包。

              ①:选择工程右击“Build Path”-->"Add Libraries",,选择“User Library”-->"next",选择“UserLibraries",命名为Struts2,选择struts下lib下面的这些包,点击OK这样struts包就加载好了。如图:参照后面的目录截图

 3,准备工作做好了,下面我们来进行Struts和hibernate的整合开发,spring最后再整合。

     文件目录如下:

     ①:LoginAction.java(action类)

package com.ssh.action;

import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.Preparable;
import com.ssh.forms.UserForm;
import com.ssh.service.UserManager;
import com.ssh.serviceImpl.UserManagerImpl;

public class LoginAction extends ActionSupport implements Preparable{
	//该类继承了ActionSupport类。这样就可以直接使用SUCCESS, LOGIN等变量和重写execute等方法
	/**
	 * 
	 */
	private static final long serialVersionUID = 724458459147961157L;
	private UserForm user;  
	  
    private UserManager userManager;  
  
    public UserForm getUser() {  
        return user;  
    }  
  
    public void setUser(UserForm user) {  
        this.user = user;  
    }  
  
    public UserManager getUserManager() {  
        return userManager;  
    }  
  
    public void setUserManager(UserManager userManager) {  
        this.userManager = userManager;  
    }  

	@Override
    public String execute() {
        try{
        	this.setUserManager(new UserManagerImpl());
        	user.setUserId("1");
            userManager.regUser(user);
            return SUCCESS;
        }catch(Exception e){
        	e.printStackTrace();
        	return ERROR;
        }
	}
	//清除LoginAction-validation.xml爆出的错误信息
	public void prepare(){
		clearErrorsAndMessages();
	};
}

②:LoginAction-validation.xml(check前台页面的配置文件,和action在一个目录下面)

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE validators PUBLIC "-//OpenSymphony Group//XWork Validator 1.0.2//EN"  "http://www.opensymphony.com/xwork/xwork-validator-1.0.2.dtd">  
<validators>  
    <!-- 添加对用户名的校验 -->  
    <field name="user.username">  
        <field-validator type="requiredstring">  
            <param name="trim">true</param>  
            <message>用户名不能为空</message>  
        </field-validator>  
        <field-validator type="regex">  
            <param name="expression"><![CDATA[(\w{4,16})]]></param>  
            <message>用户名输入不合法,必须为长度在4~16中间的数字或字母</message>  
        </field-validator>  
    </field>  
      
    <!-- 添加对密码的校验 -->  
    <field name="user.password">  
        <field-validator type="requiredstring">  
            <param name="trim">true</param>  
            <message>密码不能为空</message>  
        </field-validator>  
        <field-validator type="regex">  
            <param name="expression"><![CDATA[(\w{4,16})]]></param>  
            <message>密码输入不合法,必须为长度在4~16之间的数字或者字母</message>  
        </field-validator>  
    </field>  
</validators>  
③:User.java(beans)
package com.ssh.beans;

public class User {
	private String userId;
	private String username;  
    private String password;  
    
    public String getUserId() {
		return userId;
	}
	public void setUserId(String userId) {
		this.userId = userId;
	}
	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;  
    }  
}
④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.ssh.beans">  <!-- 數據庫表名和java類名的映射 -->
    <class name="User" table="t_user">  
        <id name="userId" column="user_id">  
        </id>  
        <property name="username" column="user_name" type="java.lang.String"  
            not-null="true" length="16"></property>  
        <property name="password" column="user_password" type="java.lang.String"  
            not-null="true" length="16" />  
    </class>  
</hibernate-mapping>  
⑤:BaseDao.java:
package com.ssh.dao;
import org.hibernate.HibernateException;  
import org.hibernate.Session; 
public interface  BaseDao {
	
	public void saveObject(Object obj) throws HibernateException;  
	
    public Session getSession();  
    public void setSession(Session session);  
}
⑥:HibernateSessionFactory.java
package com.ssh.dao.impl;

import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.cfg.Configuration;

/**
 * Configures and provides access to Hibernate sessions, tied to the
 * current thread of execution.  Follows the Thread Local Session
 * pattern, see {@link http://hibernate.org/42.html }.
 */
public class HibernateSessionFactory {

    /** 
     * Location of hibernate.cfg.xml file.
     * Location should be on the classpath as Hibernate uses  
     * #resourceAsStream style lookup for its configuration file. 
     * The default classpath location of the hibernate config file is 
     * in the default package. Use #setConfigFile() to update 
     * the location of the configuration file for the current session.   
     */
    private static String CONFIG_FILE_LOCATION = "/hibernate.cfg.xml";
	private static final ThreadLocal<Session> threadLocal = new ThreadLocal<Session>();
    private  static Configuration configuration = new Configuration();    
    private static org.hibernate.SessionFactory sessionFactory;
    private static String configFile = CONFIG_FILE_LOCATION;

	static {
    	try {
			configuration.configure(configFile);
			sessionFactory = configuration.buildSessionFactory();
		} catch (Exception e) {
			System.err
					.println("%%%% Error Creating SessionFactory %%%%");
			e.printStackTrace();
		}
    }
    private HibernateSessionFactory() {
    }
	
	/**
     * Returns the ThreadLocal Session instance.  Lazy initialize
     * the <code>SessionFactory</code> if needed.
     *
     *  @return Session
     *  @throws HibernateException
     */
    public static Session getSession() throws HibernateException {
        Session session = (Session) threadLocal.get();

		if (session == null || !session.isOpen()) {
			if (sessionFactory == null) {
				rebuildSessionFactory();
			}
			session = (sessionFactory != null) ? sessionFactory.openSession()
					: null;
			threadLocal.set(session);
		}

        return session;
    }

	/**
     *  Rebuild hibernate session factory
     *
     */
	public static void rebuildSessionFactory() {
		try {
			configuration.configure(configFile);
			sessionFactory = configuration.buildSessionFactory();
		} catch (Exception e) {
			System.err
					.println("%%%% Error Creating SessionFactory %%%%");
			e.printStackTrace();
		}
	}

	/**
     *  Close the single hibernate session instance.
     *
     *  @throws HibernateException
     */
    public static void closeSession() throws HibernateException {
        Session session = (Session) threadLocal.get();
        threadLocal.set(null);

        if (session != null) {
            session.close();
        }
    }

	/**
     *  return session factory
     *
     */
	public static org.hibernate.SessionFactory getSessionFactory() {
		return sessionFactory;
	}

	/**
     *  return session factory
     *
     *	session factory will be rebuilded in the next call
     */
	public static void setConfigFile(String configFile) {
		HibernateSessionFactory.configFile = configFile;
		sessionFactory = null;
	}

	/**
     *  return hibernate configuration
     *
     */
	public static Configuration getConfiguration() {
		return configuration;
	}

}

⑦UserDao.java:
package com.ssh.dao.impl;

import org.hibernate.HibernateException;
import org.hibernate.Session;

import com.ssh.dao.BaseDao;

public class UserDao implements BaseDao{
	private Session session;  
	  
    @Override  
    public Session getSession() {  
        return session;  
    }  
  
    @Override  
    public void setSession(Session session) {  
        this.session = session;  
    }  
  
    @Override  
    public void saveObject(Object obj) throws HibernateException {  
        session.save(obj);  
    }  
}

⑧UserForm.java
package com.ssh.forms;

public class UserForm {
	private String userId;
	private String username;  
    private String password;  
    public String getUserId() {
		return userId;
	}
	public void setUserId(String userId) {
		this.userId = userId;
	}
    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;  
    }  
}
⑨:
package com.ssh.service;

import com.ssh.forms.UserForm;

public interface  UserManager {
	public void regUser(UserForm user);  
}
⑩:
package com.ssh.serviceImpl;

import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.Transaction;

import com.ssh.beans.User;
import com.ssh.dao.BaseDao;
import com.ssh.dao.impl.HibernateSessionFactory;
import com.ssh.dao.impl.UserDao;
import com.ssh.forms.UserForm;
import com.ssh.service.UserManager;

public class UserManagerImpl implements UserManager {
	private BaseDao dao;
	private Session session;
	public UserManagerImpl(){
		dao=new UserDao();
	}
	@Override  
    public void regUser(UserForm userForm) throws HibernateException {  
		session = HibernateSessionFactory.getSession(); 
        dao.setSession(session);
        // 获取事务  
        Transaction ts = session.beginTransaction();  
        // 构造User对象  
        User user = new User();  
        user.setUserId(userForm.getUserId());
        user.setUsername(userForm.getUsername());  
        user.setPassword(userForm.getPassword());
        
        // 保存User对象  
        dao.saveObject(user);  
        // 提交事务  
        ts.commit();  
        // 关闭Session  
        HibernateSessionFactory.closeSession();  
    }  
}
11,hibernate.cfg.xml(hibernate的住配置文件)
<?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.Oracle9Dialect</property>
        <property name="connection.url">jdbc:oracle:thin:@localhost:1521:orcl</property>
        <property name="connection.username">用戶名</property>
        <property name="connection.password">密碼</property>
        <property name="connection.driver_class">oracle.jdbc.driver.OracleDriver</property>
        <property name="show_sql">true</property>
        <mapping resource="com/ssh/beans/User.hbm.xml"/><!-- 配置数据表映射文件 -->
    </session-factory>

</hibernate-configuration>
12,
<?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>
<include file="struts-default.xml"></include>
<constant name="struts.devMode" value="true" /><!-- 开发模式 -->
    <package name="default"  extends="struts-default" namespace="/">
        <action name="login" class="com.ssh.action.LoginAction" method="execute">
            <result name="success">/welcome.jsp</result>
            <result name="error">/error.jsp</result>
            <result name="input">/input.jsp</result>
        </action>
    </package>
</struts>

13,web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0" 
	xmlns="http://java.sun.com/xml/ns/javaee" 
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
	xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
	http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
  <display-name></display-name>	
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
  <filter>
       <filter-name>struts2</filter-name>
       <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
   </filter>
   <filter-mapping>
       <filter-name>struts2</filter-name>
       <url-pattern>/*</url-pattern>
   </filter-mapping>
</web-app>

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+"/";
%>
<%@ taglib uri="/struts-tags" prefix="s" %>
<!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>
   <s:form action="login" method="post">
    <s:label value="系统登陆"></s:label>
    <s:textfield name="user.username" label="账号" />
    <s:password name="user.password" label="密码" />
    <s:submit value="登录" />
   </s:form>
  </body>
</html>
15,error.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 'success.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>
    error
  </body>
</html>
16,input.jsp
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<%@ taglib uri="/struts-tags" prefix="s" %>
<!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>
   <s:form action="login" method="post">
    <s:label value="系统再登陆"></s:label>
    <s:textfield name="user.username" label="账号" />
    <s:password name="user.password" label="密码" />
    <s:submit value="登录" />
   </s:form>
  </body>
</html>

17,welcome.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 'success.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>
    successfully,欢迎${username}!
  </body>
</html>

这样我们的struts+hibernate的实验工程就OK了,下面我们运行一下如图:

登陆到数据库的数据:

4,下面我们再来外部加载spring报,参照struts包外部引入,这里就不再重复了。引入的包是spring\libs下面除了javadoc和sources的所有包

引入完了。

整个工程的目录如下图:

  1. 将Spring内libs目录下包含所有的jar包(不需要复制结尾为sources和javadoc的jar包)到SSHProject项目的lib目录下。
  2. 编写Spring的配置文件applicationContext.xml。路径:src目录下,需要在web.xml配置context-param指定路径,或者把该文件放在WEB-INF下,跟web.xml同目录。这里由于Spring配置数据源的需要,需要把Hibernate内lib/optional/c3p0下的c3p0-0.9.1.jar复制到lib不目下。
  3. 修改BaseDao和UserDao。在引入Spring后,需要用Spring进行统一的事务管理,数据源和sessionFactory都交给Spring去生成,因此接口类和实现类BaseDao和UserDao都需要做相应的修改。Spring提供了HibernateDaoSupport类来完成对数据的操作,因此UserDao在实现BaseDao的同时还需要继承HibernateDaoSupport类。并将先前session的操作修改成HibernateTemplate(可通过getHibernateTemplate()方法来获得)的操作。
  4. 修改业务逻辑实现类。在没有加入Spring之前,业务逻辑实现类的Session的获得,dao的实例化,以及事务的管理都是该类执行管理的。加入Spring后,这些都交给Spring去管理。该类的dao的实例化由Spring注入。
  5. 修改用户注册的RegisterAction类。同样,RegisterAction类中的userManager的实例化也由Spring注入。
  6. 删除Hibernate的配置文件Hibernate.cfg.xml和工厂类HibernateSesseionFactory类。他们的工作已经交给Spring去做,已经不再有用。
  7. 修改web.xml,加载Spring。要想启动时加载Spring的配置文件,需要在web.xml中配置对应的监听器(listenser),并制定Spring的配置文件。
  8. 修改Struts的配置文件struts.xml。把原来指定的名为register的action的class由原来的路径变为applicationContext.xml文件中该Action的id。
  9. LoginAction.java如下,LoginAction-validation.xml没有变化。
    package com.ssh.action;
    
    import com.opensymphony.xwork2.ActionSupport;
    import com.opensymphony.xwork2.Preparable;
    import com.ssh.forms.UserForm;
    import com.ssh.service.UserManager;
    
    public class LoginAction extends ActionSupport implements Preparable{
    	//该类继承了ActionSupport类。这样就可以直接使用SUCCESS, LOGIN等变量和重写execute等方法
    	/**
    	 * 
    	 */
    	private static final long serialVersionUID = 724458459147961157L;
    	private UserForm user;  
    	  
        private UserManager userManager;  
      
        public UserForm getUser() {  
            return user;  
        }  
      
        public void setUser(UserForm user) {  
            this.user = user;  
        }  
      
        public UserManager getUserManager() {  
            return userManager;  
        }  
      
        public void setUserManager(UserManager userManager) {  
            this.userManager = userManager;  
        }  
    
    	@Override
        public String execute() {
            try{
            	user.setUserId("2");
                userManager.regUser(user);
                return SUCCESS;
            }catch(Exception e){
            	e.printStackTrace();
            	return ERROR;
            }
    	}
    	//清除LoginAction-validation.xml爆出的错误信息
    	public void prepare(){
    		clearErrorsAndMessages();
    	};
    }
    
  10. User.java如下:User.hbm.xml没有变化
    package com.ssh.beans;
    
    public class User {
    	private String userId;
    	private String username;  
        private String password;  
        
        public String getUserId() {
    		return userId;
    	}
    	public void setUserId(String userId) {
    		this.userId = userId;
    	}
    	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;  
        }  
    }
    
  11. BaseDao.java
    package com.ssh.dao;
    import org.hibernate.HibernateException;  
    public interface  BaseDao {
    	/*
    	 *插入数据
    	 */
    	public void saveObject(Object obj) throws HibernateException;  
    //	public void selectObject(Object obj) throws HibernateException;  
    //	public void deleteObject(Object obj) throws HibernateException;
    //	public void updateObject(Object obj) throws HibernateException; 
    }
    
  12. UserDao.java
    package com.ssh.dao.impl;
    
    import org.hibernate.HibernateException;
    import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
    
    import com.ssh.dao.BaseDao;
    
    public class UserDao extends HibernateDaoSupport implements BaseDao{
        @Override  
        public void saveObject(Object obj) throws HibernateException {  
        	getHibernateTemplate().save(obj);
        }  
    //    @Override  
    //    public void select(Object obj) throws HibernateException {  
    //        session.save(obj);  
    //    } 
    }
    
  13. UserForm.java
    package com.ssh.forms;
    
    public class UserForm {
    	private String userId;
    	private String username;  
        private String password;  
        public String getUserId() {
    		return userId;
    	}
    	public void setUserId(String userId) {
    		this.userId = userId;
    	}
        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;  
        }  
    }
    
  14. UserManager.java
    package com.ssh.service;
    
    import com.ssh.forms.UserForm;
    
    public interface  UserManager {
    	public void regUser(UserForm user);  
    }
    
  15. UserManagerImpl.java
    package com.ssh.service.impl;
    
    import org.hibernate.HibernateException;
    import org.springframework.beans.BeanUtils;
    import com.ssh.beans.User;
    import com.ssh.dao.BaseDao;
    import com.ssh.forms.UserForm;
    import com.ssh.service.UserManager;
    
    public class UserManagerImpl implements UserManager {
    	private BaseDao dao;
    	public void setDao(BaseDao dao){
    		this.dao=dao;
    	}
    	@Override  
        public void regUser(UserForm userForm) throws HibernateException {  
            // 构造User对象  
            User user = new User(); 
            BeanUtils.copyProperties(userForm,user);
            // 保存User对象  
            dao.saveObject(user);  
        }  
    }
    
  16. 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>
    <include file="struts-default.xml"></include>
    <constant name="struts.devMode" value="true" /><!-- 开发模式 -->
        <package name="default"  extends="struts-default" namespace="/">
            <action name="login" class="loginAction" >
                <result name="success">/welcome.jsp</result>
                <result name="error">/error.jsp</result>
                <result name="input">/input.jsp</result>
            </action>
        </package>
    </struts>
  17. applicationContext.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="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"  
            destroy-method="close">  
            <property name="driverClass">  
                <value>oracle.jdbc.driver.OracleDriver</value>  
            </property>  
            <property name="jdbcUrl">  
                <value>jdbc:oracle:thin:@199.10.80.42:1521:orcl</value>  
            </property>  
            <property name="user">  
                <value>dm</value>  
            </property>  
            <property name="password">  
                <value>dm</value>  
            </property>  
            <property name="maxPoolSize">  
                <value>80</value>  
            </property>  
            <property name="minPoolSize">  
                <value>1</value>  
            </property>  
            <property name="initialPoolSize">  
                <value>1</value>  
            </property>  
            <property name="maxIdleTime">  
                <value>20</value>  
            </property>  
        </bean>  
      
        <!--定义Hibernate的SessionFactory -->  
        <!-- SessionFactory使用的数据源为上面的数据源 -->  
        <!-- 指定了Hibernate的映射文件和配置信息 -->  
        <bean id="sessionFactory"  
            class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">  
            <property name="dataSource">  
                <ref local="dataSource" />  
            </property>  
            <property name="mappingResources">  
                <list>  
                    <value>com/ssh/beans/User.hbm.xml</value>  
                </list>  
            </property>  
            <property name="hibernateProperties">  
                <props>  
                    <prop key="hibernate.dialect">org.hibernate.dialect.Oracle9Dialect</prop>  
                    <prop key="show_sql">true</prop>  
                    <prop key="hibernate.jdbc.batch_size">20</prop>  
                </props>  
            </property>  
        </bean>  
      
        <bean id="transactionManager"  
            class="org.springframework.orm.hibernate3.HibernateTransactionManager">  
            <property name="sessionFactory" ref="sessionFactory" />  
        </bean>  
      
        <bean id="baseDao" class="com.ssh.dao.impl.UserDao">  
            <property name="sessionFactory">  
                <ref bean="sessionFactory" />  
            </property>  
        </bean>  
      
        <!--用户注册业务逻辑类 -->  
        <bean id="userManager" class="com.ssh.service.impl.UserManagerImpl">  
            <property name="dao">  
                <ref bean="baseDao" />  
            </property>  
        </bean>  
      
        <!-- 用户注册的Action -->  
        <bean id="loginAction" class="com.ssh.action.LoginAction">  
            <property name="userManager">  
                <ref bean="userManager" />  
            </property>  
        </bean>  
      
        <!-- more bean definitions go here -->  
      
    </beans>  
  18. web.xml
    <?xml version="1.0" encoding="UTF-8"?>
    <web-app version="3.0" 
    	xmlns="http://java.sun.com/xml/ns/javaee" 
    	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    	xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
    	http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
      <display-name></display-name> 	
      <welcome-file-list>
        <welcome-file>index.jsp</welcome-file>
      </welcome-file-list>
      <filter>
           <filter-name>struts2</filter-name>
           <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
       </filter>
       <filter-mapping>
           <filter-name>struts2</filter-name>
           <url-pattern>/*</url-pattern>
       </filter-mapping>
       <listener>  
            <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>  
       </listener> 
       <!-- 指定 applicationContext.xml的路徑
       <context-param>
    	   <param-name>applicationContext.xml</param-name>
    	   <param-value>applicationContext.xml</param-value>
       </context-param>
       -->
    </web-app>
    
  19. index.jsp
    <%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
    <%
    String path = request.getContextPath();
    String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
    %>
    <%@ taglib uri="/struts-tags" prefix="s" %>
    <!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>
       <s:form action="login" method="post">
        <s:label value="系统登陆"></s:label>
        <s:textfield name="user.username" label="账号" />
        <s:password name="user.password" label="密码" />
        <s:submit value="登录" />
       </s:form>
      </body>
    </html>
    
  20. input.jsp
    <%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
    <%
    String path = request.getContextPath();
    String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
    %>
    <%@ taglib uri="/struts-tags" prefix="s" %>
    <!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>
       <s:form action="login" method="post">
        <s:label value="系统再登陆"></s:label>
        <s:textfield name="user.username" label="账号" />
        <s:password name="user.password" label="密码" />
        <s:submit value="登录" />
       </s:form>
      </body>
    </html>
    
  21. welcome.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 'success.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>
        successfully,欢迎${user.username}!
      </body>
    </html>
    
  22. error.jsp未变
  23. 下面我们运行这个工程如图:

  24. 数据库截图:
  25. 以上就大工告成了。



  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值