`

struts+spring+hibernate的web应用【6】

    博客分类:
  • ssh
阅读更多

第十七部分:Web 层代码量比较大,涉及的地方也比较多,考虑到文章过于庞大,所以分两篇写。


            我们还是先从主要的

action 开始吧。

                   com.game.products.web.actions 包中新建 ProductsAction ,这是一个 DispatchAction

代码如下:

package  com.game.products.web.actions;

 import  java.util.List;

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

 import  org.acegisecurity.AccessDeniedException;
 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  com.game.commons.Pager;
 import  com.game.commons.PagerService;
 import  com.game.products.model.Products;
 import  com.game.products.services.iface.ProductsService;
 import  com.game.products.web.forms.ProductsForm;


 public   class  ProductsAction  extends  DispatchAction   {
    
     private  ProductsService productsService;
     private  PagerService pagerService;
    
     /** */ /**  
     * 显示所有信息
      */ 
     public  ActionForward doGetProducts(
            ActionMapping mapping,
            ActionForm form,
            HttpServletRequest req,
            HttpServletResponse res)  {
        String flag = req.getParameter( " flag " );
        
         int  totalRows = productsService.getRows();
        String currentPage = req.getParameter( " currentPage " );
        String pagerMethod = req.getParameter( " pagerMethod " );
        
        Pager pager = pagerService.getPager(currentPage, pagerMethod, totalRows);
        
        List productsList = productsService.getProducts(pager.getPageSize(), pager.getStartRow());
        
        req.setAttribute( " productsList " , productsList);
        req.setAttribute( " PAGER " , pager);
        req.setAttribute( " flag " , flag);
        req.setAttribute( " totalRows " , String.valueOf(totalRows));
        
         return  mapping.findForward( " all " );
    } 
    
     /** */ /**  
     * 显示一条信息
      */ 
     public  ActionForward doGetProduct(
            ActionMapping mapping,
            ActionForm form,
            HttpServletRequest req,
            HttpServletResponse res)  {
        String flag = req.getParameter( " flag " );
        String gameId = req.getParameter( " gameId " );
        String fieldname = "" ;
        String value = "" ;
         if (flag.equals( " 2 " ))  {
            fieldname = (String)req.getParameter( " fieldname " );
            value = (String)req.getParameter( " value " );
            req.setAttribute( " fieldname " , fieldname);
            req.setAttribute( " value " , value);
        } 
        
        Products pd = productsService.getProduct(gameId);
        req.setAttribute( " pd " , pd);
        req.setAttribute( " flag " , flag);
         return  mapping.findForward( " one " );
    } 
    
     /** */ /**  
     * 添加信息页面
      */ 
     public  ActionForward doAddProductPage(
            ActionMapping mapping,
            ActionForm form,
            HttpServletRequest req,
            HttpServletResponse res)  {
        String flag = req.getParameter( " flag " );
        req.setAttribute( " flag " , flag);
        String fieldname = "" ;
        String value = "" ;
         if (flag.equals( " 2 " ))  {
            fieldname = (String)req.getParameter( " fieldname " );
            value = (String)req.getParameter( " value " );
            req.setAttribute( " fieldname " , fieldname);
            req.setAttribute( " value " , value);
        } 
        
        String maxid = productsService.getMaxID();
        req.setAttribute( " maxid " , maxid);
         return  mapping.findForward( " add " );
    } 
    
     /** */ /**  
     * 添加信息
      */ 
     public  ActionForward doAddProduct(
            ActionMapping mapping,
            ActionForm form,
            HttpServletRequest req,
            HttpServletResponse res)  {
        ProductsForm pf = (ProductsForm)form;
        String flag = pf.getFlag();
        req.setAttribute( " flag " , flag);
        String fieldname = "" ;
        String value = "" ;
         if (flag.equals( " 2 " ))  {
            fieldname = pf.getFieldname();
            value = pf.getValue();
            req.setAttribute( " fieldname " , fieldname);
            req.setAttribute( " value " , value);
        } 
        
        Products pd = new  Products();
        pd.setGameCapacity(pf.getGameCapacity());
        pd.setGameId(pf.getGameId());
        pd.setGameMedia(pf.getGameMedia());
        pd.setGameNameCn(pf.getGameNameCn());
        pd.setGameNameEn(pf.getGameNameEn());
        pd.setGameVersion(pf.getGameVersion());
        pd.setGameCopyright(pf.getGameCopyright());
        pd.setGameContent(pf.getGameContent());
         if (pf.getGamePrice().equals( "" ))  {
            pd.setGamePrice( null );
        } else  {
            pd.setGamePrice(pf.getGamePrice());
        } 
        
         int  sign = 1 ;
         try  {
            productsService.addProduct(pd);
            sign = 1 ;
        } catch (Exception e)  {
            sign = 2 ;
        } 
        
         if (sign == 1 )  {
             return  mapping.findForward( " success " );
        } else  {
             return  mapping.findForward( " failure " );
        } 
    } 
    
     /** */ /**  
     * 修改信息
      */ 
     public  ActionForward doUpdateProduct(
            ActionMapping mapping,
            ActionForm form,
            HttpServletRequest req,
            HttpServletResponse res)  {
        ProductsForm pf = (ProductsForm)form;
        String gameId = pf.getGameId();
        
        String flag = pf.getFlag();
        req.setAttribute( " flag " , flag);
        String fieldname = "" ;
        String value = "" ;
         if (flag.equals( " 2 " ))  {
            fieldname = pf.getFieldname();
            value = pf.getValue();
            req.setAttribute( " fieldname " , fieldname);
            req.setAttribute( " value " , value);
        } 
        
        Products pd = productsService.getProduct(gameId);
        pd.setGameCapacity(pf.getGameCapacity());
        pd.setGameId(pf.getGameId());
        pd.setGameMedia(pf.getGameMedia());
        pd.setGameNameCn(pf.getGameNameCn());
        pd.setGameNameEn(pf.getGameNameEn());
        pd.setGameVersion(pf.getGameVersion());
        pd.setGameCopyright(pf.getGameCopyright());
        pd.setGameContent(pf.getGameContent());
         if (pf.getGamePrice().equals( "" ))  {
            pd.setGamePrice( null );
        } else  {
            pd.setGamePrice(pf.getGamePrice());
        } 
        
         int  sign = 1 ;
         try  {
            productsService.updateProductd(pd);
            sign = 1 ;
        } catch (Exception e)  {
            sign = 2 ;
        } 
        
         if (sign == 1 )  {
             return  mapping.findForward( " success " );
        } else  {
             return  mapping.findForward( " failure " );
        } 
    } 
    
     /** */ /**  
     * 删除信息
      */ 
     public  ActionForward doDeleteProduct(
            ActionMapping mapping,
            ActionForm form,
            HttpServletRequest req,
            HttpServletResponse res)  {
        String flag = req.getParameter( " flag " );
        req.setAttribute( " flag " , flag);
        String fieldname = "" ;
        String value = "" ;
         if (flag.equals( " 2 " ))  {
            fieldname = (String)req.getParameter( " fieldname " );
            value = (String)req.getParameter( " value " );
            req.setAttribute( " fieldname " , fieldname);
            req.setAttribute( " value " , value);
        } 
        
        String gameId = req.getParameter( " gameId " );
        
        Products pd = productsService.getProduct(gameId);
         int  sign = 1 ;
         try  {
            productsService.deleteProduct(pd);
            sign = 1 ;
        } catch (Exception e)  {
            sign = 2 ;
        } 
        
         if (sign == 1 )  {
             return  mapping.findForward( " success " );
        } else  {
             return  mapping.findForward( " failure " );
        } 
    } 
    
     /** */ /**  
     * 返回信息
      */ 
     public  ActionForward doReturnProduct(
            ActionMapping mapping,
            ActionForm form,
            HttpServletRequest req,
            HttpServletResponse res)  {
        String flag = req.getParameter( " flag " );
        req.setAttribute( " flag " , flag);
        String fieldname = "" ;
        String value = "" ;
         if (flag.equals( " 1 " ))  {
             int  totalRows = productsService.getRows();
            String currentPage = req.getParameter( " currentPage " );
            String pagerMethod = req.getParameter( " pagerMethod " );
            
            Pager pager = pagerService.getPager(currentPage, pagerMethod, totalRows);
            
            List productsList = productsService.getProducts(pager.getPageSize(), pager.getStartRow());
            
            req.setAttribute( " productsList " , productsList);
            req.setAttribute( " PAGER " , pager);
            req.setAttribute( " flag " , flag);
            req.setAttribute( " totalRows " , String.valueOf(totalRows));
        } else   if (flag.equals( " 2 " ))  {
            fieldname = (String)req.getParameter( " fieldname " );
            value = (String)req.getParameter( " value " );
             int  totalRows = productsService.getRows(fieldname,value);
            String currentPage = req.getParameter( " currentPage " );
            String pagerMethod = req.getParameter( " pagerMethod " );
            
            Pager pager = pagerService.getPager(currentPage, pagerMethod, totalRows);

            req.setAttribute( " fieldname " , fieldname);
            req.setAttribute( " value " , value);
            
            List productsList = productsService.queryProducts(fieldname, value,pager.getPageSize(), pager.getStartRow());
            
            req.setAttribute( " productsList " , productsList);
            req.setAttribute( " PAGER " , pager);
            req.setAttribute( " totalRows " , String.valueOf(totalRows));
        } 
        
         return  mapping.findForward( " all " );
        
    } 
    
     /** */ /**  
     * 查询信息
      */ 
     public  ActionForward doQueryProduct(
            ActionMapping mapping,
            ActionForm form,
            HttpServletRequest req,
            HttpServletResponse res)  {
        String flag = req.getParameter( " flag " );
        req.setAttribute( " flag " , flag);
        String fieldname = "" ;
        String value = "" ;
        fieldname = (String)req.getParameter( " fieldname " );
        value = (String)req.getParameter( " value " );
        
         int  totalRows = productsService.getRows(fieldname,value);
        String currentPage = req.getParameter( " currentPage " );
        String pagerMethod = req.getParameter( " pagerMethod " );
        
        Pager pager = pagerService.getPager(currentPage, pagerMethod, totalRows);

        req.setAttribute( " fieldname " , fieldname);
        req.setAttribute( " value " , value);
        
        List productsList = productsService.queryProducts(fieldname, value,pager.getPageSize(), pager.getStartRow());
        
        req.setAttribute( " productsList " , productsList);
        req.setAttribute( " PAGER " , pager);
        req.setAttribute( " totalRows " , String.valueOf(totalRows));
        
         return  mapping.findForward( " all " );
        
    } 
 
      public  ProductsService getProductsService()   {
         return  productsService;
    } 
 
      public   void  setProductsService(ProductsService productsService)   {
         this .productsService  =  productsService;
    } 
 
      public  PagerService getPagerService()   {
         return  pagerService;
    } 
 
      public   void  setPagerService(PagerService pagerService)   {
         this .pagerService  =  pagerService;
    } 
    
} 

 

 

第十八部分: com.game.products.web.forms 包中新建 ProductsForm ,他继承了 ValidatorForm 。代码如下:

 

 package  com.game.products.web.forms;

 import  javax.servlet.http.HttpServletRequest;

 import  org.apache.struts.action.ActionMapping;
 import  org.apache.struts.validator.ValidatorForm;

 public   class  ProductsForm  extends  ValidatorForm   {
     //     Fields  
      private  String gameId; // 编号 
      private  String gameNameCn; // 中文名称 
      private  String gameNameEn; // 英文名称 
      private  String gameCapacity; // 碟数 
      private  String gameVersion; // 版本 
      private  String gameMedia; // 介质 
      private  String gameCopyright; // 版权 
      private  String gamePrice; // 价格 
      private  String gameContent; // 攻略 
     
     private  String flag;
     private  String fieldname;
     private  String value;
    
     //     Constructors 
       public  ProductsForm()  {
        gameId = null ;
        gameNameCn = null ;
        gameNameEn = null ;
        gameCapacity = null ;
        gameVersion = null ;
        gameMedia = null ;
        gameCopyright = null ;
        gamePrice = null ;
        gameContent = null ;
        
        flag = null ;
        fieldname = null ;
        value = null ;
    } 
    
     //  reset 
       public   void  reset(ActionMapping mapping, HttpServletRequest request)   {
        gameId = null ;
        gameNameCn = null ;
        gameNameEn = null ;
        gameCapacity = null ;
        gameVersion = null ;
        gameMedia = null ;
        gameCopyright = null ;
        gamePrice = null ;
        gameContent = null ;
        
        flag = null ;
        fieldname = null ;
        value = null ;
    } 
    
     //     Property accessors 
       public  String getGameCapacity()   {
         return  gameCapacity;
    } 
 
      public   void  setGameCapacity(String gameCapacity)   {
         this .gameCapacity  =  gameCapacity;
    } 
 
      public  String getGameId()   {
         return  gameId;
    } 
 
      public   void  setGameId(String gameId)   {
         this .gameId  =  gameId;
    } 
 
      public  String getGameNameCn()   {
         return  gameNameCn;
    } 
 
      public   void  setGameNameCn(String gameNameCn)   {
         this .gameNameCn  =  gameNameCn;
    } 
 
      public  String getGameNameEn()   {
         return  gameNameEn;
    } 
 
      public   void  setGameNameEn(String gameNameEn)   {
         this .gameNameEn  =  gameNameEn;
    } 
 
      public  String getGameVersion()   {
         return  gameVersion;
    } 
 
      public   void  setGameVersion(String gameVersion)   {
         this .gameVersion  =  gameVersion;
    } 
 
      public  String getGameMedia()   {
         return  gameMedia;
    } 
 
      public   void  setGameMedia(String gameMedia)   {
         this .gameMedia  =  gameMedia;
    } 
 
      public  String getFieldname()   {
         return  fieldname;
    } 
 
      public   void  setFieldname(String fieldname)   {
         this .fieldname  =  fieldname;
    } 
 
      public  String getFlag()   {
         return  flag;
    } 
 
      public   void  setFlag(String flag)   {
         this .flag  =  flag;
    } 
 
      public  String getValue()   {
         return  value;
    } 
 
      public   void  setValue(String value)   {
         this .value  =  value;
    } 
 
      public  String getGameCopyright()   {
         return  gameCopyright;
    } 
 
      public   void  setGameCopyright(String gameCopyright)   {
         this .gameCopyright  =  gameCopyright;
    } 
 
      public  String getGameContent()   {
         return  gameContent;
    } 
 
      public   void  setGameContent(String gameContent)   {
         this .gameContent  =  gameContent;
    } 
 
      public  String getGamePrice()   {
         return  gamePrice;
    } 
 
      public   void  setGamePrice(String gamePrice)   {
         this .gamePrice  =  gamePrice;
    } 
 
} 
 

 

 

分享到:
评论

相关推荐

    struts1.2 + spring2.5 + hibernate3.2框架demo

    Struts1.2、Spring2.5和Hibernate3.2是经典的Java企业级开发框架组合,它们各自在应用程序的不同层次上发挥着重要作用。Struts1.2是一个MVC(Model-View-Controller)框架,主要负责处理用户界面与业务逻辑之间的...

    struts+spring+hibernate实现图书修改和删除

    在IT行业中,SSH(Struts + Spring + Hibernate)是一个经典的Java Web开发框架组合,用于构建高效、可扩展的Web应用程序。本项目通过SSH框架实现了图书管理系统的图书修改和删除功能,下面将详细介绍这三个核心组件...

    使用struts+spring+hibernate 组装web应用

    使用struts+spring+hibernate 组装web应用

    ssh(struts+spring+hibernate)教务管理信息系统(全部代码) 可以直接运行

    SSH(Struts+Spring+Hibernate)是一个经典的Java Web开发框架,用于构建高效、可扩展的企业级应用程序。这个教务管理信息系统是基于SSH框架实现的,提供了完整的源代码,可以直接运行,便于开发者学习和参考。 **...

    struts+spring+hibernate三大框架整合

    SSH三大框架,即Struts、Spring和Hibernate,是Java Web开发中的常用技术组合,它们各自负责不同的职责:Struts作为MVC模式中的Controller层,处理用户请求并转发到相应的业务逻辑;Spring则作为核心容器,负责依赖...

    struts2+spring2.5+hibernate3.2整合完整项目,带数据库脚本

    Struts2、Spring2.5和Hibernate3.2是Java Web开发中经典的三大框架,它们的整合使用在过去的许多年里被广泛应用于企业级应用系统。这个完整的项目提供了从开发环境到运行环境的所有必要组件,包括数据库脚本,使得...

    Struts+Spring+Hibernate开发实例.pdf

    总的来说,文档中提及的知识点包含了Struts、Spring和Hibernate三个框架的核心概念和实际应用。开发者需要对这些框架有深入的了解,并能够将它们整合在一起,设计出结构良好的Web应用。由于文档内容是通过OCR扫描的...

    最新项目系统:Struts2+Spring4+Hibernate4三大框架整合

    Struts2、Spring4和Hibernate4是Java Web开发中的三大主流框架,它们分别负责MVC模式中的表现层、业务层和服务层。这个最新的项目系统整合了这三个框架,旨在提供一个高效、灵活且易于维护的开发环境。下面将详细...

    SSH(Struts+Spring+Hibernate)结合项目简单实例

    SSH(Struts+Spring+Hibernate)是Java Web开发中一种常见的技术栈,它将MVC设计模式、依赖注入和持久层框架集成为一个整体,大大提高了开发效率和代码的可维护性。下面我们将深入探讨SSH框架的各个组成部分及其结合...

    struts2+spring4+hibernate

    Struts2、Spring4和Hibernate是Java开发中的三大框架,它们在构建企业级Web应用程序时起着核心作用。本教程将深入探讨这三个框架如何协同工作,以实现高效、灵活和可扩展的电子商务平台。 首先,Struts2是一个基于...

    图书管理系统spring+struts+hibernate

    《图书管理系统spring+struts+hibernate》是一款基于Java技术栈开发的图书管理软件,其核心框架包括Spring、...对于学习和理解Java Web开发,特别是Spring、Struts和Hibernate的集成应用,这是一个很好的实践案例。

    Struts+Spring+Hibernate+MySQL

    Struts、Spring、Hibernate以及MySQL是Java开发领域中非常经典的四大框架组合,它们共同构建了一个强大的Web应用程序架构。Struts提供了MVC(Model-View-Controller)设计模式的支持,Spring作为一个全面的轻量级...

    轻量级Java EE企业应用实战(第4版) Struts 2+Spring 4+Hibernate整合开发 光盘 源码

    《轻量级Java EE企业应用实战(第4版)》这本书深入探讨了Struts 2、Spring 4和Hibernate这三大框架的整合开发,旨在帮助读者掌握如何在实际项目中高效构建Java EE应用。SSH(Struts 2、Spring、Hibernate)是Java ...

    struts+spring+hibernate(mysql)用户登录及文件上传

    Struts、Spring 和 Hibernate 是Java Web开发中的三大框架,它们结合使用可以构建高效、模块化的应用程序,特别是对于处理用户登录和文件上传等常见功能。在这个项目中,"struts+spring+hibernate(mysql)用户登录及...

    论坛系统项目(Struts 2+Hibernate+Spring实现)

    论坛系统项目(Struts 2+Hibernate+Spring实现)论坛系统项目(Struts 2+Hibernate+Spring实现)论坛系统项目(Struts 2+Hibernate+Spring实现)论坛系统项目(Struts 2+Hibernate+Spring实现)论坛系统项目(Struts...

    struts+spring+hibernate

    Struts、Spring 和 Hibernate 是Java开发中非常著名的三个开源框架,它们组合在一起形成的SSH(Struts、Spring、Hibernate)框架,是企业级Web应用开发的常用解决方案。SSH框架的整合可以实现模型-视图-控制器(MVC...

    Struts2+Spring+Hibernate和Struts2+Spring+Ibatis

    Struts2+Spring+Hibernate和Struts2+Spring+Ibatis是两种常见的Java Web应用程序集成框架,它们分别基于ORM框架Hibernate和轻量级数据访问框架Ibatis。这两种框架结合Spring,旨在提供一个强大的、可扩展的、易于...

    Myeclipse8.5下搭建SSH框架(图解)Struts2.1+Spring3.0+Hibernate3.3

    SSH框架是指将Struts、Spring以及Hibernate这三个开源框架进行整合,以实现更加强大、灵活且可扩展的企业级应用开发。本文将详细介绍如何在MyEclipse 8.5环境下搭建基于Struts2.1、Spring3.0以及Hibernate3.3的SSH...

Global site tag (gtag.js) - Google Analytics