文档章节

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

ruyees
 ruyees
发布于 2014/10/05 11:48
字数 1084
阅读 11
收藏 0
点赞 0
评论 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
深圳
产品经理
JAVA SSH 框架介绍

SSH 为 struts+spring+hibernate 的一个集成框架,是目前较流行的一种JAVA Web应用程序开源框架。  Struts   Struts是一个基于Sun J2EE平台的MVC框架,主要是采用Servlet和JSP技术来实现的...

小梅菜鸟 ⋅ 2012/04/20 ⋅ 0

基于Struts+Spring+Hibernate的使用和部署

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

chenruibing ⋅ 2015/08/18 ⋅ 0

struts+spring+hibernate分别用到了什么设计模式

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

skyone ⋅ 2012/12/27 ⋅ 14

登陆界面不输密码点一次登陆出现一个用户名和密码不能为空(点n个出现n个)

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

lzw_me ⋅ 2014/05/29 ⋅ 0

想学习Struts+Spring+Hibernate给推荐本这样的书吧

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

帅气的鱼 ⋅ 2010/12/16 ⋅ 20

java-学习路线

学习路线 java基础 java基础 java界面编程 java API java语法 AWT java IO 面向对象 事件机制 多线程 常见api Swing 网络编程 集合框架 反射注解 数据库开发 SQL基础 JDBC编程 JDBC高级 SQL基...

zhaoliang1131 ⋅ 2017/01/17 ⋅ 0

excel数据导入导出

各位中午好! 我现在做一个旅游管理系统,遇到一些问题。 如何实现页面点击按钮将EXCEL数据导入到数据中,另外,也能实现点击按钮实现将数据库表导出到EXCEL中。 我用的开发语言是JAVA,数据...

汪斌 ⋅ 2011/03/31 ⋅ 4

异常org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name java.lang.Long

使用了struts+spring+hibernate 在注入的是的时候发生org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name java.lang.Long。大侠帮忙看看......

深蓝的天空 ⋅ 2014/02/11 ⋅ 0

ssh三大框架对于新手先学哪个比较好点

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

奋斗的少年 ⋅ 2015/04/08 ⋅ 20

SSH框架总结(框架分析+环境搭建+实例源码下载)

首先,SSH不是一个框架,而是多个框架(struts+spring+hibernate)的集成,是目前较流行的一种Web应用程序开源集成框架,用于构建灵活、易于扩展的多层Web应用程序。 集成SSH框架的系统从职责...

亚特兰缇斯 ⋅ 2015/03/04 ⋅ 0

没有更多内容

加载失败,请刷新页面

加载更多

下一页

volatile和synchronized的区别

volatile和synchronized的区别 在讲这个之前需要先了解下JMM(Java memory Model :java内存模型):并发过程中如何处理可见性、原子性、有序性的问题--建立JMM模型 详情请看:https://baike.b...

MarinJ_Shao ⋅ 41分钟前 ⋅ 0

深入分析Kubernetes Critical Pod(一)

Author: xidianwangtao@gmail.com 摘要:大家在部署Kubernetes集群AddOn组件的时候,经常会看到Annotation scheduler.alpha.kubernetes.io/critical-pod"="",以表示这是一个关键服务,那你知...

WaltonWang ⋅ 49分钟前 ⋅ 0

原子性 - synchronized关键词

原子性概念 原子性提供了程序的互斥操作,同一时刻只能有一个线程能对某块代码进行操作。 原子性的实现方式 在jdk中,原子性的实现方式主要分为: synchronized:关键词,它依赖于JVM,保证了同...

dotleo ⋅ 55分钟前 ⋅ 0

【2018.06.22学习笔记】【linux高级知识 14.4-15.3】

14.4 exportfs命令 14.5 NFS客户端问题 15.1 FTP介绍 15.2/15.3 使用vsftpd搭建ftp

lgsxp ⋅ 今天 ⋅ 0

JeeSite 4.0 功能权限管理基础(Shiro)

Shiro是Apache的一个开源框架,是一个权限管理的框架,实现用户认证、用户授权等。 只要有用户参与一般都要有权限管理,权限管理实现对用户访问系统的控制,按照安全规则或者安全策略控制用户...

ThinkGem ⋅ 昨天 ⋅ 0

python f-string 字符串格式化

主要内容 从Python 3.6开始,f-string是格式化字符串的一种很好的新方法。与其他格式化方式相比,它们不仅更易读,更简洁,不易出错,而且速度更快! 在本文的最后,您将了解如何以及为什么今...

阿豪boy ⋅ 昨天 ⋅ 0

Python实现自动登录站点

如果我们想要实现自动登录,那么我们就需要能够驱动浏览器(比如谷歌浏览器)来实现操作,ChromeDriver 刚好能够帮助我们这一点(非谷歌浏览器的驱动有所不同)。 一、确认软件版本 首先我们...

blackfoxya ⋅ 昨天 ⋅ 0

线性回归原理和实现基本认识

一:介绍 定义:线性回归在假设特证满足线性关系,根据给定的训练数据训练一个模型,并用此模型进行预测。为了了解这个定义,我们先举个简单的例子;我们假设一个线性方程 Y=2x+1, x变量为商...

wangxuwei ⋅ 昨天 ⋅ 0

容器之查看minikue的environment——minikube的环境信息

执行如下命令 mjduan@mjduandeMacBook-Pro:~/Docker % minikube docker-envexport DOCKER_TLS_VERIFY="1"export DOCKER_HOST="tcp://192.168.99.100:2376"export DOCKER_CERT_PATH="/U......

汉斯-冯-拉特 ⋅ 昨天 ⋅ 0

mysql远程连接不上

设置了root所有hosts远程登录,可是远程登录还是失败,原因可能如下: 登录本地数据库 mysql -uroot -p123456 查询用户表 mysql> select user,host,password from mysql.user; 删除密码为空的...

冰公子 ⋅ 昨天 ⋅ 0

没有更多内容

加载失败,请刷新页面

加载更多

下一页

返回顶部
顶部