`
primer_2004
  • 浏览: 124501 次
  • 性别: Icon_minigender_1
  • 来自: 广州
社区版块
存档分类
最新评论

struts+spring+hibernate的web应用(4.1)

    博客分类:
  • Java
阅读更多

 Web层代码编写:

 我们还是先从主要的 action 开始吧。

在 com.game.products.web.actions 包中新建 ProductsAction ,这是一个 DispatchAction ,代码如下:
 
  1. package com.game.products.web.actions;   
  2.   
  3.  import  java.util.List;   
  4.   
  5.  import  javax.servlet.http.HttpServletRequest;   
  6.  import  javax.servlet.http.HttpServletResponse;   
  7.   
  8.  import  org.acegisecurity.AccessDeniedException;   
  9.  import  org.apache.struts.action.ActionForm;   
  10.  import  org.apache.struts.action.ActionForward;   
  11.  import  org.apache.struts.action.ActionMapping;   
  12.  import  org.apache.struts.actions.DispatchAction;   
  13.   
  14.  import  com.game.commons.Pager;   
  15.  import  com.game.commons.PagerService;   
  16.  import  com.game.products.model.Products;   
  17.  import  com.game.products.services.iface.ProductsService;   
  18.  import  com.game.products.web.forms.ProductsForm;   
  19.   
  20.   
  21.  public   class  ProductsAction  extends  DispatchAction   {   
  22.        
  23.      private  ProductsService productsService;   
  24.      private  PagerService pagerService;   
  25.        
  26.      /** */ /**     
  27.      * 显示所有信息   
  28.       */    
  29.      public  ActionForward doGetProducts(   
  30.             ActionMapping mapping,   
  31.             ActionForm form,   
  32.             HttpServletRequest req,   
  33.             HttpServletResponse res)  {   
  34.         String flag = req.getParameter( " flag " );   
  35.            
  36.          int  totalRows = productsService.getRows();   
  37.         String currentPage = req.getParameter( " currentPage " );   
  38.         String pagerMethod = req.getParameter( " pagerMethod " );   
  39.            
  40.         Pager pager = pagerService.getPager(currentPage, pagerMethod, totalRows);   
  41.            
  42.         List productsList = productsService.getProducts(pager.getPageSize(), pager.getStartRow());   
  43.            
  44.         req.setAttribute( " productsList " , productsList);   
  45.         req.setAttribute( " PAGER " , pager);   
  46.         req.setAttribute( " flag " , flag);   
  47.         req.setAttribute( " totalRows " , String.valueOf(totalRows));   
  48.            
  49.          return  mapping.findForward( " all " );   
  50.     }    
  51.        
  52.      /** */ /**     
  53.      * 显示一条信息   
  54.       */    
  55.      public  ActionForward doGetProduct(   
  56.             ActionMapping mapping,   
  57.             ActionForm form,   
  58.             HttpServletRequest req,   
  59.             HttpServletResponse res)  {   
  60.         String flag = req.getParameter( " flag " );   
  61.         String gameId = req.getParameter( " gameId " );   
  62.         String fieldname = "" ;   
  63.         String value = "" ;   
  64.          if (flag.equals( " 2 " ))  {   
  65.             fieldname = (String)req.getParameter( " fieldname " );   
  66.             value = (String)req.getParameter( " value " );   
  67.             req.setAttribute( " fieldname " , fieldname);   
  68.             req.setAttribute( " value " , value);   
  69.         }    
  70.            
  71.         Products pd = productsService.getProduct(gameId);   
  72.         req.setAttribute( " pd " , pd);   
  73.         req.setAttribute( " flag " , flag);   
  74.          return  mapping.findForward( " one " );   
  75.     }    
  76.        
  77.      /** */ /**     
  78.      * 添加信息页面   
  79.       */    
  80.      public  ActionForward doAddProductPage(   
  81.             ActionMapping mapping,   
  82.             ActionForm form,   
  83.             HttpServletRequest req,   
  84.             HttpServletResponse res)  {   
  85.         String flag = req.getParameter( " flag " );   
  86.         req.setAttribute( " flag " , flag);   
  87.         String fieldname = "" ;   
  88.         String value = "" ;   
  89.          if (flag.equals( " 2 " ))  {   
  90.             fieldname = (String)req.getParameter( " fieldname " );   
  91.             value = (String)req.getParameter( " value " );   
  92.             req.setAttribute( " fieldname " , fieldname);   
  93.             req.setAttribute( " value " , value);   
  94.         }    
  95.            
  96.         String maxid = productsService.getMaxID();   
  97.         req.setAttribute( " maxid " , maxid);   
  98.          return  mapping.findForward( " add " );   
  99.     }    
  100.        
  101.      /** */ /**     
  102.      * 添加信息   
  103.       */    
  104.      public  ActionForward doAddProduct(   
  105.             ActionMapping mapping,   
  106.             ActionForm form,   
  107.             HttpServletRequest req,   
  108.             HttpServletResponse res)  {   
  109.         ProductsForm pf = (ProductsForm)form;   
  110.         String flag = pf.getFlag();   
  111.         req.setAttribute( " flag " , flag);   
  112.         String fieldname = "" ;   
  113.         String value = "" ;   
  114.          if (flag.equals( " 2 " ))  {   
  115.             fieldname = pf.getFieldname();   
  116.             value = pf.getValue();   
  117.             req.setAttribute( " fieldname " , fieldname);   
  118.             req.setAttribute( " value " , value);   
  119.         }    
  120.            
  121.         Products pd = new  Products();   
  122.         pd.setGameCapacity(pf.getGameCapacity());   
  123.         pd.setGameId(pf.getGameId());   
  124.         pd.setGameMedia(pf.getGameMedia());   
  125.         pd.setGameNameCn(pf.getGameNameCn());   
  126.         pd.setGameNameEn(pf.getGameNameEn());   
  127.         pd.setGameVersion(pf.getGameVersion());   
  128.         pd.setGameCopyright(pf.getGameCopyright());   
  129.         pd.setGameContent(pf.getGameContent());   
  130.          if (pf.getGamePrice().equals( "" ))  {   
  131.             pd.setGamePrice( null );   
  132.         } else  {   
  133.             pd.setGamePrice(pf.getGamePrice());   
  134.         }    
  135.            
  136.          int  sign = 1 ;   
  137.          try  {   
  138.             productsService.addProduct(pd);   
  139.             sign = 1 ;   
  140.         } catch (Exception e)  {   
  141.             sign = 2 ;   
  142.         }    
  143.            
  144.          if (sign == 1 )  {   
  145.              return  mapping.findForward( " success " );   
  146.         } else  {   
  147.              return  mapping.findForward( " failure " );   
  148.         }    
  149.     }    
  150.        
  151.      /** */ /**     
  152.      * 修改信息   
  153.       */    
  154.      public  ActionForward doUpdateProduct(   
  155.             ActionMapping mapping,   
  156.             ActionForm form,   
  157.             HttpServletRequest req,   
  158.             HttpServletResponse res)  {   
  159.         ProductsForm pf = (ProductsForm)form;   
  160.         String gameId = pf.getGameId();   
  161.            
  162.         String flag = pf.getFlag();   
  163.         req.setAttribute( " flag " , flag);   
  164.         String fieldname = "" ;   
  165.         String value = "" ;   
  166.          if (flag.equals( " 2 " ))  {   
  167.             fieldname = pf.getFieldname();   
  168.             value = pf.getValue();   
  169.             req.setAttribute( " fieldname " , fieldname);   
  170.             req.setAttribute( " value " , value);   
  171.         }    
  172.            
  173.         Products pd = productsService.getProduct(gameId);   
  174.         pd.setGameCapacity(pf.getGameCapacity());   
  175.         pd.setGameId(pf.getGameId());   
  176.         pd.setGameMedia(pf.getGameMedia());   
  177.         pd.setGameNameCn(pf.getGameNameCn());   
  178.         pd.setGameNameEn(pf.getGameNameEn());   
  179.         pd.setGameVersion(pf.getGameVersion());   
  180.         pd.setGameCopyright(pf.getGameCopyright());   
  181.         pd.setGameContent(pf.getGameContent());   
  182.          if (pf.getGamePrice().equals( "" ))  {   
  183.             pd.setGamePrice( null );   
  184.         } else  {   
  185.             pd.setGamePrice(pf.getGamePrice());   
  186.         }    
  187.            
  188.          int  sign = 1 ;   
  189.          try  {   
  190.             productsService.updateProductd(pd);   
  191.             sign = 1 ;   
  192.         } catch (Exception e)  {   
  193.             sign = 2 ;   
  194.         }    
  195.            
  196.          if (sign == 1 )  {   
  197.              return  mapping.findForward( " success " );   
  198.         } else  {   
  199.              return  mapping.findForward( " failure " );   
  200.         }    
  201.     }    
  202.        
  203.      /** */ /**     
  204.      * 删除信息   
  205.       */    
  206.      public  ActionForward doDeleteProduct(   
  207.             ActionMapping mapping,   
  208.             ActionForm form,   
  209.             HttpServletRequest req,   
  210.             HttpServletResponse res)  {   
  211.         String flag = req.getParameter( " flag " );   
  212.         } else  {   
  213.              return  mapping.findForward( " failure " );   
  214.         }    
  215.     }    
  216.        
  217.      /** */ /**     
  218.      * 删除信息   
  219.       */    
  220.      public  ActionForward doDeleteProduct(   
  221.             ActionMapping mapping,   
  222.             ActionForm form,   
  223.             HttpServletRequest req,   
  224.             HttpServletResponse res)  {   
  225.     
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics