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

freemarker中的directive

 
阅读更多

//加载自定义标签,主要是在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; i<custList.size(); i++) {
						loopVars[0]=new BeanModel(custList.get(i), new BeansWrapper());
						if(loopVars.length>1){
							loopVars[1]=new SimpleNumber(i);
						}
						body.render(out);
					}
					
				}
			}
		}
		
		
	}
	

}

 继承的BaseDirective的写法

/**
 * <p>
 * Description: 自定义标签共同的功能
 * </p>
 */
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是循环变量
				<li>
				<#if cuList.gender??>
					<#if cuList.gender== 1>
						<div class="avatar"><img width="65" height="66" src="../img/ta/men_100X100.jpg"></div>
					<#elseif cuList.gender== 2>
						<div class="avatar"><img width="65" height="66" src="../img/ta/woman_100X100.jpg"></div>
					</#if>
				</#if>
					
						<p class="s14">${cuList.realName}</p>
					<div class="i_onlinecs_btnc c_b">
					
					
					<a href="tencent://message/?uin=${cuList.qq}&site=www.xx.com&menu=yes" target="_blank" rel="nofollow">
					<img src="http://wpa.qq.com/pa?p=1:${cuList.qq}:4" border="0" align="bottom" class="pic"></a>
					</div>
				</li>
			</@custList>

 还有一种循环方法

if(body!=null){
			//设置循环变量
			if(loopVars !=null && loopVars.length>0){
				List<TravelAgency> 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>
						<dl>
							<dd><a href="${contextPath}www/store.do?traAgCode=${trList.traAgCode?default('')}">${trList.companyInfo.companyName!""}</a></dd>
						</dl>
						</#list>
					<#else>
						<dl>
							<dd>暂时没有旅行社添加!</dd>
						</dl>
						
					</#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<String, String> paramMap=new HashMap<String, String>();
				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<TailPromotion> trpList=tailProService.findTP(tailPromotion,orderby,page, row);
				//这个是ajax的做法
				TreeMap<Integer, Integer> daysMap=new TreeMap<Integer, Integer>();//旅游天数
				HashMap trPropertyMap=new HashMap();//团的属性
				TreeMap<Integer, String> priceMap=new TreeMap<Integer, String>();//价格
				if(trpList!=null && trpList.size()>0){
					for(int i=0;i<trpList.size();i++){
						Integer day=trpList.get(i).getTourDays();
						if(day!=null && day!=0){
							daysMap.put(day,day);
						}
						Byte trPro=trpList.get(i).getTrProperty();
						if(trPro!=null){
							trPropertyMap.put(trPro, trPro);
						}
						Integer lowPrice=trpList.get(i).getLowestPrice();
						if(lowPrice!=null && lowPrice!=0){
							if(lowPrice<100){
								priceMap.put(100, "100元以下");
							}else if(lowPrice>100 && lowPrice<=300){
								priceMap.put(300, "100-300元");
							}else if(lowPrice>300 && lowPrice<=500){
								priceMap.put(500, "300-500元");
							}
						}
						
						
						
					}
				}
				
				//搜索条件 放到map里面
				Map<String,Map> sMap=new HashMap<String,Map>();
				
				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><!-- 含有多个循环变量的-->
	<!--  搜索区  -->
	<div class="dec_box">
			<#if sMap.get('daysMap')?size != 0>
			<#assign daysMap = sMap.get('daysMap')>   
			<dl>
				<dt>行程天数:</dt>
				<dd> 
					<#list daysMap.keySet() as key>
					<a href="${contextPath}zhoubian.do?cityCode=${cityCode}&cityName=${cityName}&templetPath=index_zb_wd_so.html&days=${key}<#if (price??)>&price=${price}</#if><#if (topicLabels??)>&topicLabels=${topicLabels}</#if><#if (sequence??)>&sequence=${sequence}</#if><#if (ptaNames??)>&ptaNames=${ptaNames}</#if><#if (trProperty??  && trProperty!='')>&trProperty=${trProperty}</#if><#if (transportMeans?? && transportMeans!='')>&transportMeans=${transportMeans}</#if>" class="<#if ((days!'') == key?string)>active</#if>" >${key}日游</a>
					</#list>
				</dd>
			</dl>
			</#if>
			<#if sMap.get('priceMap')?size != 0>
			<#assign priceMap = sMap.get('priceMap')> 
			<dl>
				<dt>价格范围:</dt>
				<dd>
					<#list priceMap.keySet() as key>
						<a href="${contextPath}zhoubian.do?cityCode=${cityCode}&cityName=${cityName}&templetPath=index_zb_wd_so.html&price=${key?c}<#if (days??)>&days=${days}</#if><#if (topicLabels??)>&topicLabels=${topicLabels}</#if><#if (sequence??)>&sequence=${sequence}</#if><#if (ptaNames??)>&ptaNames=${ptaNames}</#if><#if (trProperty??  && trProperty!='')>&trProperty=${trProperty}</#if><#if (transportMeans?? && transportMeans!='')>&transportMeans=${transportMeans}</#if>" class="<#if ((price!'') == key?c)>active</#if>" >${priceMap.get(key)}</a>
					</#list>
				</dd>
			</dl>
			</#if>
		
		</div>

	</div>
	
	
	<div class="search_sum">
		<strong class="s18 c_c">${cityName?if_exists}<#if (ptaNames??)>到${ptaNames}旅游 </#if> <#if (days??)>${days}日游</#if></strong> 
			共找到 <strong id="tCount" class="c_o"></strong> 条线路</div>

</div>


<div class="d_wrap selfclear">

	<div class="d_r">

		
		
	
	<div class="d_m dec_m">
	
		<!-- 搜索结果 -->
		<div class="dec_box selfclear">
			
			<div class="search_sort">
				<span class="active">默认排序</span>
				<span>
					<a href="${contextPath}zhoubian.do?cityCode=${cityCode}&cityName=${cityName}&templetPath=index_zb_wd_so.html&sequence=1<#if (topicLabels??)>&topicLabels=${topicLabels}</#if><#if (days??)>&days=${days}</#if><#if (price??)>&price=${price}</#if><#if (sequence??)>&sequence=${sequence}</#if><#if (ptaNames??)>&ptaNames=${ptaNames}</#if><#if (trProperty??  && trProperty!='')>&trProperty=${trProperty}</#if><#if (transportMeans?? && transportMeans!='')>&transportMeans=${transportMeans}</#if>">价格 <em class="icon_sort up"></em></a>&nbsp; 
					<a href="${contextPath}zhoubian.do?cityCode=${cityCode}&cityName=${cityName}&templetPath=index_zb_wd_so.html&sequence=2<#if (topicLabels??)>&topicLabels=${topicLabels}</#if><#if (days??)>&days=${days}</#if><#if (price??)>&price=${price}</#if><#if (sequence??)>&sequence=${sequence}</#if><#if (ptaNames??)>&ptaNames=${ptaNames}</#if><#if (trProperty??  && trProperty!='')>&trProperty=${trProperty}</#if><#if (transportMeans?? && transportMeans!='')>&transportMeans=${transportMeans}</#if>">价格<em class="icon_sort down"></em></a>
				</span>
				<form name="form1" method="post" action="" >
					<script type="text/javascript">
					function form1Submit(){
						var action="${contextPath}zhoubian.do?cityCode=${cityCode}&cityName=${cityName}&templetPath=index_zb_wd_so.html&sequence=1<#if (topicLabels??)>&topicLabels=${topicLabels}</#if><#if (days??)>&days=${days}</#if><#if (price??)>&price=${price}</#if><#if (sequence??)>&sequence=${sequence}</#if><#if (ptaNames??)>&ptaNames=${ptaNames}</#if>";
						
						var trProperty=$.trim($("#trProperty").val());
						
						if(trProperty!=''){
							
							action=action+"&trProperty="+trProperty;
						
						}else{}
						
						var transportMeans=$.trim($("#transportMeans").val());
						
						if(transportMeans!=''){
							
							action=action+"&transportMeans="+transportMeans;
						
						}else{}
						
						form1.action=action;
						
						form1.submit();
					}
					</script>
				<span>&nbsp;
					<select name="trProperty" id="trProperty" onchange="javascript:form1Submit();"> 
						<option value=''>出游方式</option>
						<option value='1' <#if ((trProperty!'') == '1')>selected="selected"</#if> >跟团游</option>
						<option value='2' <#if ((trProperty!'') == '2')>selected="selected"</#if> >自由行</option>
						<option value='3' <#if ((trProperty!'') == '3')>selected="selected"</#if> >自驾游</option>
						<option value='4' <#if ((trProperty!'') == '4')>selected="selected"</#if> >独立包团</option>
					</select>
				</span>
				<span>&nbsp;
					<select name="transportMeans" id="transportMeans"  onchange="javascript:form1Submit();">
						<option value=''>交通工具</option>
						<option value="飞机" <#if ((transportMeans!'') == '飞机')>selected="selected"</#if>>飞机</option>
	                    <option value="火车" <#if ((transportMeans!'') == '火车')>selected="selected"</#if>>火车</option>
	                    <option value="汽车" <#if ((transportMeans!'') == '汽车')>selected="selected"</#if>>汽车</option>
	                    <option value="轮船" <#if ((transportMeans!'') == '轮船')>selected="selected"</#if>>轮船</option>
	                    <option value="动车" <#if ((transportMeans!'') == '动车')>selected="selected"</#if>>动车</option>
	                    <option value="高铁" <#if ((transportMeans!'') == '高铁')>selected="selected"</#if>>高铁</option>
					</select>
				</span>
				</form>
			</div>

			<div class="search_c" style="display:block">
					<#list trpList as tour>
					<div class="search_route_c">
							<div class="g_pic"><a href="${tour.url!''}" target="_blank"><img src="${tour.picturePath!'/img/ta/whitelogo.jpg'}"></a></div>
							<div class="search_route_op">
								¥<strong class="s24 c_c">${tour.lowestPrice!''}</strong>起
								<a href="${tour.url!''}" target="_blank"><div class="search_c_goto">去看看</div></a>
							</div>
							<h3><a href="${tour.url!''}"  target="_blank" class="c_c">${tour.trName!''}</a></h3>
							<p><span class="c_lgray">行程概览: </span><span class="c_o">${tour.passedTourAttrNames!'暂无行程'}</span></p>
							<p><span class="c_lgray">往返交通: </span><span class="dec_mr">${tour.transportMeans!''}</span><span class="c_lgray">服务承诺:</span>
								<#if (tour.extracomsuption??)>
								<span class="c_g">
									<#if (tour.extracomsuption == 1)>
										无购物无自费
									<#elseif (tour.extracomsuption == 2)>
										无购物有自费
									<#elseif (tour.extracomsuption == 3)>
										有购物无自费
									<#elseif (tour.extracomsuption == 4)>
										有购物有自费
									</#if>
								</span>
								</#if>
								</p>
							<p>${tour.companyName!''}</p>
					</div>
					</#list>
			</div>
			
			<div class="search_c">
				<div class="g_loading"></div>
			</div>

		</div>
		<div class="g_page">
			${pager.formPageStr}
		</div>
	</div>
</@tailPage>

 

  • 大小: 11.2 KB
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics