`

Displaytag + spring + dwr+hibernate

阅读更多

Displaytag is an open source suite of custom tags with which you can easily display a collection of Objects as a table including direct support for paging and sorting. Normally selecting a new page, or sorting the tables leads to a complete page-refresh. It is more user-friendly to refresh only the data in the table using Ajax technology, however Displaytag doesn't offer this out-of-the-box. But we can of course try to add support for this using one of the many Ajax frameworks that are currently available.

A non ajax enabled Displaytag would do a request to a controller for every action such as a sorting or selecting a next page. This would result in a complete page refresh (step 1-8 ). When we Ajax enable the Displaytag we skip the page refresh and only refresh a specific piece of the page using an exposed service which provides the updated HTML fragment (step 1a-8a).

flow

In the example I am going to use the following technologies:

The senario is as follows: We want to display episodes in a table. We want to do this in an Ajax way by using DWR. But where do we start?

Well, first of all we create a domain object called Episode.

 
public

 class

 Episode {


   long

 id;
   String



 show;
   String



 name;
   String



 episode;
   String



 airDate;
....
}


 

Next we create a repository.

 
public

 class

 EpisodeRepository extends

 HibernateDaoSupport {


 
    public

 List<Episode> getAllEpisodes(

int

 firstResult, int

 maxResults, String



 orderBy,
    boolean

 ascending)

 {


        Criteria criteria = getSession(

)

.createCriteria

(

Episode.class

)


            .setFirstResult

(

firstResult)


	    .setMaxResults

(

maxResults)


	    .addOrder

(

ascending ? Order.asc

(

orderBy)

 : Order.desc

(

orderBy)

)

;
	    return

 criteria.list

(

)

;
	}


 
    public

 int

 getNumberOfEpisodes(

)

 {


        Criteria criteria = getSession(

)

.createCriteria

(

Episode.class

)

;
	criteria.setProjection

(

Projections.count

(

"id"

)

)

;
	return

 (

Integer



)

criteria.uniqueResult

(

)

;
    }


}


 

And a Jsp containing the Displaytag for presenting the collection of Episodes.
<display:table id="ep" name="eps " sort="external" requestURI="replaceURI " pagesize="30" partialList="true" size="${nrOfEps }">
    <display:setProperty name="basic.empty.showtable" value="true" />
    <display:column title="Show" property="show" sortable="true" sortName="show" />
    <display:column title="Name" property="name" sortable="true" sortName="name" />
    <display:column class="Episode" property="episode" sortable="true" sortName="episode" />
    <display:column title="Airdate" property="airDate" sortable="true" sortName="airDate" />
</display:table>

Note that I use replaceURI as requestURI. This is very important because we are going to replace this with a call to a javascript method. Displaytag creates links like <a href="repaceURI?d-2332-o=1...."> and this should be replaced by <a href="javascript:update('d-2332-o=1...')> so that we the links will be Ajax enabled too. Unfortunatly this is not the only thing that we need to update. We need to update the range we are displaying, the page we are currently on and how the data is sorted.

So how do we do that?
Well, we need to expose a service using DWR. DWR is an Ajax toolkit which make it possible to expose services which can then be called from JavaScript. I created an abstract generic class so that you can use it to enable any service you like. The abstract class contains the following method:

 
public

 abstract

 class

 AbstractExposedDisplayTagService<T> implements

 InitializingBean {


 
    public

 void

 afterPropertiesSet(

)

 throws

 Exception



 {


        ....
    }


 
    public

 String



 findAllObjects(

String



 criteria)

 {


        WebContext wctx = WebContextFactory.get

(

)

;
	HttpServletRequest request = wctx.getHttpServletRequest

(

)

;
 
	// split results and set values;


	int

 maxResults = Integer



.parseInt

(

getCriterionValue(

criteria, "maxResults"

, DEFAULT_MAXIMUM_RESULTS)

)

;
	int

 page = Integer



.parseInt

(

getCriterionValue(

criteria, displayTagPage, "1"

)

)

;
	boolean

 ascending = Integer



.parseInt

(

getCriterionValue(

criteria, displayTagSortOrder, "1"

)

)

 == 1

 ? true

 : false

;
	String



 orderBy = getCriterionValue(

criteria, displayTagOrderBy, "id"

)

;
	int

 firstResult = (

page - 1

)

 * maxResults;
	int

 numberOfObjects = getNumberOfObjects(

)

;
 
	// set the episodes on the request so dwr can reload the jsp part.


	request.setAttribute

(

getObjectsName(

)

, getObjects(

firstResult, maxResults, orderBy, ascending)

)

;
	request.setAttribute

(

getNumberOfObjectsName(

)

, numberOfObjects)

;
	try

 {


	    String



 html = wctx.forwardToString

(

viewFragment)

;
	    html = DisplayTagReplacementUtil.updatePagingHtml

(

html, page, maxResults, numberOfObjects, displayTagPage)

;
	    html = DisplayTagReplacementUtil.updateSortOrderHtml

(

html, ascending, displayTagSortOrder)

;
	    html = DisplayTagReplacementUtil.updateHtmlLinks

(

html)

;
	    return

 html;
	}

 catch

 (

ServletException e)

 {


	    return

 ""

;
	}

 catch

 (

IOException



 e)

 {


	    return

 ""

;
	}


}


}


 

And here is the implementation.

 
public

 class

 EpisodeService extends

 AbstractExposedDisplayTagService<Episode> {


	private

 EpisodeRepository episodeRepository;
 
	@Override
	public

 int

 getNumberOfObjects(

)

 {


		return

 episodeRepository.getNumberOfEpisodes

(

)

;
	}


 
	@Override
	public

 String



 getNumberOfObjectsName(

)

 {


		return

 "numberOfEpisodes"

;
	}


 
	@Override
	public

 List<Episode> getObjects(

int

 firstResult, int

 maxResults, String



 orderBy, boolean

 ascending)

 {


		return

 episodeRepository.getAllEpisodes

(

firstResult, maxResults, orderBy, ascending)

;
	}


 
	@Override
	public

 String



 getObjectsName(

)

 {


		return

 "episodes"

;
	}


 
	public

 void

 setEpisodeRepository(

EpisodeRepository episodeRepository)

 {


		this

.episodeRepository

 = episodeRepository;
	}


}


 

Now you see that the findAllObjects method calls 3 static update methods on the DisplayTagReplacementUtil class.
These are responsible for the actual Ajax enabling. They use regular expressions to update the links, sorting etc..

Finally we need to add some JavaScript to be able to call the exposed service methods.
The following piece of code should be in the head of the jsp , in which you include the jsp containing the displaytag shown above in.
<script type='text/javascript' src='<c:url value="/dwr/interface/EpisodeService.js"/>'></script>
<script type='text/javascript' src='<c:url value="/dwr/engine.js"/>'></script>
<script type='text/javascript' src='<c:url value="/dwr/util.js"/>'></script>
<link rel="stylesheet" type="text/css" href='<c:url value="/css/displaytag.css"/>' />

and the javascript:
<script type="text/javascript">
function update(criteria) {
EpisodeService.findAllObjects(criteria, function(data) {
dwr.util.setValue("displayTable", data, { escapeHtml:false });
});
}
update("");
</script>

It is now a simple matter of extending the abstract class and you have Ajax enabled your Displaytag .
I have attached the example project here so you can look into the code yourself.

分享到:
评论

相关推荐

    Struts2.1+hibernate3.3+spring3整合开发的商品库存管理系统

    Struts2.1+hibernate3.3+spring3+jquery+displaytag+mysql整合开发的商品库存管理系统。大家一起学习!做的不好,请大家指导!没有JAR包,大家自己把环境建好再!

    [浪曦][原创][A342]Struct+Hibernate+DisplayTag标签+js+Div+css 第4讲(zk原创).exe

    [浪曦][原创][A342]Struct+Hibernate+DisplayTag标签+js+Div+css 第4讲(zk原创).exe[浪曦][原创][A342]Struct+Hibernate+DisplayTag标签+js+Div+css 第4讲(zk原创).exe

    Struts2+displaytag+dbutil

    总结来说,"Struts2+displaytag+dbutil"的组合提供了一个强大的平台,用于构建动态的、数据驱动的Web应用。Struts2处理请求和流程控制,DisplayTag美化数据展示,而DbUtil则简化了数据库交互,三者协同工作,实现了...

    Spring2.5+Struts2.0+hibernate3.0+Dwr+jquery+displayTag

    自已写的一个demo 1 基于SSH,service采用 annotation注入减少配置 ...暴露DWR,写了一个验证用户名的流程 4 采用jpa作为POJO,还是减少配置 5 加入display的分页,并且是物理分页 打开后自已建表sql.txt jdbc.properties

    SSH泛型DAO+Proxool+DisPlayTag+Jquery easyui

    标题中的"SSH泛型DAO+Proxool+DisPlayTag+Jquery easyui"涉及到的是一个Web开发技术的组合,主要包括以下几个部分: 1. SSH(Struts2 + Spring + Hibernate):这是一个流行的Java Web开发框架组合,用于构建企业级...

    Struts2+Spring +Hibernate的整合

    Struts2、Spring和Hibernate是Java Web开发中的三大框架,它们各自负责Web应用程序的不同层面:Struts2专注于表现层,Spring处理业务逻辑和服务层,而Hibernate则是持久化层的重要工具。将这三者进行整合,可以构建...

    [浪曦原创]Struct+Hibernate+DisplayTag标签+js+Div+css 第4讲 (zk001).rar

    【标题】:“[浪曦原创]Struct+Hibernate+DisplayTag标签+js+Div+css 第4讲 (zk001).rar”所涵盖的知识点主要集中在Web开发领域,特别是使用Struts、Hibernate、DisplayTag、JavaScript、Div和CSS等技术进行网页设计...

    [浪曦][原创][A335]Struct+Hibernate+DisplayTag标签+js+Div+css 第1讲(zk原创).rar

    在 "[A335]Struct+Hibernate+DisplayTag标签+js+Div+css 第1讲.exe" 这个文件中,你将深入了解到这些技术的结合应用。这不仅涵盖了后端的数据管理,也包括前端的展示和交互,是一份全面的Web开发入门教程。通过学习...

    [浪曦][原创][A337]Struts+Hibernate+DisplayTag标签+js+Div+css 第2讲(zk原创).rar

    【浪曦】是一个在线教育平台,提供原创的IT技术教程,本教程重点讲解了如何将Struts、Hibernate、DisplayTag、JavaScript、Div和CSS结合使用,以构建一个强大的Web应用程序。这一讲是系列课程的第二部分,由讲师 zk ...

    Structs+Spring+Hibernate框架所需jar包下载

    标题中的"Structs+Spring+Hibernate框架所需jar包下载"提到了三个重要的Java开发框架:Struts、Spring和Hibernate。这三个框架是企业级Java应用开发中常用的组件,用于构建MVC(模型-视图-控制器)架构的应用程序。...

    Struts2+Spring2+Hibernate3

    Struts2、Spring和Hibernate是Java Web开发中的三大主流框架,它们各司其职,共同构建了一个高效、灵活的企业级应用开发环境。SSH框架整合,是将这三个框架的优势结合在一起,形成一个强大的后端开发解决方案。 ...

    数据库struts2.3.4.1+hibernate3.6.10+sping3.1.2(sql2000+proxool)+displaytag

    数据库struts2.3.4.1+hibernate3.6.10+sping3.1.2(sql2000+proxool)+displaytag 据库 struts+hibernate+sping sql2000 proxool displaytag

    struts2.3.4.1+hibernate3.6.10+sping3.1.2(sql2000+proxool)+displaytag

    struts hibernate sping sql2000 proxool displaytag 源码 实例 struts2.3.4.1+hibernate3.6.10+sping3.1.2(sql2000+proxool)+displaytag原创代码实例 sqlserver2000搭建ssh proxool连接池

    Spring+Hibernate+Struts实现分页

    在Java Web开发中,Spring、Hibernate和Struts是三大核心框架,它们分别负责不同层面的任务。Spring作为一个全面的轻量级应用框架,提供了依赖注入(DI)和面向切面编程(AOP)等功能,用于管理应用程序的组件。...

    spring+hibernate+struts实现分页代码

    在Java Web开发中,Spring、Hibernate和Struts是三大核心框架,它们的组合常被称为SSH框架,用于构建高效、可维护的企业级应用。本项目利用这三者来实现分页功能,这是一种常见的需求,特别是在数据量大的情况下,...

    spring+struts2+hibernate框架

    在Spring和Struts2中,可以通过自定义拦截器或者使用第三方库(如DisplayTag或MyBatis的PageHelper)来实现分页。在Hibernate中,可以利用Criteria的setFirstResult和setMaxResults方法实现分页查询。 7. **项目...

    struts2+spring+ibatis

    在Struts2中,可以使用插件如DisplayTag或FreeMarker模板配合实现分页。同时,Spring的Pageable接口或MyBatis的PageHelper插件也可以帮助实现分页功能。 至于标签中的“分页 struts2”,说明这个项目重点展示了如何...

    Displaytag dwr 分页

    Displaytag、DWR、Spring 和 Hibernate 是 Java Web 开发中常用的技术栈,它们在构建高效、动态和数据驱动的Web应用程序方面发挥着重要作用。这里我们将深入探讨这些技术以及它们如何协同工作。 首先,Displaytag ...

    酒店客房管理系统(毕业设计) struts + spring + ibaits2.0

    【酒店客房管理系统(毕业设计) struts + spring + ibatis2.0】是一个典型的Java Web应用程序,用于管理和优化酒店的客房预订和服务流程。这个系统利用了MVC(Model-View-Controller)架构模式,其中Struts作为控制...

Global site tag (gtag.js) - Google Analytics