文档章节

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

ruyees
 ruyees
发布于 2014/10/05 11:48
字数 1084
阅读 11
收藏 0

第十七部分: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;
    } 
 
}

 

 

本文转载自:http://zuoqiang.iteye.com/blog/673372

共有 人打赏支持
ruyees
粉丝 3
博文 71
码字总数 0
作品 0
深圳
产品经理
基于Struts+Spring+Hibernate的使用和部署

就现在WEB开发而言,J2EE企业级开发技术比较成熟,开发模式模式也比较繁多,较流行开发模式MVC更是普遍,而基于MVC模式开发的框架也是种类繁多,现在使用最多的可能就是SSH这三种框架,所以展...

chenruibing
2015/08/18
0
0
struts+spring+hibernate分别用到了什么设计模式

struts+spring+hibernate分别用到了什么设计模式,请大家知无不答,共同讨论。 并举例说明哪个框架的哪个地方用到了哪个java设计模式.

skyone
2012/12/27
3K
14
登陆界面不输密码点一次登陆出现一个用户名和密码不能为空(点n个出现n个)

环境ssh(struts+spring+hibernate) 编写好登陆界面什么都正常,就是点击登陆时无限的出现用户名和密码不能为空 错误原因:没有在action中配置多例,action默认是单例的 scope=“prototype...

lzw_me
2014/05/29
0
0
想学习Struts+Spring+Hibernate给推荐本这样的书吧

想学习Struts+Spring+Hibernate 的整合开发 大家给推荐本这样的书吧 最好是新版的 谢谢了

帅气的鱼
2010/12/16
709
20
ssh三大框架对于新手先学哪个比较好点

学java的,但没有接触过ssh( struts+spring+hibernate),想问一句,先学哪个比较好点?推荐一些好点的书籍或视频也行,

奋斗的少年
2015/04/08
7.8K
20

没有更多内容

加载失败,请刷新页面

加载更多

Win10内部更新:警告用户别用chrome和Firefox

简评:别和 Chrome 和 Firefox 约行不,我 Edge 明明更美、屁股更翘、更性感。。。 微软正在测试 Windows 10 的一个更新:警告用户不要安装 Chrome 和 Firefox。是测试人员发现的这个新警告,...

极光推送
27分钟前
2
0
Java并发编程高阶技术 高性能并发框架源码解析与实战

全网唯一深度解析并发编程框架disruptor底层源码课程,助你成为并发编程高手,拿下高薪 网盘地址下载

qq__2304636824
今天
1
0
day92-20180918-英语流利阅读-待学习

健身最大的敌人不是懒惰,而是逞强 Daniel 2018-09-19 1.今日导读 还记得 2008 年北京奥运会运动员刘翔的退赛风波吗?那天几乎所有中国人都将视线聚焦在了鸟巢体育馆 110 米栏的项目上,迫不...

飞鱼说编程
今天
8
0
70.shell的函数 数组 告警系统需求分析

20.16/20.17 shell中的函数 20.18 shell中的数组 20.19 告警系统需求分析 20.16/20.17 shell中的函数: ~1. 函数就是把一段代码整理到了一个小单元中,并给这个小单元起一个名字,当用到这段...

王鑫linux
今天
3
0
分布式框架spring-session实现session一致性使用问题

前言:项目中使用到spring-session来缓存用户信息,保证服务之间session一致性,但是获取session信息为什么不能再服务层获取? 一、spring-session实现session一致性方式 用户每一次请求都会...

WALK_MAN
今天
7
0

没有更多内容

加载失败,请刷新页面

加载更多

返回顶部
顶部