Struts2.0+Spring2.0+Hibernate3.1登录及增删改查的完整演示示例 连载中...(三)

18 篇文章 0 订阅

  好了,废话不多说了,接着今天早上我们的示例接着做完.
  六、我们开始写Spring层:
  1、 服务层接口IUsersService.java 

package  org.login.service;

import  java.util.List;

import  org.login.vo.Users;

@SuppressWarnings(
" unchecked " )
public   interface  IUsersService {

    
public   boolean  checkLogin(Users user);
    
    
public   boolean  saveUser(Users user);
    
    
public   boolean  deleteUser(Integer id);
    
    
public   boolean  updateUser(Users user);
    
    
public  List < Users >  findAllUsers();
    
    
public  Users getUserById(Integer id);
}
  2、服务接口实现层:UsersService.java
package  org.login.service.Impl;

import  java.util.List;

import  org.login.dao.IUsersDAO;
import  org.login.service.IUsersService;
import  org.login.vo.Users;

@SuppressWarnings(
" unchecked " )
public   class  UsersService  implements  IUsersService {

    
private  IUsersDAO userDAO  =   null ;
    
    
public  IUsersDAO getUserDAO() {
        
return  userDAO;
    }

    
public   void  setUserDAO(IUsersDAO userDAO) {
        
this .userDAO  =  userDAO;
    }

    @Override
    
public   boolean  checkLogin(Users user) {
        
        
return  userDAO.checkLogin(user);
    }

    @Override
    
public   boolean  deleteUser(Integer id) {
        
        
return  userDAO.deleteUser(id);
    }

    @Override
    
public   boolean  saveUser(Users user) {
        
        
return  userDAO.saveUser(user);
    }

    @Override
    
public   boolean  updateUser(Users user) {
        
        
return  userDAO.updateUser(user);
    }

    @Override
    
public  List < Users >  findAllUsers() {
        
        
return  userDAO.findAllUsers();
    }

    @Override
    
public  Users getUserById(Integer id) {
        
        
return  userDAO.getUserById(id);
    }

}

  七、现在我们开始写Struts层:
  1、写Struts层的Action:  Login.java

package  org.login.actions;

import  java.util.List;

import  javax.servlet.http.HttpServletRequest;

import  org.apache.struts2.interceptor.ServletRequestAware;
import  org.login.service.IUsersService;
import  org.login.vo.Users;

import  com.opensymphony.xwork2.ActionSupport;

@SuppressWarnings({
" serial " , " unchecked " })
public   class  Login  extends  ActionSupport  implements  ServletRequestAware {

    
private   static   final  String UPDATE  =   " update " ;
    
    
private  HttpServletRequest request  =   null ;
    
    
private  IUsersService userService  =   null ;
    
    
private  String id  =   null ;
    
private  String name  =   null ;
    
private  String password  =   null ;
    
    
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;
    }
    
    
public  String getId() {
        
return  id;
    }

    
public   void  setId(String id) {
        
this .id  =  id;
    }
    
    
public  IUsersService getUserService() {
        
return  userService;
    }

    
public   void  setUserService(IUsersService userService) {
        
this .userService  =  userService;
    }

    @Override
    
public  String execute()  throws  Exception {
        Users user 
=   new  Users();
        user.setUsername(name);
        user.setPassword(password);
        
if  (getUserService().checkLogin(user)) {
            
return  SUCCESS;
        }
        
return  INPUT;
    }
    
    
public  String save()  throws  Exception {
        Users user 
=   new  Users();
        user.setUsername(name);
        user.setPassword(password);
        
if  (getUserService().saveUser(user)) {
            
return  SUCCESS;
        }
        
return  ERROR;
    }

    
public  String update()  throws  Exception {
        Users user 
=   null ;
        String action 
=  request.getParameter( " action " );
        String id 
=  request.getParameter( " id " );
        
if  ( " loading " .equals(action)) {
            user 
=   new  Users();
            user 
=  getUserService().getUserById(Integer.valueOf(id));
            
if  ( null   !=  user) {
                request.setAttribute(
" user " , user);
            }
            
return  UPDATE;
            
        } 
else  {
            user 
=   new  Users();
            user.setUsername(name);
            user.setPassword(password);
            user.setId(Integer.valueOf(id));
            
if  (getUserService().updateUser(user)) {
                
return  SUCCESS;
            }
        }
        
        
return  ERROR;
    }
    
    
public  String delete()  throws  Exception {
        String id 
=  request.getParameter( " id " );
        
if  (getUserService().deleteUser(Integer.valueOf(id))) {
            
return  SUCCESS;
        }
        
return  ERROR;
    }
    
    
    
public  String findAllUsers()  throws  Exception {
        List
< Users >  list  =  getUserService().findAllUsers(); 
        
if  (list.size()  >   0 ) {
            request.setAttribute(
" list " , list);
            
return  SUCCESS;
        }
        
return  ERROR;
    }

    @Override
    
public   void  setServletRequest(HttpServletRequest request) {
        
this .request  =  request;        
    }
}
  
  好了,现在我们所有的实现类都已经完成了,剩下来的就是配置文件了。
  八、开始写配置文件了
  1、配置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" />
    
< package  name ="struts2"  extends ="struts-default" >
        
< action  name ="logon"  class ="login" >
            
< result  name ="success" > /success.jsp </ result >
            
< result  name ="input" > /login.jsp </ result >
        
</ action >
        
< action  name ="save"  class ="login"  method ="save" >
            
< result  name ="success" > /success.jsp </ result >
            
< result  name ="error" > /error.jsp </ result >
        
</ action >
        
< action  name ="update"  class ="login"  method ="update" >
            
< result  name ="success" > /success.jsp </ result >
            
< result  name ="update" > /update.jsp </ result >
            
< result  name ="error" > /error.jsp </ result >
        
</ action >
        
< action  name ="delete"  class ="login"  method ="delete" >
            
< result  name ="success" > /success.jsp </ result >
            
< result  name ="error" > /error.jsp </ result >
        
</ action >
        
< action  name ="show"  class ="login"  method ="findAllUsers" >
            
< result  name ="success" > /list.jsp </ result >
            
< result  name ="error" > /error.jsp </ result >
        
</ action >
    
</ package >
</ struts >
  2、配置struts.properties属性文件,以支持与Spring的集成
struts.locale = zh_CN
struts.i18n.encoding
= UTF- 8   
struts.objectFactory
= spring  
  注:增加struts.objectFactory=spring是为了支持与spring的集成,否则会出错
  3、配置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-2.0.xsd" >


    
< bean  id ="dataSource"
        class
="org.apache.commons.dbcp.BasicDataSource" >
        
< property  name ="driverClassName"
            value
="com.microsoft.jdbc.sqlserver.SQLServerDriver" >
        
</ property >
        
< property  name ="url"
            value
="jdbc:microsoft:sqlserver://localhost:1433;databaseName=pubs" >
        
</ property >
        
< property  name ="username"  value ="sa" ></ property >
        
< property  name ="password"  value ="aa" ></ property >
    
</ bean >
    
< bean  id ="sessionFactory"
        class
="org.springframework.orm.hibernate3.LocalSessionFactoryBean" >
        
< property  name ="dataSource" >
            
< ref  bean ="dataSource"   />
        
</ property >
        
< property  name ="hibernateProperties" >
            
< props >
                
< prop  key ="hibernate.dialect" >
                    org.hibernate.dialect.SQLServerDialect
                
</ prop >
            
</ props >
        
</ property >
        
< property  name ="mappingResources" >
            
< list >
                
< value > org/login/vo/Users.hbm.xml </ value >
            
</ list >
        
</ property >
    
</ bean >
    
< bean  id ="baseDAO"  class ="org.login.dao.Impl.BaseDAO" >
        
< property  name ="sessionFactory" >
            
< ref  bean ="sessionFactory" />
        
</ property >
    
</ bean >
    
    
< bean  id ="userDAO"  class ="org.login.dao.Impl.UsersDAO"  parent ="baseDAO" ></ bean >
    
< bean  id ="userService"  class ="org.login.service.Impl.UsersService" >
        
< property  name ="userDAO" >
            
< ref  bean ="userDAO" />
        
</ property >
    
</ bean >
    
    
< bean  id ="login"  class ="org.login.actions.Login"  scope ="prototype" >
        
< property  name ="userService" >
            
< ref  bean ="userService" />
        
</ property >
    
</ bean >
</ beans >
  
  这一篇也不少了,我们的工作也基本上完成了,就剩下UI层了,等下篇再说吧。好了,我们这一篇就到这里啦,下一篇请大家继续关注我的博客连载,大家也可以先下载示例代码先看看哈,如果有看不懂的给我留言。

  下载地址:
Struts2.0+Spring2.0+Hibernate3.1登录及增删改查的完整演示示例下载地址:http://download.csdn.net/source/326113

  以前的一个版本:Struts1.2+Spring1.2+HIbernate3.1整合登录示例示例:http://download.csdn.net/source/323772

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值