public
Last active

  • Download Gist
ItemListActionBean.java
Java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77
package org.stripesbook.quickstart.action;
 
// my imports
// java imports
import java.util.List;
 
// 3rd party imports
import net.sourceforge.stripes.action.DefaultHandler;
import net.sourceforge.stripes.action.ForwardResolution;
import net.sourceforge.stripes.action.Resolution;
import org.apache.log4j.Logger;
import org.hibernate.criterion.Restrictions;
import org.hibernate.Session;
import org.stripesbook.quickstart.model.Item;
import org.stripesstuff.stripersist.Stripersist;
 
 
public class ItemListActionBean extends ItemBaseActionBean {
 
private final static String LIST = "/WEB-INF/jsp/item_list.jsp";
private final static String ITEM_LIST = "/WEB-INF/jsp/parts/item_table.jsp";
 
private static Logger logger = Logger.getLogger(ItemListActionBean.class);
private List<Item> items;
public String filter;
 
@DefaultHandler
public Resolution list() {
logger.debug("*** in list()");
Session sess = org.stripesbook.quickstart.util.DAO.getSession();
items = sess.createCriteria(Item.class).list();
logger.debug("*** items.size: " + items.size());
return new ForwardResolution(LIST);
}
public Resolution findByName() {
logger.debug("*** in findByName()");
Session sess = org.stripesbook.quickstart.util.DAO.getSession();
if (filter != null && filter.length() > 0) {
items = sess.createCriteria(Item.class)
.add(Restrictions.ilike("name", filter+"%"))
.list();
} else {
this.list();
}
logger.debug("*** after filter");
return new ForwardResolution(ITEM_LIST);
}
 
public List<Item> getItems() {
return items;
}
 
public void setItems(List<Item> items) {
this.items = items;
}
public String getFilter() {
return filter;
}
 
public void setFilter(String filter) {
this.filter = filter;
}
}
item_list.js
JavaScript
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
/***
* Excerpted from "Stripes: and Java Web Development is Fun Again",
* published by The Pragmatic Bookshelf.
* Copyrights apply to this code. It may not be used to create training material,
* courses, books, articles, and the like. Contact us if you are in doubt.
* We make no guarantees that this code is fit for any purpose.
* Visit http://www.pragmaticprogrammer.com/titles/fdstr for more book information.
***/
/* item_list.js */
 
function filterItems(field, url) {
// from aporter:
//$('$item_table').load(url, { 'filter': $(field).val(), '_eventName': 'findByName' });
 
$.get(url,
{ 'filter': $(field).val(),
'_eventName': 'findByName'
},
function(data) {
$('#item_table').html(data);
}
);
}
 
function ajaxLink(link, update) {
$.get(link, function(data) {
$(update).html(data);
$(update).show();
});
return false;
}
item_list.jsp
Java Server Pages
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80
<%-- Trying to do what Matt Raible does in his example here: http://raibledesigns.com/rd/entry/the_future_of_the_displaytag --%>
 
<%@ include file="/WEB-INF/jsp/common/taglibs.jsp" %>
 
<s:layout-render name="/WEB-INF/jsp/common/layout.jsp" title="Found Items">
 
<s:layout-component name="head">
<script type="text/javascript" src="${contextPath}/script/jquery-1.3.2.min.js"></script>
<script type="text/javascript" src="${contextPath}/script/item_list.js"></script>
<script type="text/javascript" src="${contextPath}/script/aa.js"></script>
 
<script type="text/javascript">
ajaxAnywhere.getZonesToReaload = function() { return "itemList" }
ajaxAnywhere.onAfterResponseProcessing = function() { replaceLinks() }
function replaceLinks() {
// replace all the links in <thead> with onclick's that call AjaxAnywhere
var sortLinks = $('userList').getElementsByTagName('thead')[0]
.getElementsByTagName('a');
ajaxifyLinks(sortLinks);
if (document.getElementsByClassName('pagelinks').length > 0) {
var pagelinks = document.getElementsByClassName('pagelinks')[0]
.getElementsByTagName('a');
ajaxifyLinks(pagelinks);
}
}
function ajaxifyLinks(links) {
for (i=0; i < links.length; i++) {
links[i].onclick = function() {
ajaxAnywhere.getAJAX(this.href);
return false;
}
}
}
replaceLinks();
</script>
</s:layout-component>
 
<s:layout-component name="body">
<div>Found Items</div>
 
<%-- The filter field --%>
<s:url var="url" beanclass="org.stripesbook.quickstart.action.ItemListActionBean"/>
Item Filter: <input type="text" onkeyup="filterItems(this, '${url}');"/><br/>
 
<%-- This tag is the one called by the JS filter to redraw the table --%>
<div id="item_table">
 
<aa:zone name="itemList">
<%-- I generally include the table below in the include, but commented out and pasted below for now --%>
<%--@include file="/WEB-INF/jsp/parts/item_table.jsp" --%>
 
<d:table name="${actionBean.items}" id="item" requestURI="" defaultsort="1"
pagesize="30" sort="list" defaultorder="descending">
 
<d:column title="Date Found" sortProperty="dateFound" sortable="true" >
<fmt:formatDate type="date" dateStyle="full" value="${item.dateFound}" />
</d:column>
 
<d:column title="Name" sortable="true">
<s:link beanclass="org.stripesbook.quickstart.action.ItemDetailActionBean">
<s:param name="item" value="${item.id}"/>
${item.name}
</s:link>
</d:column>
 
<d:column title="City" property="cityId" sortable="true" />
<d:column title="State" property="stateId" sortable="true" />
 
<d:column title="Picture" sortable="true">
<c:if test="${item.imageExists}">
x
</c:if>
</d:column>
</d:table>
</div>
 
</aa:zone>
</s:layout-component>
</s:layout-render>
taglibs.jsp
Java Server Pages
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
<%-- Stripes TLD --%>
<%@ taglib prefix="s" uri="http://stripes.sourceforge.net/stripes.tld" %>
<%@ taglib prefix="sdyn" uri="http://stripes.sourceforge.net/stripes-dynattr.tld" %>
 
<%-- JSTL TLDs --%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %>
 
<%-- Display Tag - displaytag.sourceforge.net --%>
<%@ taglib prefix="d" uri="http://displaytag.sf.net" %>
 
<%-- AjaxAnywhere - ajaxanywhere.sourceforge.net --%>
<%@ taglib prefix="aa" uri="http://ajaxanywhere.sourceforge.net/" %>
 
<%-- This is so that you can conveniently refer to the context path with ${contextPath} --%>
<c:set var="contextPath" value="${pageContext.request.contextPath}"/>

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.