本示例是 Spring+Struts+Hibernate 整合来编写一个用户注册的简单示例 , 项目名称为 mySSH, 示例中连接的数据库为 MySQL, 使用前应在 MySQL 中创建一个数据库 mySSH,mySSH 中创建数据库表 user,SQL 语句如下 ,CREATE TABLE ‘user’(‘id’ VARCHAR(32)NOT MULL,’username’ VARCHAR(32) NOT NULL,’password’ VARCHAR(32) NOT NULL,PRIMARY KEY (‘id’))ENGINE=MYISAM; 示例的简单代码如下 :
< 一 > 在 mySSH/WEB-INF 目录下编写 web.xml
<? xml version = "1.0" encoding = "UTF-8" ?>
< web-app xmlns = "http://java.sun.com/xml/ns/j2ee"
xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance"
version = "2.4"
xsi:schemaLocation = "http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd" >
< servlet >
< servlet-name > actionServlet </ servlet-name >
< servlet-class > org.apache.struts.action.ActionServlet </ servlet-class >
< init-param >
< param-name > config </ param-name >
< param-value > /WEB-INF/struts-config.xml </ param-value >
</ init-param >
< load-on-startup > 1 </ load-on-startup >
</ servlet >
< servlet-mapping >
< servlet-name > actionServlet </ servlet-name >
< url-pattern > *.do </ url-pattern >
</ servlet-mapping >
</ web-app >
< 二 > 在 mySSH/WEB-INF/jsp 目录下编写用户注册页面 regedit.jsp
<%@ page contentType = "text/html;charset=GBK" %>
< html >
< head >< title > Spring,Struts 和 Hibernate 整合实例 </ title ></ head >
<%
String msg=request.getAttribute( "msg" )== null ? "" :(String)request.getAttribute( "msg" );
%>
< body > <%= msg %>
< form action = "/mySSH/regedit.do" method = "post" >
用户名 : < input type = "text" name = "name" value = "${user.username }"/><br>
密码 : < input type = "password" name = "password" value = "" />< br >
< input type = "submit" name = "method" value = " 提交 " />
</ form >
</ body >
</ html >
< 三 > 在 mySSH/WEB-INF/jsp 目录下编写用户注册成功页面 success.jsp
<%@ page contentType = "text/html;charset=GBK" %>
< html >
< head >< title > Spring,Struts 和 Hibernate 整合实例 </ title ></ head >
< body >
提交成功 : 你输入的用户名是 :${user.name }, 密码是 :${user.password }
</ body >
</ html >
< 四 > 在 com.gc.vo 包下编写 POJO---User.java
package com.gc.vo;
public class User {
private String id ;
private String username ;
private String password ;
public User(String username,String password){
this . username =username;
this . password =password;
}
public User(){
}
public String getId() {
return id ;
}
public void setId(String id) {
this . id = id;
}
public String getPassword() {
return password ;
}
public void setPassword(String password) {
this . password = password;
}
public String getUsername() {
return username ;
}
public void setUsername(String username) {
this . username = username;
}
}
< 五 > 在 com.gc.vo 包下编写映射文件 User.hbm.xml
<? xml version = "1.0" encoding = "UTF-8" ?>
<! DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 2.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd" >
< hibernate-mapping >
< class name = "com.gc.vo.User" table = "user" >
< id name = "id" type = "java.lang.String" >
< generator class = "uuid.hex" ></ generator >
</ id >
< property name = "username"
type = "java.lang.String"
column = "username"
not-null = "true"
length = "32" >
</ property >
< property name = "password"
type = "java.lang.String"
column = "password"
not-null = "true"
length = "32" >
</ property >
</ class >
</ hibernate-mapping >
< 六 > 在 com.gc.dao 包下编写接口类 UserDAO.java
package com.gc.dao;
import com.gc.vo.*;
public interface UserDAO {
// 新增用户
public abstract void createUser(User user);
// 修改用户
public abstract void updateUser(User user);
// 删除用户
public abstract void deleteUser(User user);
// 查询用户
public abstract User queryUser(String name);
}
< 七 > 在 com.gc.dao.impl 包下编写接口类 UserDAO 的实现类 UserDAOImpl.java
package com.gc.dao.impl;
import com.gc.dao.UserDAO;
import com.gc.vo.User;
import java.util.ArrayList;
import java.util.List;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import org.springframework.orm.toplink.SessionFactory;
public class UserDAOImpl extends HibernateDaoSupport implements UserDAO{
// 依赖注入 SessionFactory
private SessionFactory sessionFactory ;
// 定义查询 SQL 语气
private String SQL = "from user u where u.username=?" ;
// 新增用户
public void createUser(User user){
this .getHibernateTemplate().save(user);
}
// 修改用户
public void updateUser(User user){
this .getHibernateTemplate().update(user);
}
// 删除用户
public void deleteUser(User user){
this .getHibernateTemplate().delete(user);
}
// 查询用户
public User queryUser(String name){
List userList;
if ( this .getHibernateTemplate().find( SQL , name)== null )
userList= new ArrayList();
else
userList= this .getHibernateTemplate().find( SQL , name);
return (User)userList.get(0);
}
}
< 八 > 在 mySSH/WEB-INF 目录下编写 Struts 的配置文件 struts-config.xml
<? xml version = "1.0" encoding = "UTF-8" ?>
<! DOCTYPE struts-config PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 1.2//EN"
"http://struts.apache.org/dtds/struts-config_1_2.dtd" >
< struts-config >
<!-- 定义 formBean -->
< form-beans >
< form-bean name = "user" type = "com.gc.vo.User" />
</ form-beans >
< action-mappings >
< action path = "/regedit" type = "org.springframework.web.struts.DelegatingActionProxy" name = "user" >
< forward name = "success" path = "WEB-INF/jsp/success.jsp" ></ forward >
< forward name = "input" path = "WEB-INF/jsp/regedit.jsp" ></ forward >
</ action >
< action path = "/input" type = "org.apache.struts.actions.ForwardAction"
parameter = "/WEB-INF/jsp/regedit.jsp" >
</ action >
</ action-mappings >
<!-- 注册 Struts 插件 , 与 Spring 相结合 -->
< plug-in className = "org.springframework.web.struts.ContextLoaderPlugIn" >
< set-property property = "contextConfigLocation" value = "/WEB-INF/config.xml" />
</ plug-in >
</ struts-config >
< 九 > 在 mySSH/WEB-INF 目录下编写 Spring 的配置文件 config.xml
<? xml version = "1.0" encoding = "UTF-8" ?>
<! DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN"
"http://www.springframework.org/dtd/sring-beans.dtd" >
< beans >
< bean id = "regedit" class = "com.gc.service.impl.RegeditImpl" >
< property name = "userDAO" >
< ref local = "userDAO" />
</ property >
</ bean >
< bean name = "/regedit" class = "com.gc.action.RegeditAction" >
< property name = "regedit" >
< ref bean = "regedit" />
</ property >
</ bean >
< bean name = "/input" class = "com.gc.action.RegeditAction" >
< property name = "regedit" >
< ref bean = "regedit" />
</ property >
</ bean >
<!-- 定义数据源 -->
< bean id = "dataSource"
class = "org.apache.commons.dbcp.BasicDataSource"
destroy-method = "close" >
<!-- 设定驱动 -->
< property name = "driverClassName" >
< value > com.mysql.jdbc.Driver </ value >
</ property >
<!-- 设定 URL -->
< property name = "url" >
< value > jdbc:mysql://localhost:3306/mySSH </ value >
</ property >
<!-- 设定用户名 -->
< property name = "username" >
< value > tang </ value >
</ property >
<!-- 设定密码 -->
< property name = "password" >
< value > admin </ value >
</ property >
</ bean >
<!-- 和 Hibernate 联系起来 -->
< bean id = "sessionFactory"
class = "org.springframework.orm.hibernate3.LocalSessionFactoryBean" >
< property name = "dataSource" >
< ref bean = "dataSource" />
</ property >
< property name = "mappingResources" >
< list >
< value > com\gc\vo\User.hbm.xml </ value >
</ list >
</ property >
< property name = "hibernateProperties" >
< props >
< prop key = "hibernate.dialect" > org.hibernate.dialect.MySQLDialect </ prop >
< prop key = "hibernate.show_sql" > true </ prop >
</ props >
</ property >
</ bean >
<!-- 进行事务处理 -->
< bean id = "transactionManager"
class = "org.springframework.orm.hibernate3.HibernateTransactionManager" >
< property name = "sessionFactory" >
< ref bean = "sessionFactory" />
</ property >
</ bean >
<!-- 进行事务代理 -->
< bean id = "userDAOProxy"
class = "org.springframework.transaction.interceptor.TransactionProxyFactoryBean" >
< property name = "transactionManager" >
< ref bean = "transactionManager" />
</ property >
< property name = "target" >
< ref local = "userDAO" />
</ property >
</ bean >
< bean id = "userDAO" class = "com.gc.dao.impl.UserDAOImpl" >
< property name = "sessionFactory" >
< ref bean = "sessionFactory" />
</ property >
</ bean >
</ beans >
< 十 > 在 com.gc.action 包下编写控制器 RegeditAction.java
package com.gc.action;
import com.gc.vo.User;
import com.gc.service.Regedit;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
public class RegeditAction extends Action{
private Regedit regedit ;
// 依赖注入
public Regedit getRegedit() {
return regedit ;
}
public void setRegedit(Regedit regedit) {
this . regedit = regedit;
}
public ActionForward execute(ActionMapping mapping,
ActionForm form,HttpServletRequest request,
HttpServletResponse response) throws Exception{
// 保存页面提交的用户信息
getRegedit().saveUser((User)form);
request.setAttribute( "user" ,(User)form);
return mapping.findForward( "success" );
}
}
< 十一 > 在 com.gc.service 包下编写业务逻辑接口 Regedit.java
package com.gc.service;
import com.gc.vo.User;
public interface Regedit {
// 保存用户
public abstract void saveUser(User user);
}
< 十二 > 在 com.gc.service 包下编写具体的业务逻辑类 RegeditImpl.java
package com.gc.service.impl;
import com.gc.dao.UserDAO;
import com.gc.service.Regedit;
import com.gc.vo.User;
public class RegeditImpl implements Regedit{
private UserDAO userDao ;
// 保存用户
public void saveUser(User user){
userDao .createUser(user);
}
// 修改用户
public void updateUser(User user){
userDao .updateUser(user);
}
// 修改用户
public void deleteUser(User user){
userDao .deleteUser(user);
}
// 查询用户
public User queryUser(String username){
return userDao .queryUser(username);
}
// 依赖注入
public UserDAO getUserDao() {
return userDao ;
}
public void setUserDao(UserDAO userDao) {
this . userDao = userDao;
}
}
posted on 2007-08-21 10:28
冬天出走的猪 阅读(488)
评论(0) 编辑 收藏 所属分类:
j2ee