`
floger
  • 浏览: 213408 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

Hibernate+Spring+Struts2+ExtJS开发CRUD功能(2)

阅读更多
3、  建立的项目目录:

Root:

/resource/ext2.0/  将下载的ext-2.0-beta1.zip文件解压到该目录

/WEB-INF/web.xml

/WEB-INF/lib

/WEB-INF/classes/struts.xml

/WEB-INF/spring/applicationContext.xml

4、  代码清单: 

Level.java

 

import javax.persistence.Column;

import javax.persistence.Entity;

import javax.persistence.Id;

import javax.persistence.Table;

 

 

@Entity

@Table(name = "LOON_LEVEL")

public class Level implements java.io.Serializable {

    private Long levelid;

    private String levelname;

    private String description;

 

    public Level() {

    }

 

    public Level(Long levelid) {

       this.levelid = levelid;

    }

 

    public Level(Long levelid, String levelname, String description) {

       this.levelid = levelid;

       this.levelname = levelname;

       this.description = description;

    }

 

    @Id

    @Column(name = "LEVELID", unique = true, nullable = false, precision = 5, scale = 0)

    public Long getLevelid() {

       return this.levelid;

    }

 

    public void setLevelid(Long levelid) {

       this.levelid = levelid;

    }

 

    @Column(name = "LEVELNAME", length = 64)

    public String getLevelname() {

       return this.levelname;

    }

 

    public void setLevelname(String levelname) {

       this.levelname = levelname;

    }

 

    @Column(name = "DESCRIPTION", length = 256)

    public String getDescription() {

       return this.description;

    }

 

    public void setDescription(String description) {

       this.description = description;

    }

}

 

ILevelDAO.java

 

import java.util.List;

public interface ILevelDAO {

    public Level findLevelById(Long id);

    public List<Level>  findAllLevels();

    public void persistLevel(Level level);

    public void removeLevel(Level level);

    public void removeById(Long id);

}

 

LevelDAO.java

 

import java.util.List;

import org.hibernate.Session;

import org.springframework.orm.hibernate3.HibernateCallback;

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

public class LevelDAO extends HibernateDaoSupport implements ILevelDAO {

    public LevelDAO() {

       super();

    }

    public Level findLevelById(Long id) {

       return (Level) getHibernateTemplate().get(Level.class, id);

    }

    public List<Level> findAllLevels() {

       return getHibernateTemplate().loadAll(Level.class);// .find("from Level o");//

    }

    public void persistLevel(Level level) {

       getHibernateTemplate().saveOrUpdate(level);

    }

    public void removeLevel(Level level) {

       getHibernateTemplate().delete(level);

    }

 

    public void removeById(final Long id) {

       this.getHibernateTemplate().execute(new HibernateCallback() {

           public Object doInHibernate(Session session) {

              session.createQuery("delete from Level o where o.levelid=" + id + "").executeUpdate(); 

              return 1;

           }

       });

    }

}

 

ILevelService.java

 

import java.util.List;

public interface ILevelService {

    public Level findLevelById(Long id) throws Exception;

    public List<Level> findAllLevels() throws Exception;

    public List<Level> findLevelsByExample(Level level) throws Exception;

    public void persistLevel(Level level) throws Exception;

    public void removeLevel(Level level) throws Exception;

public void removeLevelById(Long id) throws Exception;

}

 

LevelService.java

 

import java.util.List;

import privilege.dao.*;

import privilege.database.Level;

import org.springframework.context.ApplicationContext;

public class LevelService implements ILevelService {

    private ILevelDAO dao;

    private static final String SERVICE_BEAN_ID = "LevelService";

    public LevelService() {

       super();

    }

    public static ILevelService getInstance(ApplicationContext context) {

       return (ILevelService) context.getBean(SERVICE_BEAN_ID);

    }

    public Level findLevelById(Long id) throws Exception {

       try {

           return getDao().findLevelById(id);

       } catch (RuntimeException e) {

           throw new Exception("findLevelById failed with the id " + id + ": " + e.getMessage());

       }

    }

    public void persistLevel(Level level) throws Exception {

       try {

           getDao().persistLevel(level);

       } catch (RuntimeException e) {

           throw new Exception("persistLevel failed: " + e.getMessage());

       }

    }

    public void removeLevel(Level level) throws Exception {

       try {

           getDao().removeLevel(level);

       } catch (RuntimeException e) {

           throw new Exception("removeLevel failed: " + e.getMessage());

       }

    }

    public void removeLevelById(Long id) throws Exception {

       try {

           getDao().removeById(id);

       } catch (RuntimeException e) {

           throw new Exception("removeLevel failed: " + e.getMessage());

       }

    }

 

    public void setDao(ILevelDAO dao) {

       this.dao = dao;

    }

    public ILevelDAO getDao() {

       return this.dao;

    }

}

 

ExtJSONActionSuport.java

辅助类,继承了ActionSupport

import com.opensymphony.xwork2.ActionSupport;

 

public class ExtJSONActionSuport extends ActionSupport {

    private int totalCount = 0;// 总数

    private transient int start = 0;// 开始数

    private transient int limit = 0;// 限制数量

    private String jsonString = "";

 

    public String getJsonString() {

       return jsonString;

    }

 

    public void setJsonString(String jsonString) {

       this.jsonString = jsonString;

    }

 

    public String jsonExecute() throws Exception {

       return super.execute();

    }

 

     

 

    public int getTotalCount() {

       return totalCount;

    }

 

    public void setTotalCount(int totalCount) {

       this.totalCount = totalCount;

    }

 

    public int getStart() {

       return start;

    }

 

    public void setStart(int start) {

       this.start = start;

    }

 

    public int getLimit() {

       return limit;

    }

 

    public void setLimit(int limit) {

       this.limit = limit;

    }

}

 

 

LevelAction.java

 

import java.util.List;

import java.util.ArrayList;

import javax.servlet.http.HttpSession;

import org.apache.struts2.ServletActionContext;

import net.sf.json.JSONArray;

import privilege.database.Level;

import privilege.service.*;

import commons.utils.action.ExtJSONActionSuport;

 

public class LevelAction extends ExtJSONActionSuport {

    private static final long serialVersionUID = 1L;

    private Level level = null;

    private List<Level> levels = new ArrayList<Level>(0);

    private ILevelService levelService = null;

    private String delData;

 

    public String execute() {

       return this.SUCCESS;

    }

 

    @Override

    public String jsonExecute() throws Exception {

       if (this.getDelData() != null && !"".equals(this.getDelData())) {

           if (this.getDelData().indexOf(",") < 0) {

              this.levelService.removeLevelById(Long.parseLong(this.getDelData()));

               System.out.println("del_id:" + getDelData());

           } else {

              String id[] = this.getDelData().split(",");

              for (int i = 0; i < id.length; i++) {

                  System.out.println("del:" + id[i]);

                  this.levelService.removeLevelById(Long.parseLong(id[i]));

               }

           }

       }

       HttpSession session = ServletActionContext.getRequest().getSession();

       Object o = null;// session.getAttribute("Level_Data1");

       if (o == null) {

           try {

              this.levels = this.getLevelService().findAllLevels();

              session.setAttribute("Level_Data1", this.levels);

              System.out.println("query database");

           } catch (Exception e) {

              e.printStackTrace();

           }

       } else {

           this.setLevels(((List<Level>) o));

       }

       this.setTotalCount(this.levels.size());

       JSONArray array = JSONArray.fromObject(this.levels);

       // System.out.println(this.getStart() + "---" + this.getLimit());

       this.setJsonString("{success:true,totalCount : " + this.getTotalCount() + ", list:" + array.toString() + "}");

       // System.out.println(this.getJsonString());

       return super.jsonExecute();

    }

 

    /**

     * Find an entity by its id (primary key).

     * 

     * @param id

     * @return The found entity instance or null if the entity does not exist.

     */

    public String findLevelById(Long id) {

       try {

           this.level = this.getLevelService().findLevelById(id);

       } catch (Exception e) {

           e.printStackTrace();

       }

       JSONArray array = JSONArray.fromObject(this.levels);

       this.setJsonString(array.toString());

       return SUCCESS;

    }

 

    public String findLevelById() {

       System.out.println(this.level.getLevelid());

       try {

           this.level = this.getLevelService().findLevelById(this.level.getLevelid());

       } catch (Exception e) {

           e.printStackTrace();

       }

       JSONArray array = JSONArray.fromObject(this.level);

       this.setJsonString(array.toString());

       this.setJsonString("{success:true,totalCount:1,list:" + array.toString() + "}");

       System.out.println(array.toString());

       return SUCCESS;

    }

 

    /**

     * @return Return all persistent instances of the <code>Level</code> entity.

     */

    public String getAllLevels() {

       try {

           this.levels = this.getLevelService().findAllLevels();

       } catch (Exception e) {

           e.printStackTrace();

       }

       return SUCCESS;

    }

 

 

    /**

     * Make the given instance managed and persistent.

     * 

     * @return

     */

    public String persistLevel() {

       System.out.println(this.level.getLevelid() + "---" + this.level.getLevelname() + "---"

              + this.level.getDescription());

       this.setJsonString("{success:true}");

       try {

           this.getLevelService().persistLevel(this.getLevel());

       } catch (Exception e) {

           e.printStackTrace();

       }

       return SUCCESS;

    }

 

    /**

     * Remove the given persistent instance.

     * 

     * @return

     */

    public String removeLevel() {

       try {

           this.getLevelService().removeLevel(this.getLevel());

       } catch (Exception e) {

           e.printStackTrace();

       }

       return SUCCESS;

    }

 

    /**

     * Remove an entity by its id (primary key). *

     * 

     * @return

     */

    public String removeLevelById(Long id) {

       try {

           this.getLevelService().removeLevelById(id);

       } catch (Exception e) {

           e.printStackTrace();

       }

       return SUCCESS;

    }

 

    public Level getLevel() {

       return level;

    }

 

    public void setLevel(Level level) {

       this.level = level;

    }

 

    public List<Level> getLevels() {

       return levels;

    }

 

    public void setLevels(List<Level> levels) {

       this.levels = levels;

    }

 

    public ILevelService getLevelService() {

       return levelService;

    }

 

    public void setLevelService(ILevelService levelService) {

       this.levelService = levelService;

    }

 

    public String getDelData() {

       return delData;

    }

 

    public void setDelData(String delData) {

       this.delData = delData;

    }

}

 

分享到:
评论

相关推荐

    Hibernate+Spring+Struts2+ExtJS开发CRUD功能

    Hibernate+Spring+Struts2+ExtJS开发CRUD功能

    Hibernate+Spring+Struts2+ExtJS开发CRUD功能实例

    在IT行业中,构建Web应用程序是常见的任务,而“Hibernate+Spring+Struts2+ExtJS开发CRUD功能实例”提供了一个完整的解决方案,用于快速开发基于Java的Web应用,特别是涉及数据库操作的CRUD(创建、读取、更新、删除...

    Hibernate+Spring+Struts2+ExtJS开发CRUD功能最新版lib[Zone Yan]

    Hibernate+Spring+Struts2+ExtJS开发CRUD功能最新版lib[Zone Yan] 2个小时才找全的啊。

    Hibernate+Spring+Struts2+ExtJS整合开发实例

    在"Hibernate+Spring+Struts2+ExtJS整合开发实例"中,开发者通常会利用这些框架的协同工作来实现CRUD(Create, Read, Update, Delete)操作,这是数据库应用的基本功能: 1. **创建(Create)**: 用户通过ExtJS的...

    Hibernate+Spring+Struts2+ExtJS开发CRUD功能.doc

    ### Hibernate+Spring+Struts2+ExtJS集成开发CRUD功能 #### 一、技术栈介绍与环境搭建 **1. Hibernate:** Hibernate是一个强大的对象关系映射(ORM)框架,用于简化Java应用程序与数据库之间的交互。通过...

    Hibernate+Spring+Struts2+ExtJS CRUD

    总结来说,整合Hibernate、Spring、Struts2和ExtJS可以构建出功能强大且用户体验良好的Web应用。通过CRUD操作和动态加载树,我们可以有效地管理复杂的数据结构,为用户提供直观的交互界面。在实际开发中,还需要根据...

    比较流行的框架模式(Hibernate+Spring+Struts2+ExtJS)

    标题中的"Hibernate+Spring+Struts2+ExtJS"是一个经典的Java Web开发框架组合,也被称为SSH2(Struts2, Spring, Hibernate)与ExtJS的集成。这个组合在过去的几年里广泛应用于企业级应用开发,提供了强大的数据持久...

    struts2+spring+hibernate+extjs实例(音乐播放器)

    这是一个基于Java技术栈的Web应用实例,名为...总之,这个实例是一个典型的Java Web项目,通过集成Struts2、Spring、Hibernate和ExtJS,实现了音乐播放器的功能,对于学习和理解这些技术的结合使用具有很高的参考价值。

    图书管理系统源码(ExtJs+struts2+hibernate+spring)

    【图书管理系统源码(ExtJs+struts2+hibernate+spring)】是一个基于Web的软件项目,它展示了如何整合多种技术来构建一个实际的应用系统。这个管理系统使用了前端框架ExtJs,后端MVC框架Struts2,持久层框架...

    Struts2+Spring+Hibernate+ExtJS开发实例.pdf

    ### Struts2+Spring+Hibernate+ExtJS集成开发详解 #### 一、技术栈概览与集成背景 在企业级应用开发中,采用MVC架构的项目常常会使用到多种框架来实现不同的业务需求,其中Struts2、Spring、Hibernate以及ExtJS是...

    Extjs+hibernate+struts2+spring案例大全源代码

    这是一个基于Java技术栈的Web应用开发案例集合,涵盖了ExtJS前端框架、Hibernate持久化框架、Struts2 MVC框架以及Spring框架的综合运用。这个源代码库对于初学者来说是一个宝贵的资源,可以帮助他们理解和掌握这些...

    J2EE最新精品项目源码Struts2.0+Hibernate+Spring+ExtJS

    这个压缩包文件包含了一个基于J2EE架构的高级项目源码,主要采用了Struts2.0、Hibernate、Spring和ExtJS这四大核心技术。下面将详细解释这些技术及其在项目中的应用。 **Struts2.0** 是一个MVC(Model-View-...

    spring mvc+hibernate+extjs代码示例

    这是一个基于Spring MVC、Hibernate和ExtJS的Web应用示例,主要展示了如何整合这三个技术来构建一个功能完善的后台管理系统。下面将分别对这三个技术及其整合方式进行详细介绍。 **Spring MVC** Spring MVC是Spring...

    extjs2.0+struts1.2+hibernate+spring增删改查

    本项目名为"extjs2.0+struts1.2+hibernate+spring增删改查",结合了四个核心的技术框架,它们分别是ExtJS 2.0、Struts 1.2、Hibernate和Spring,用于实现数据的CRUD(创建、读取、更新和删除)操作。这四个组件共同...

    Struts2.18+Spring2.56+Hibernate3+Extjs+JSON实现登陆修改密码等

    Struts2.18+Spring2.56+Hibernate3+Extjs+JSON是一个经典的Java Web开发技术组合,常用于构建高效、可扩展的企业级应用。这个项目涉及到的知识点广泛,涵盖前端、后端以及数据持久化等多个层面。以下是这些技术在...

Global site tag (gtag.js) - Google Analytics