//加载自定义标签,主要是在html的模班页面使用,在freemarkerUtil中的initCfg()方法中
freemarkerCfg.setSharedVariable("demo", new DemoDirective());
//客服
freemarkerCfg.setSharedVariable("custList", new CustServiceDirective());
public class CustServiceDirective extends BaseDirective implements TemplateDirectiveModel{
private StoreCustServiceService storeCustServiceService;
public CustServiceDirective(){
init("storeCustServiceService");//主要是用于调用service中的逻辑处理方法
}
@Override
public void execute(Environment env, Map params, TemplateModel[] loopVars,
TemplateDirectiveBody body) throws TemplateException, IOException {
StoreCustService storeCustService=new StoreCustService();
String traAgId = getParam(params, "traAgId");//获取参数的操作
if (traAgId != null) {
storeCustService.setTraAgId(Integer.parseInt(traAgId));
}
String status= getParam(params, "status");
if(status!=null){
storeCustService.setStatus(Integer.parseInt(status));
}
Writer out=env.getOut();
if(body!=null){
//设置循环变量
if(loopVars !=null && loopVars.length>0){
List custList=storeCustServiceService.findCustList(storeCustService," sequence ", "true".equals(getParam(params,"cache"))?true:false);//获取到需要的list
if(custList!=null && custList.size()>0){//循环输出
for (int i = 0; i1){
loopVars[1]=new SimpleNumber(i);
}
body.render(out);
}
}
}
}
}
}
继承的BaseDirective的写法
/**
*
* Description: 自定义标签共同的功能
*
*/
public class BaseDirective extends Base {
/**
* 获得参数
*
* @param params
* @param name
* @return
*/
public String getParam(Map params, String name) {
String value = "";
if (params.get(name) != null
&& params.get(name).toString().length() > 0) {
value = params.get(name).toString();
}
return value;
}
/**
* 获得参数并传递默认值
*
* @param params
* @param name
* @param defaultValue
* @return
*/
public String getParam(Map params, String name, String defaultValue) {
String value = defaultValue;
if (params.get(name) != null
&& params.get(name).toString().length() > 0) {
value = params.get(name).toString();
}
return value;
}
/**
* 获得int参数并传递默认值
*
* @param params
* @param name
* @param defaultValue
* @return
*/
public int getParamInt(Map params, String name, int defaultValue) {
int value = defaultValue;
if (params.get(name) != null
&& params.get(name).toString().length() > 0) {
try {
value = Integer.parseInt(params.get(name).toString());
} catch (Exception e) {
}
}
return value;
}
/**
* 获得数据
*
* @param params
* @param name
* @return
* @throws TemplateModelException
*/
public String getData(Environment env, String name)
throws TemplateModelException {
String value = "";
if (env.getDataModel().get(name) != null
&& env.getDataModel().get(name).toString().length() > 0) {
value = env.getDataModel().get(name).toString();
}
return value;
}
/**
* 获得数据并传递默认值
*
* @param params
* @param name
* @param defaultValue
* @return
* @throws TemplateModelException
*/
public String getData(Environment env, String name, String defaultValue)
throws TemplateModelException {
String value = defaultValue;
if (env.getDataModel().get(name) != null
&& env.getDataModel().get(name).toString().length() > 0) {
value = env.getDataModel().get(name).toString();
}
return value;
}
}
在html页面的应用
<@custList traAgId="${traAgId}" status="1";cuList>//status是参数,cuList是循环变量
<#if cuList.gender??>
<#if cuList.gender== 1>
<#elseif cuList.gender== 2>
#if>
#if>
${cuList.realName}
@custList>
还有一种循环方法
if(body!=null){
//设置循环变量
if(loopVars !=null && loopVars.length>0){
List travelList=travelAgencyService.find(travelAgency, order,limit);
loopVars[0]=new ArrayModel(travelList.toArray(), new BeansWrapper());
body.render(out);
}
}
在html中就是,这种写法能判断是否有值,可以针对这个做处理
<@travelList cityCode="${cityCode}" limit="0,10";traList>
<#if traList?exists && traList?size != 0>
<#list traList as trList>
#list>
<#else>
- 暂时没有旅行社添加!
#if>
@travelList>
还有一种多参数的做法
//多个循环变量的情况以及涉及到freemarker的分页做法
public class TailPageDirective extends BaseDirective implements TemplateDirectiveModel{
public TailPageDirective(){
init("tailProService");
}
@Override
public void execute(Environment env, Map params, TemplateModel[] loopVars,
TemplateDirectiveBody body) throws TemplateException, IOException {
if(body!=null){
//设置循环变量
if(loopVars !=null && loopVars.length>0){
String orderby=getParam(params, "orderby"," used desc");
//显示数量
int row=getParamInt(params, "row", 10);//给参数的默认值
//标题长度
int titleLen=getParamInt(params, "titleLen",0);
//当前第几页
int page=getParamInt(params, "page", 1);
String action=getParam(params, "action");
HashMap paramMap=new HashMap();
if(tailPromotion==null){
tailPromotion=new TailPromotion();
}
String cityCode = getParam(params, "cityCode");
if (StringUtil.isNotEmpty(cityCode)) {
tailPromotion.setDepartCityCode(cityCode);
}
tailPromotion.setTrStatus(0);
tailPromotion.setStatus(Byte.valueOf("3"));
String transportMeans=getParam(params, "transportMeans");//获取的是标签内传递的参数
//env.getDataModel().get("transportMeans")获取的是地址栏中的参数 if(env.getDataModel().get("transportMeans")!=null && env.getDataModel().get("transportMeans").toString()!=""){
transportMeans=env.getDataModel().get("transportMeans").toString();
tailPromotion.setTransportMeans(transportMeans);
paramMap.put("transportMeans", transportMeans);
action=action+"&transportMeans="+transportMeans;
}else if(StringUtils.isNotEmpty(transportMeans)){
tailPromotion.setTransportMeans(transportMeans);
paramMap.put("transportMeans", transportMeans);
action=action+"&transportMeans="+transportMeans;
}
//升降序的做法
String order =" sequence ";
String sequence=getParam(params, "sequence");
if(env.getDataModel().get("sequence")!=null && env.getDataModel().get("sequence").toString()!=""){
sequence =env.getDataModel().get("sequence").toString();
if("1".equals(sequence)){
order=" lowest_price desc ";
}else if("2".equals(sequence)){
order=" lowest_price ";
}
paramMap.put("sequence", sequence);
action=action+"&sequence="+sequence;
}else if(StringUtils.isNotEmpty(sequence)){
if("1".equals(sequence)){
order=" lowest_price desc ";
}else if("2".equals(sequence)){
order=" lowest_price ";
}
paramMap.put("sequence", sequence);
action=action+"&sequence="+sequence;
}
int count=tailProService.countByExample(tailPromotion);
FreemarkerPager pager=new FreemarkerPager();//分页的做法
pager.setCurrPage(page);
pager.setTotalCount(count);
pager.setPageSize(row);
pager.setAction(action);
paramMap.put("tcount", String.valueOf(count));
pager.setParams(paramMap);
List trpList=tailProService.findTP(tailPromotion,orderby,page, row);
//这个是ajax的做法
TreeMap daysMap=new TreeMap();//旅游天数
HashMap trPropertyMap=new HashMap();//团的属性
TreeMap priceMap=new TreeMap();//价格
if(trpList!=null && trpList.size()>0){
for(int i=0;i100 && lowPrice<=300){
priceMap.put(300, "100-300元");
}else if(lowPrice>300 && lowPrice<=500){
priceMap.put(500, "300-500元");
}
}
}
}
//搜索条件 放到map里面
Map sMap=new HashMap();
sMap.put("daysMap", daysMap);
sMap.put("trPropertyMap", trPropertyMap);
sMap.put("priceMap", priceMap);
if(loopVars.length==1){
loopVars[0]=new MapModel(sMap,new BeansWrapper());
}else if(loopVars.length==3){
loopVars[0]=new ArrayModel(trpList.toArray(),new BeansWrapper());
loopVars[1]=new BeanModel(pager,new BeansWrapper());
loopVars[2]=new MapModel(sMap,new BeansWrapper());
}
body.render(env.getOut());
}
}
}
}
在html页面中
<@tailPage row='10' page='${page!1}' istheme='0' proType="1" cityCode="${cityCode?if_exists}" action='${contextPath}zhoubian.do?cityCode=${cityCode}&cityName=${cityName}&templetPath=index_zb_wd_so.html' ;trpList,pager,sMap>
<#if sMap.get('daysMap')?size != 0>
<#assign daysMap = sMap.get('daysMap')>
- 行程天数:
-
<#list daysMap.keySet() as key>
${key}日游
#list>
#if>
<#if sMap.get('priceMap')?size != 0>
<#assign priceMap = sMap.get('priceMap')>
- 价格范围:
-
<#list priceMap.keySet() as key>
${priceMap.get(key)}
#list>
#if>