论坛首页 Java企业应用论坛

如何把值传递给velocity定义的模板?在模板中又怎样获得值?

浏览 3956 次
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
作者 正文
   发表时间:2008-02-18  

在使用Spring+Hibernate+Webwork2+DWR做一个项目,里面要使用一个三级联动的地区控件,就使用Velocity封装了一个模板,

<nobr>
#*
	地区编码规则:省份2位编码
			    市4位
			    区/县6位
*#
##parse("/template/xhtml/controlheader.vm")
##获得页面控件的name
#set ($areaControl=$parameters.name)
##获得需要初始化的值
#set ($areaNumID=$parameters.value) ##问题出在这里?
<select name="${areaControl}_provience" onchange="${areaControl}_initCity()">
	<option value="">请选择省份</option></select>
	<select name="${areaControl}_city" onchange="${areaControl}_initTown()">
	<option value="">请选择城市</option></select>
	<select name="${areaControl}_town" onchange="${areaControl}_initValue()">
	<option value="" >请选择区县</option></select>
	<input type="hidden" name="${areaControl}" 
	##判断是否有初始化值
	#if($parameters.value)
		value="$parameters.value"
	#else 
		value=""
	#end 
	>
</nobr>
<script language="javascript">
		var ${areaControl}_value=document.getElementById("${areaControl}").value;
  		##初始化省份信息
  		function ${areaControl}_initProvince()
  		{
  			AreaDeal.getProvince(${areaControl}_dealProvince);
  		}
  		##省份信息的回调函数
  		function ${areaControl}_dealProvince(data)
  		{
  			dwr.util.addOptions("${areaControl}_provience",data,'id','areaname');
  		}
  		##初始化城市信息
  		function ${areaControl}_initCity()
  		{
  			
  			if(document.getElementById("${areaControl}_provience").value!="")
  			{
  				AreaDeal.getCity(document.getElementById("${areaControl}_provience").value,${areaControl}_dealCity);
  				dwr.util.removeAllOptions("${areaControl}_town");
  				dwr.util.addOptions("${areaControl}_town",[{name:'请选择区县',value:""}],'value','name');
  			}
  			document.getElementById("${areaControl}_city").value="";
  			${areaControl}_initValue();
  		}
  		##城市信息的回调函数
  		function ${areaControl}_dealCity(data)
  		{
  			dwr.util.removeAllOptions("${areaControl}_city");
  			dwr.util.addOptions("${areaControl}_city",[{name:'请选择城市',value:""}],'value','name');
  			dwr.util.addOptions("${areaControl}_city",data,'id','areaname');	
  		}
  		##初始化区县信息
  		function ${areaControl}_initTown()
  		{
  			if(document.getElementById("${areaControl}_city").value!="")
  			{
  				AreaDeal.getTown(document.getElementById("${areaControl}_city").value,${areaControl}_dealTown);
  			}
  			document.getElementById("${areaControl}_town").value="";
  			${areaControl}_initValue();
  		}
  		##区县信息的回调函数
  		function ${areaControl}_dealTown(data)
  		{
  			dwr.util.removeAllOptions("${areaControl}_town");
  			dwr.util.addOptions("${areaControl}_town",[{name:'请选择区县',value:""}],'value','name');
  			dwr.util.addOptions("${areaControl}_town",data,'id','areaname');
  		}
  		##把地区信息保存在隐藏域里
  		function ${areaControl}_initValue()
  		{
  			if(document.getElementById("${areaControl}_town").value!="")
  				document.getElementById("${areaControl}").value=document.getElementById("${areaControl}_town").value;
  			else if(document.getElementById("${areaControl}_city").value!="")
  				document.getElementById("${areaControl}").value=document.getElementById("${areaControl}_city").value;
  			else if(document.getElementById("${areaControl}_provience").value!="")
  				document.getElementById("${areaControl}").value=document.getElementById("${areaControl}_provience").value;
  			else document.getElementById("${areaControl}").value="";
  		}
  		##初始化有初始地区编号的控件
  		function ${areaControl}_hasValue()
		{
			 ${areaControl}_initProvince();
			if(areaNumValue.length>0 && areaNumValue.length<7)
			{
				document.getElementById("${areaControl}_provience").value=${areaControl}_value.substring(0,2);
			}
			if(areaNumValue.length>2 && areaNumValue.length<7)
			{
				 ${areaControl}_initCity();
				document.getElementById("${areaControl}_city").value=${areaControl}_value.substring(0,4);
			}
			if(areaNumValue.length>2 && areaNumValue.length<7)
			{
				 ${areaControl}_initTown();
				document.getElementById("${areaControl}_town").value=${areaControl}_value;
			}
		}
	#if(${areaNumID})
		document.onLoad=${areaControl}_hasValue();
	#else 
		document.onLoad=${areaControl}_initProvince();
	#end 
</script>
##parse("/template/xhtml/controlfooter.vm")
</nobr>

页面上调用模板的代码为:

<ww:textfield name="areaNum" value="110501" template="AreaControl.vm" theme="xhtml"  ></ww:textfield>

 

取值没有问题,但是当需要把获取的地区编号传到控件里时怎么也不能使控件选取到需要的状态,那位知道怎么回事,指定一下,先谢过了!

   发表时间:2008-02-19  
你每次提交都要保存好,每个菜单上的当前的值,否则就会丢失。
0 请登录后投票
   发表时间:2008-02-19  

我修改了前台,使用<ww:param>标签把值传到了模板中,这样在模板里可以取到值

<ww:textfield name="areaNum"  template="AreaControl.vm" theme="xhtml"  >
	<ww:param name="areaValue" value="110501"/>
</ww:textfield>

模板也作了修改,内容为:

<nobr>
#*
	地区编码规则:省份2位编码
			    市4位
			    区/县6位
*#
##parse("/template/xhtml/controlheader.vm")
##获得页面控件的name
#set ($areaControl=$parameters.name)
##获得需要初始化的值
#set ($areaValue=$parameters.areaValue)
<select name="${areaControl}_provience" onchange="${areaControl}_initCity()">
	<option value="">请选择省份</option></select>
<select name="${areaControl}_city" onchange="${areaControl}_initTown()">
	<option value="">请选择城市</option></select>
<select name="${areaControl}_town" onchange="${areaControl}_initValue()">
	<option value="" >请选择区县</option></select>
<input type="hidden" name="${areaControl}" 
	##判断是否有初始化值
	#if(${areaValue})
		value="${areaValue}"
	#else 
		value=""
	#end 
	>
</nobr>
<script language="javascript">
		var ${areaControl}_value="${areaValue}";
  		##初始化省份信息
  		function ${areaControl}_initProvince()
  		{
  			AreaDeal.getProvince(${areaControl}_dealProvince);
  		}
  		##省份信息的回调函数
  		function ${areaControl}_dealProvince(data)
  		{
  			dwr.util.addOptions("${areaControl}_provience",data,'id','areaname');
  		}
  		##初始化城市信息
  		function ${areaControl}_initCity()
  		{
  			
  			if(document.getElementById("${areaControl}_provience").value!="")
  			{
  				AreaDeal.getCity(document.getElementById("${areaControl}_provience").value,${areaControl}_dealCity);
  				dwr.util.removeAllOptions("${areaControl}_town");
  				dwr.util.addOptions("${areaControl}_town",[{name:'请选择区县',value:""}],'value','name');
  			}
  			
  				document.getElementById("${areaControl}_city").value="";
  				${areaControl}_initValue();
  					
  		}
  		##城市信息的回调函数
  		function ${areaControl}_dealCity(data)
  		{
  			dwr.util.removeAllOptions("${areaControl}_city");
  			dwr.util.addOptions("${areaControl}_city",[{name:'请选择城市',value:""}],'value','name');
  			dwr.util.addOptions("${areaControl}_city",data,'id','areaname');	
  		}
  		##初始化区县信息
  		function ${areaControl}_initTown()
  		{
  			if(document.getElementById("${areaControl}_city").value!="")
  			{
  				AreaDeal.getTown(document.getElementById("${areaControl}_city").value,${areaControl}_dealTown);
  			}
  			
  			document.getElementById("${areaControl}_town").value="";
  			${areaControl}_initValue();
  			
  		}
  		##区县信息的回调函数
  		function ${areaControl}_dealTown(data)
  		{
  			dwr.util.removeAllOptions("${areaControl}_town");
  			dwr.util.addOptions("${areaControl}_town",[{name:'请选择区县',value:""}],'value','name');
  			dwr.util.addOptions("${areaControl}_town",data,'id','areaname');
  		}
  		##把地区信息保存在隐藏域里
  		function ${areaControl}_initValue()
  		{
  			if(document.getElementById("${areaControl}_town").value!="")
  				document.getElementById("${areaControl}").value=document.getElementById("${areaControl}_town").value;
  			else if(document.getElementById("${areaControl}_city").value!="")
  				document.getElementById("${areaControl}").value=document.getElementById("${areaControl}_city").value;
  			else if(document.getElementById("${areaControl}_provience").value!="")
  				document.getElementById("${areaControl}").value=document.getElementById("${areaControl}_provience").value;
  			else document.getElementById("${areaControl}").value="";
  		}
  		##初始化有初始地区编号的控件
  		function ${areaControl}_hasValue()
		{
			${areaControl}_initProvince();
			if(${areaControl}_value.length>0 && ${areaControl}_value.length<7)
			{
				dwr.util.setValue("${areaControl}_provience",${areaControl}_value.substring(0,2));//现在只有这里不工作,为什么?
				AreaDeal.getCity(${areaControl}_value.substring(0,2),${areaControl}_dealCity);
			}
			if(${areaControl}_value.length>2 && ${areaControl}_value.length<7)
			{
				AreaDeal.getTown(${areaControl}_value.substring(0,4),${areaControl}_dealTown);
				dwr.util.setValue("${areaControl}_city",${areaControl}_value.substring(0,4));//这里也不工作
			}
			if(${areaControl}_value.length>4 && ${areaControl}_value.length<7)
			{
				dwr.util.setValue("${areaControl}_town",${areaControl}_value);//还有这里
			}
		}
	#if(${areaValue})
		document.onLoad=${areaControl}_hasValue();
	#else 
		document.onLoad=${areaControl}_initProvince();
	#end 
</script>
##parse("/template/xhtml/controlfooter.vm")
</nobr>

根据页面出过来的值,省、市和区县都进行了初始化:效果图

问题是该选中的项还是没有被选中,这是为什么?

0 请登录后投票
   发表时间:2008-02-20  
问题终于解决了,是因为DWR远程调用返回的时间差导致,等有空了在Blog里把这个问题整理一下
0 请登录后投票
论坛首页 Java企业应用版

跳转论坛:
Global site tag (gtag.js) - Google Analytics