集成valueList配置方法:
1.在
http://valuelist.sourceforge.net/index.html官方网站下载最新版valueList war包,其中包含了jar包和实例等所有相关内容。
2.valueList.jar,valuelist.tld拷贝到WEB-INF下(该文件也是从ValueList.war中解压出来的)。
3.创建一个applicationContext-valueList.xml文件,并且添加valuelist的entry。
xml内容如下:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:jee="http://www.springframework.org/schema/jee" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd
http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-2.5.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd ">
<bean id="valueListHelper" class="net.mlw.vlh.web.mvc.ValueListHandlerHelper">
<property name="valueListHandler">
<ref bean="valueListHandler"/>
</property>
</bean>
<bean id="valueListHandler" class="net.mlw.vlh.DefaultValueListHandlerImpl">
<property name="config.adapters">
<map>
<entry key="testAdapter">
<bean class="net.mlw.vlh.adapter.jdbc.dynabean.DefaultDynaBeanAdapter">
<property name="dataSource"><ref bean="dataSource"/></property>
<property name="useName"><value>false</value></property>
<property name="showSql"><value>true</value></property>
<property name="defaultNumberPerPage"><value>10</value></property>
<property name="defaultSortDirection"><value>desc</value></property>
<property name="defaultSortColumn" ><value>name</value></property>
<property name="adapterType"><value>2</value></property>
<property name="sql">
<value>
select id from test
</value>
</property>
</bean>
</entry>
</map>
</property>
</bean>
</beans>
4.在action类里封装如下方法(valueListHelper需要注入):
/**
* @function:默认查询,查询结果放到request的list变量中
* @param adapter根据adapter变量内容在xml中寻找对应标签如上例中testAdapter
* @param queries参数内容如需要传入查询条件放入map中
* @param tableId根据tableId变量内容在jsp页面中寻找对应root标签的id
* @param request查询结果存入request中
*/
public void setValueListToRequest(String adapter, Map queries, String tableId, HttpServletRequest request)
{
Map map = ValueListRequestUtil.getRequestParameterMap(request, tableId);
map.putAll(queries);
ValueListInfo info = new ValueListInfo(map);
if (map.get("limit")!=null) {
info.setPagingNumberPer(Integer.parseInt(map.get("limit").toString()));//每页记录数
} else {
info.setPagingNumberPer(Integer.MAX_VALUE - 1); // 不分页时取integer最大值(原来是默认80会有数据不全问题)
}
if (map.get("start")!=null) {
info.setPagingPage(Integer.parseInt(map.get("start").toString()) / info.getPagingNumberPer() + 1);//页数
} else {
info.setPagingPage(1); // 不分页时默认按照从第1页开始
}
ValueList valueList = valueListHelper.getValueList(adapter, info);
valueListHelper.backupInfoFor(request, info, tableId);
valueListHelper.setValueListTo(request, valueList, "list");//list为结果集的键
}
/**
*
* @function:获得表中数据
* @param adapter key值
* @param params 需要传的参数
* @return ValueList对象
* @author: mengqingyu 2011-10-18 下午05:11:34
*/
public ValueList getValueList(String adapter, Map params)
{
ValueListInfo info = new ValueListInfo(params);
if (params.get("limit")!=null) {
info.setPagingNumberPer(Integer.parseInt(params.get("limit").toString()));//每页记录数
} else {
info.setPagingNumberPer(Integer.MAX_VALUE - 1); // 不分页时取integer最大值(原来是默认80会有数据不全问题)
}
if (params.get("start")!=null) {
info.setPagingPage(Integer.parseInt(params.get("start").toString()) / info.getPagingNumberPer() + 1);//页数
} else {
info.setPagingPage(1); // 不分页时默认按照从第1页开始
}
ValueList valueList = valueListHelper.getValueList(adapter, info);
return valueList;
}
/**
*
* @function:将valueList对象转换成ArrayList对象
* @param adapter
* @param params
* @return
* @author: mengqingyu 2011-10-18 下午05:12:31
*/
public List<Map> getValuelistToListMap(String adapter, Map params)
{
ValueList valueList = this.getValueList(adapter, params);
List<BasicDynaBean> listBean = valueList.getList();
List<Map> listMap = new ArrayList<Map>();
for(BasicDynaBean dynaBean : listBean){
Map<String,String> map = new HashMap<String,String>();
DynaProperty[] dynaProperties = dynaBean.getDynaClass().getDynaProperties();
for (int i = 0; i < dynaProperties.length; i++) {
String key = dynaProperties[i].getName();
Object o = dynaBean.get(key);
if(o!=null){
map.put(key, o.toString());
}
}
listMap.add(map);
}
return listMap;
}
5.jsp页面代码:
需要引入标签
<%@ taglib uri="/WEB-INF/tld/valuelist.tld" prefix="vlh"%>
<%@ taglib uri="/WEB-INF/tld/c.tld" prefix="c" %>
valueList查询代码list为结果集的键,如果不想用vlh标签,需要在后台对list结果集重新封装来实现。
<vlh:root id="tableId" value="list" url="?" includeParameters="*" >
<vlh:header >
<vlh:column title="" attributes="width='3%'"/>
<vlh:column title="序号" attributes="width='5%'"/>
<vlh:column title="业务名称" property="serviceName" sortable="desc" attributes="width='30%'"/>
<vlh:column title="创建人" property="creator" attributes="width='10%'"/>
<vlh:column title="创建时间" property="startTime" sortable="desc" attributes="width='18%'"/>
<vlh:column title="结束时间" property="endTime" sortable="desc" attributes="width='18%'"/>
<vlh:column title="状态" attributes="width='7%'"/>
<vlh:column title="操作" attributes="width='11%'"/>
</vlh:header>
<c:if test="${list.valueListInfo.totalNumberOfEntries>0}">
<vlh:paging pages="4" showSummary="true"><c:out value="${pagetableId}" /></vlh:paging>
</c:if>
</vlh:root>
提交表单需要隐藏当前分页属性
<input type="hidden" name="tableId.pagingNumberPer" value="${tableId.pagingNumberPer}">
<input type="hidden" name="tableId.sortColumntable" value="${tableId.sortingColumn}">
<input type="hidden" name="tableId.sortDirectiontable" value="${tableId.sortingDirection}">
<input type="hidden" name="tableId.pagingPagetable" value="${tableId.pagingPage}">
6.加入valueList分页按钮图片
microsoftLook.properties拷贝到源码目录下(该文件也是从ValueList.war中解压出来的)。
在xml中加入如下配置:
<bean id="propertyConfigurer"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<list>
<value>classpath:microsoftLook.properties</value>
</list>
</property>
</bean>
<bean id="csvDisplayProvider" class="net.mlw.vlh.web.tag.support.CsvDisplayProvider"/>
<bean id="excelDisplayProvider" class="net.mlw.vlh.web.tag.support.ExcelDisplayProvider"/>
<bean id="microsoftLook" class="net.mlw.vlh.web.ValueListConfigBean">
<property name="displayHelper"><bean class="net.mlw.vlh.web.util.ImagesHomeDisplayHelper" /></property>
<property name="messageSource">
<bean class="org.springframework.context.support.ResourceBundleMessageSource">
<property name="basename"><value>microsoftLook</value></property>
</bean>
</property>
<property name="linkEncoder">
<bean class="net.mlw.vlh.web.tag.support.DefaultLinkEncoder" >
<property name="encoding">
<value>UTF-8</value>
</property>
</bean>
</property>
<property name="stylePrefix"><value>microsoftLook</value></property>
<property name="displayProviders">
<map>
<entry key="html">
<bean class="net.mlw.vlh.web.tag.support.HtmlDisplayProvider">
<property name="imageHome"><value>images/valueList</value></property>
<property name="usePadding"><value>false</value></property>
<property name="useNoWrap"><value>false</value></property>
<property name="preAppendContextPath"><value>true</value></property>
</bean>
</entry>
<entry key="csv"><ref bean="csvDisplayProvider" /></entry>
<entry key="excel"><ref bean="excelDisplayProvider" /></entry>
</map>
</property>
</bean>
7.修改valueList配置文件内容无需重启服务的方法:
需要自定义两个类文件取代valueList.jar中的类文件。
DefaultValueListHandlerImpl.java和Configuration.java
DefaultValueListHandlerImpl中改写引用Configuration是自定义类而不是jar中的类。
Configuration中加入:
/**
*
* @function: DEBUG模式
* @return
*/
public Map getAdapters()
{
return this.adapters;
}
原类中无此方法。
接下来在访问的超类中加入如下内容:
protected static Boolean isDebug = false; //valueList调试模式
/**
* @function:读取valueList配置文件
*/
private void reloadValueListContext() {
if(isDebug){
try {
DefaultValueListHandlerImpl handler = (DefaultValueListHandlerImpl) valueListHelper.getValueListHandler();
String path = new File(this.getClass().getResource("/").getPath()).getParent();
FileSystemXmlApplicationContext fileSystemXmlApplicationContext = new FileSystemXmlApplicationContext(new String[]{
path + "/springContext/applicationContext-page*.xml",
path + "/springContext/applicationContext-datasource*.xml"});
DefaultValueListHandlerImpl handlerDebug = (DefaultValueListHandlerImpl)fileSystemXmlApplicationContext.getBean("valueListHelper");
Map resultAdapters = handler.getConfig().getAdapters();
resultAdapters.putAll(handlerDebug.getConfig().getAdapters());
handler.getConfig().setAdapters(resultAdapters);
} catch (Exception e) {
e.printStackTrace();
}
}
}
在自定义的setValueListToRequest方法中加入reloadValueListContext方法。
idDebug变量内容可以通过前台页面传入来赋值。