三个JAVA文件完成你的MVC应用

只需要你三个JAVA类和两个配置文件,就可以完成你的MVC应用,我们就做一个示例吧。


本文以一个登录为例。

第一个JAVA文件

<!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>-->
/** ============================================================
 * 版权: Antbee 版权所有 (c) 2002 - 2006
 * 文件: com.antbee.sysmanage.login.action.UserAction
 * 所含类: UserAction
 * 修改记录:
 * 日期                作者                内容
 * =============================================================
 * 2006-8-28            和巍雅             建立文件
 * ============================================================
*/

package  com.antbee.sysmanage.login.action;

import  java.util.List;

import  javax.servlet.http.HttpServletRequest;
import  javax.servlet.http.HttpServletResponse;
import  javax.servlet.http.HttpSession;

import  org.apache.struts.action.ActionForm;
import  org.apache.struts.action.ActionForward;
import  org.apache.struts.action.ActionMapping;
import  org.apache.struts.actions.DispatchAction;
import  org.apache.struts.validator.DynaValidatorForm;
import  com.antbee.hibernate.model.AirlineUserInformation;
import  com.antbee.sysmanage.login.dao.UserDAO;

/**
 * <p>
 * </p>
 *
 * <p>
 * Copyright: 版权所有 (c) 2002 - 2006
 * </p>
 * <p>
 * Company: antbee
 * </p>
 *
 * 
@author  @家军
 * 
@version  1.0
 
*/

public   class  UserAction  extends  DispatchAction {
    
//  依赖注入
     private  UserDAO userDAO;

    
public  UserDAO getUserDAO() {
        
return  userDAO;
    }

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

    
/**
     *
     * 用户登录
     *
     * 
@param
     * 
@return
     * 
@throws
     
*/
    
public  ActionForward Login(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            
throws  Exception {
        HttpSession session 
=  request.getSession();
        DynaValidatorForm LoginActionForm 
=  (DynaValidatorForm) form;
        AirlineUserInformation User 
=  (AirlineUserInformation) LoginActionForm
                .get(
" User " );
        
if  (User  !=   null ) {
            String MDpwd 
=  CommonTools.encrypt(User.getPassword());
            
if  (userDAO.Login(User.getLoginName(), MDpwd)) {
                
//  成功登录
                
//  加载一些session
                AirlineUserInformation LegalUser  =  userDAO.GetUser(User
                        .getLoginName());
                String LoginName 
=  LegalUser.getLoginName();
                String UserName 
=  LegalUser.getUserName();
                String UserId 
=  LegalUser.getUserId().toString();
                String UserType 
=  LegalUser.getUserType().toString();
                String DeptId 
=  LegalUser.getAirlineDepartmentInformation()
                        .getDepartmentId().toString();
                String DeptName 
=  LegalUser.getAirlineDepartmentInformation().getDepartmentName();
                String RoleId 
=  LegalUser.getSysRoleInfo().getRoleId()
                        .toString();
                session.setAttribute(
" LoginName " , LoginName); //  登录名
                session.setAttribute( " UserName " , UserName); //  用户姓名
                session.setAttribute( " UserId " , UserId); //  用户ID
                session.setAttribute( " DeptId " , DeptId); //  部门ID
                session.setAttribute( " DeptName " , DeptName); //  部门DeptName
                session.setAttribute( " UserType " , UserType); //  用户类别
                session.setAttribute( " RoleId " , RoleId); //  角色ID
                 return  mapping.findForward( " Comein " );
            }
        }
        request.setAttribute(
" UserMsg " " User.LawLess " );
        
return  mapping.findForward( " Fail " );
    }

    
/**
     *
     * 用户退出
     *
     * 
@param
     * 
@return
     * 
@throws
     
*/
    
public  ActionForward LogOut(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            
throws  Exception {
        HttpSession session 
=  request.getSession();
        session.removeAttribute(
" LoginName " ); //  登录名
        session.removeAttribute( " UserName " ); //  用户姓名
        session.removeAttribute( " UserId " ); //  用户ID
        session.removeAttribute( " DeptId " ); //  部门ID
        session.removeAttribute( " UserType " ); //  用户类别
        session.removeAttribute( " RoleId " ); //  角色ID
        session.invalidate(); //  控件无效
         return  mapping.findForward( " LogOut " );
    }


第二个JAVA文件:

<!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>-->
/** ============================================================
 * 版权: antbee 版权所有 (c) 2002 - 2006
 * 文件: com.antbee.kams.sysmanage.login.dao.UserDAO
 * 所含类: IUserDAO
 * 修改记录:
 * 日期                作者                内容
 * =============================================================
 * 2006-8-28           @家军             建立文件
 * ============================================================
*/

package  com.antbee.sysmanage.login.dao;

import  java.util.List;

import  com.antbee.hibernate.model.AirlineUserInformation;

/**
 * <p></p>
 *
 * <p>Copyright: 版权所有 (c) 2002 - 2006</p>
 * <p>Company: antbee</p>
 *
 * 
@author  @家军
 * 
@version  1.0
 
*/

public   interface  UserDAO {
    
// 用户登录 如果是正确的用户名/密码,则返回true,否则返回false
     public   boolean  Login(String UserName,String PassWord);
    
// 取得用户信息
     public  AirlineUserInformation GetUser(String LoginName);
}


第三个JAVA文件;
<!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>-->
/** ============================================================
 * 版权: antbee 版权所有 (c) 2002 - 2006
 * 文件: com.antbee.sysmanage.login.dao.hibernate.UserDAOImpl
 * 所含类: UserDAOImpl
 * 修改记录:
 * 日期                作者                内容
 * =============================================================
 * 2006-8-28            @家军             建立文件
 * ============================================================
*/

package  com.antbee.sysmanage.login.dao.hibernate;

import  java.util.ArrayList;
import  java.util.Collections;
import  java.util.Comparator;
import  java.util.Iterator;
import  java.util.List;
import  java.util.Set;

import  org.springframework.orm.ObjectRetrievalFailureException;
import  org.springframework.orm.hibernate3.support.HibernateDaoSupport;


import  com.antbee.hibernate.model.AirlineUserInformation;
import  com.travelsky.kams.sysmanage.login.dao.UserDAO;

/**
 * <p>
 * </p>
 *
 * <p>
 * Copyright: 版权所有 (c) 2002 - 2006
 * </p>
 * <p>
 * Company: antbee
 * </p>
 *
 * 
@author  Administrator
 * 
@version  1.0
 
*/

public   class  UserDAOImpl  extends  HibernateDaoSupport  implements  UserDAO {

    
/*
     * (非 Javadoc)
     *
     * @see com.travelsky.kams.sysmanage.login.dao.IUserDAO#Login(java.lang.String,
     *      java.lang.String) 用户登录 如果是正确的用户名/密码,则返回true,否则返回false
     
*/
    
public   boolean  Login(String UserName, String PassWord) {
        
//  TODO Auto-generated method stub
        String finduser  =   " from AirlineUserInformation as user where user.loginName=' "
                
+  UserName  +   " ' and user.password=' "   +  PassWord  +   " ' " ;
        List Lfinduser 
=   this .getHibernateTemplate().find(finduser);
        
if  (Lfinduser.size()  >=   1 ) {
            
return   true ;
        }
        
return   false ;
    }

    
/*
     * (非 Javadoc)
     *
     * @see com.travelsky.kams.sysmanage.login.dao.IUserDAO#Login(java.lang.String,
     *      java.lang.String) 取得用户信息
     
*/
    
public  AirlineUserInformation GetUser(String LoginName) {
        
//  TODO Auto-generated method stub
        AirlineUserInformation user  =   null ;
        String finduser 
=   " from AirlineUserInformation as user where user.loginName=' "
                
+  LoginName  +   " ' " ;
        List Lfinduser 
=   this .getHibernateTemplate().find(finduser);
        
if  (Lfinduser.size()  >=   1 ) {
            user 
=  (AirlineUserInformation) Lfinduser.get( 0 );
        } 
else  {
            
throw   new  ObjectRetrievalFailureException(
                    AirlineUserInformation.
class , LoginName);
        }
        
return  user;
    }
}


两个配置文件,
第一个struts-config.xml
<!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>-->
<? 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 >
    
< data-sources  />
    
<!--  动态验证FORM   -->
    
< form-beans >
        
< form-bean  name ="LoginActionForm"
            type
="org.apache.struts.validator.DynaValidatorForm" >
            
< form-property  name ="User"
                type
="com.antbee.hibernate.model.AirlineUserInformation"   />
        
</ form-bean >
    
</ form-beans >
    
<!--  全局异常  -->
    
< global-exceptions >
        
< exception  key ="error.required"
            type
="org.springframework.dao.DataAccessException"
            path
="/dataAccessFailure.jsp"   />
    
</ global-exceptions >
    
<!--  全局转向  -->
    
< global-forwards >
        
<!--  所有非法用户返回登录页面   -->
        
< forward  name ="LoginGo"  path ="/User.do?method=LogOut" ></ forward >
        
<!--  业务规则出错信息提示   -->
        
< forward  name ="Fail"  path ="/jsp/common/failed.jsp" ></ forward >
    
</ global-forwards >
    
<!--  分发控制  -->
    
< action-mappings >
        
< action  path ="/User"
            type
="org.springframework.web.struts.DelegatingActionProxy"
            name
="LoginActionForm"  scope ="request"  parameter ="method"
            validate
="false" >
            
< forward  name ="UserMenu"  path ="/jsp/leftMenu.jsp"   />
            
< forward  name ="Comein"  path ="/jsp/index.jsp"   />
            
< forward  name ="LogOut"  path ="/login.jsp"   />
        
</ action >         
    
</ action-mappings >
    
<!--  资源文件  -->
    
< message-resources  parameter ="resources.ApplicationResources"   />
    
<!--  使用Spring控制事务  -->
    
< plug-in
        
className ="org.springframework.web.struts.ContextLoaderPlugIn" >
        
< set-property  property ="contextConfigLocation"
            value
="classpath:applicationContext.xml"   />
    
</ plug-in >
    
<!--  使用表单验证共同类validator  -->
    
< plug-in  className ="org.apache.struts.validator.ValidatorPlugIn" >
        
< set-property  property ="pathnames"
            value
="/WEB-INF/validator-rules.xml, /WEB-INF/validation.xml"   />
    
</ plug-in >
</ struts-config >


第二个XML文件:
applicationContext.xml

<!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>-->
<? xml version="1.0" encoding="UTF-8" ?>
<! DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd" >

< beans >

    
<!-- Start of Session Factory  -->
    
< bean  id ="SessionFactory"
        class
="org.springframework.orm.hibernate3.LocalSessionFactoryBean" >
        
< property  name ="configLocation" >
            
< value > classpath:hibernate.cfg.xml </ value >
        
</ property >
        
< property  name ="hibernateProperties" >
            
< props >
                
< prop  key ="connection.provider_class" >
                    org.hibernate.connection.ProxoolConnectionProvider
                
</ prop >
                
< prop  key ="hibernate.dialect" >
                    org.hibernate.dialect.Oracle9Dialect
                
</ prop >
            
</ props >
        
</ property >
    
</ bean >
    
<!-- End of SessionFactory  -->

    
<!-- Start of TransactionManager  -->
    
< bean  id ="TransactionManager"
        class
="org.springframework.orm.hibernate3.HibernateTransactionManager"
        abstract
="false"  singleton ="true"  lazy-init ="default"
        autowire
="default"  dependency-check ="default" >
        
< property  name ="sessionFactory" >
            
< ref  bean ="SessionFactory"   />
        
</ property >
    
</ bean >
    
<!-- End of TransactionManager  -->
    
<!--  基本的DAO BEGIN -->
    
< bean  id ="BaseTransactionProxy"
        class
="org.springframework.transaction.interceptor.TransactionProxyFactoryBean"
        abstract
="true" >
        
< property  name ="transactionManager" >
            
< ref  bean ="TransactionManager"   />
        
</ property >
        
< property  name ="proxyTargetClass" >
            
< value > true </ value >
        
</ property >
        
< property  name ="transactionAttributes" >
            
< props >
                
< prop  key ="save*" > PROPAGATION_REQUIRED </ prop >
                
< prop  key ="remove*" > PROPAGATION_REQUIRED </ prop >
                
< prop  key ="*" > PROPAGATION_REQUIRED </ prop >
            
</ props >
        
</ property >
    
</ bean >
    
<!--  基本的DAO END  -->

    
<!-- Start of userDAO  -->
    
< bean  id ="userDAO"
        class
="com.antbee.sysmanage.login.dao.hibernate.UserDAOImpl" >
        
< property  name ="sessionFactory" >
            
< ref  local ="SessionFactory"   />
        
</ property >
    
</ bean >
    
< bean  id ="userDAOProxy"  parent ="BaseTransactionProxy"
        class
="org.springframework.transaction.interceptor.TransactionProxyFactoryBean" >
        
< property  name ="target" >
            
< ref  local ="userDAO"   />
        
</ property >
    
</ bean >
    
<!-- End of userDAO  -->
                

    
<!-- Start of UserAction  -->
    
< bean  name ="/User"
        class
="com.antbee.sysmanage.login.action.UserAction"  singleton ="false" >
        
< property  name ="userDAO" >
            
< ref  bean ="userDAOProxy"   />
        
</ property >
    
</ bean >
    
<!-- End of AddUserAction  -->

</ beans >


其它的POJO使用myeclise插件就会自动生成。

呵呵是不是开发很简单呢,能够节省你多少的工作量呀,如果有兴趣的同志可以共同探讨。

你可能感兴趣的:(java)