基于Eclipse搭建SSH框架:第四篇 使用SSH开发一个小程序

这篇博客详细讲解了如何使用SSH(Struts2、Spring、Hibernate)框架开发一个用户注册程序。在开发过程中遇到了关于hibernate的ClassNotFound异常,通过降级到Hibernate3.6.7解决了问题。步骤包括:创建数据库和表、定义实体和映射文件、实现DAO接口及操作、创建注册Action类、配置web.xml,最后展示register.jsp和success2.jsp内容,以及运行和查看数据库结果。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

本文将介绍如何使用SSH框架开发一个简单的用户注册程序。

注:由于在开发本注册程序的时候,报出了 java.lang.ClassNotFoundException: org.hibernate.service.jta.platform.spi.JtaPlatform等错误,网上说的是spring3与hibernate4的整合的确会有错误。,所以本项目中使用的hibernate修改为使用hibernate3.6.7


1.在mysql数据库中创建数据库test ,并在test中创建user数据表



2.在src目录下创建com.integration.entity包,然后在该包下创建持久化类以及映射文件

package com.integration.entity;

public class User implements java.io.Serializable{

	private int id;
	private String name;
	private String password;
	
	public User(){
		
		
		
	}
	public User(int id, String name, String password) {
		this.id = id;
		this.name = name;
		this.password = password;
		
	}
	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getPassword() {
		return password;
	}
	public void setPassword(String password) {
		this.password = password;
	}
	
	
}
<?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>  
   <class name="com.hibernate.entity.User" table="user">  
      <id name="id" column="user_id" type="java.lang.Integer">  
          <generator class="increment"/>  
      </id>  
      <property name="name" type="java.lang.String" column="name" length="20"></property>  
      <property name="password" type="java.lang.String" column="password" length="12"></property>
    </class>  
</hibernate-mapping>   

3.在src目录下创建com.integration.dao包,然后在该包下创建DAO接口UserDAO.java、DAO实现类UserDAOImpl.java

package com.integration.dao;
import com.integration.entity.User;
public interface UserDAO {

	void save(User user);
	
}

package com.integration.dao;

import org.hibernate.SessionFactory;
import org.springframework.orm.hibernate3.HibernateTemplate;

import com.integration.entity.User;

public class UserDAOImpl implements UserDAO {

	private SessionFactory sessionFactory;
	private HibernateTemplate hibernatetemplate;

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

	}

	private HibernateTemplate getHibernateTemplate() {
		if (hibernatetemplate == null)
			hibernatetemplate = new HibernateTemplate(sessionFactory);
		System.out.println("here");
		return hibernatetemplate;
	}
	
	@Override
	public void save(User user){
		
		getHibernateTemplate().save(user);
   	
	}
	

}
这里为了简单起见,只编写并实现了save方法。

4.在src目录下创建com.integration.action包,然后在该包下创建RegisterAction类,如下:

package com.integration.action;

import com.integration.dao.UserDAO;
import com.integration.entity.User;
import com.opensymphony.xwork2.ActionSupport;

public class RegisterAction extends ActionSupport{

	private String userName;
	private String pwd;
	private UserDAO userDAO;
	public UserDAO getUserDAO() {
		return userDAO;
	}
	public void setUserDAO(UserDAO userDAO) {
		this.userDAO = userDAO;
	}
	public String getUserName() {
		return userName;
	}
	public void setUserName(String userName) {
		this.userName = userName;
	}
	public String getPwd() {
		return pwd;
	}
	public void setPwd(String pwd) {
		this.pwd = pwd;
	}
	
	public String execute() throws Exception{
	    User user=new User();
	    user.setId(1);
	    user.setName(this.userName);
	    user.setPassword(this.pwd);
		userDAO.save(user);
		return "success";
	}
}
 5.配置文件的内容如下:

    5.1web.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"
	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">
	 <display-name>S2SH</display-name>
	 <context-param> 
	 	<param-name>contextConfigLocation</param-name> 
		<param-value>/WEB-INF/classes/applicationContext.xml</param-value>
	 </context-param> 
		<listener> 
		    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> 
		</listener> 
	<filter>
		<!-- Filter名字 -->
		<filter-name>struts2</filter-name>
		<!-- Filter入口 -->
		<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
	</filter>
	<filter-mapping>
		<!-- Filter名字 -->
		<filter-name>struts2</filter-name>
		<!-- 截获的URL -->
		<url-pattern>/*</url-pattern>
	</filter-mapping>


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

       5.2 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.xml文件的根元素 -->
<struts>
	<constant name="struts.i18n.encoding" value="UTF-8"></constant>
<!-- 	<constant name="struts.objectFactory" value="spring" />
  - --> 
	<!-- 定义包 -->
	<package name="default" namespace="/" extends="struts-default">

		
		<action name="register" class="registerService">
			<result name="success">/success2.jsp</result>
		</action>
		 
	
	</package>

</struts>

       5.3applicationContext.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"
	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/sping-aop-3.0.xsd">





	<!-- 定义数据源org.springframework.jdbc.dataSource.DriverManagerDataSource -->
	<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
		<!-- 指定连接数据库的驱动 -->
		<property name="driverClassName">
			<value>com.mysql.jdbc.Driver</value>
		</property>
		<!-- 指定连接数据库的URL -->
		<property name="url">
			<value>jdbc:mysql://localhost:3306/test</value>
		</property>

		<!-- 指定连接数据库的用户名 -->
		<property name="username">
			<value>root</value>
		</property>
		<!-- 指定连接数据库的密码 -->
		<property name="password">
			<value>toor</value>
		</property>
	</bean>

	<!-- 定义Hibernare的sessionFactory -->
	<bean id="sessionFactory"
		class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
		<!-- 依赖注入已配置好的数据源dataSource -->
		<property name="dataSource">
			<ref local="dataSource" />
		</property>
		<!-- 指定Hibernate所有映射文件的路径 -->
		<property name="mappingResources">
			<list>
		  <value>com/integration/entity/User.hbm.xml</value>
			</list>
		</property>

		<!-- 设置Hibernate的属性 -->
		<property name="hibernateProperties">
			<props>
				<!-- 配置Hibernate的数据库方言 -->
				<prop key="hibernate.dialect">
					org.hibernate.dialect.MySQLDialect
				</prop>
				<!-- 设置是否在控制台输出由Hibernate生成的SQL语句 -->

				<prop key="show_sql">true</prop>
			</props>

		</property>
	</bean>
        	<bean id="userDAO" class="com.integration.dao.UserDAOImpl">
		<property name="sessionFactory">
			<ref local="sessionFactory" />
		</property>

	</bean>
	<bean id="registerService" class="com.integration.action.RegisterAction">
	    <property name="userDAO">
	       <ref local="userDAO"/>
	    </property>
	</bean>

</beans>

  6.项目概览

  

7.register.jsp与success2.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>
<form name="input" action="register" method="get">
用户名: <input type="text" name="userName"><br/>
密码:<input type="password" name="pwd"><br/>

<input type="submit" value="提交">
</form> 
</body>
</html>
<%@ 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>
注册成功了!!
</body>
</html>

success.jsp用不到。


8.运行项目

 

      在地址栏输入


     输入信息并提交

   

      

      看一下数据库

      

      至此,如何使用SSH开发程序就介绍完了!!

      完整的源代码在这里

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值