本文主要是介绍Click和Velocity浅析。,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
Click 简介信息
◆ 简单易学,
◆ 面向页面与组件设计,
◆ 基于事件编程模型,
◆ 自动表单验证,
◆ 使用Velocity模板作为页面视图,
◆ 漂亮的错误报告功能,
◆ 使用Rico(一个开源的JavaScript库,它提供对AJAX完全支持并可让Web应用程序支持拖放操作等其它功能)来对AJAX的支持等。
该项目主页:http://click.sourceforge.net/
其中也包含了Velocity模版引擎的介绍。
实例:
控制层的java代码如下:
package cn.edu.zju.grs.mobile.page.college;
import org.apache.click.ActionResult;
import org.apache.commons.lang.StringUtils;
import net.butfly.albacore.exception.BusinessException;
import cn.edu.zju.grs.alpaca.page.Action;
import cn.edu.zju.grs.alpaca.utils.JSONUtils;
import cn.edu.zju.grs.mobile.utils.BusStation;
import cn.edu.zju.grs.alufer.albert.mobile.facade.entity.CategoryResult;
import cn.edu.zju.grs.alufer.albert.mobile.facade.entity.MobilePushConfResult;
import cn.edu.zju.grs.alufer.albert.mobile.mapper.Category;
import cn.edu.zju.grs.alufer.albert.mobile.mapper.MobilePushConf;
import cn.edu.zju.grs.mobile.constant.ActionContext;
import cn.edu.zju.grs.mobile.constant.PageParameters;
import cn.edu.zju.grs.mobile.page.CollegeLayoutPage;
@Action(ActionContext.ACTION_COLLEGE_XXTS)
public class PushConfPageCollege extends CollegeLayoutPage{
private static final long serialVersionUID = 1336762764798794283L;
@Override
public void onInit() {
super.onInit();
String search = getContext().getRequestParameter("search");
if (search != null) {
pagingMap.put("page", "1");
}
readConfig();
searchData();
}
public void readConfig() {
try {
Category category = new Category();
CategoryResult result = BusStation.categoryFacade.selectCategoryList(category, 1, 100);
Category[] categorys = result.getCategoryList();
this.addModel("categorys", categorys);
} catch (BusinessException e) {
e.printStackTrace();
}
}
public void searchData() {
String categoryId = this.getContext().getRequestParameter("categoryId");
String pushType = this.getContext().getRequestParameter("pushType");
MobilePushConf mobilePushConf = new MobilePushConf();
if (StringUtils.isNotEmpty(categoryId)) {
mobilePushConf.setCategoryId(categoryId);
this.addModel("searchCategoryId", categoryId);
}
if (StringUtils.isNotEmpty(pushType)) {
mobilePushConf.setPushType(Long.valueOf(pushType));
this.addModel("searchPushType", pushType);
}
try {
int count = BusStation.mobilePushConfFacade.countMobilePushConf(mobilePushConf);
this.addModel("count", count);
if (count>0) {
MobilePushConfResult result = BusStation.mobilePushConfFacade.selectMobilePushConfList(mobilePushConf,
Integer.valueOf(pagingMap.get("pageId")),
Integer.parseInt((String) pagingMap.get("perPageNumber")));
pagingMap.put("per_page_num", String.valueOf(PageParameters.PERPAGE_MAX));
commonUtil.getPagingButton(count, pagingMap, pagingMap);
this.addModel("pagingMap", pagingMap);
MobilePushConf[] mobilePushConfs = result.getMobilePushConfList();
if (mobilePushConfs!=null && mobilePushConfs.length>0) {
this.addModel("mobilePushConfs", mobilePushConfs);
}
}
} catch (BusinessException e) {
e.printStackTrace();
}
}
/**
* 删除
* @return
*/
public ActionResult delete() {
//id
String id = this.getContext().getRequestParameter("id");
MobilePushConf mobilePushConf = new MobilePushConf();
if (!commonUtil.isNull(id)) {
mobilePushConf.setId(id);
mobilePushConf.setDeleted(true);
try {
BusStation.mobilePushConfFacade.deleteMobilePushConf(mobilePushConf);
} catch (Exception e) {
return new ActionResult(JSONUtils.toJSON("操作出现异常"),ActionResult.JSON);
}
}
return new ActionResult();
}
}
说明:
其中根据click.xml中的配置:
<page path="/page/mobile/college/showCategory.htm" classname="cn.edu.zju.grs.mobile.page.college.ShowCategory" />
,根据预定义的映射关系,在访问某个htm页面(此处为showCategory.htm)时,访问该页面后会自动执行该类的onInit()方法,可用于初始化该页面的数据。其中getContext().getRequestParameter("search");是从htm页面中获取名为search的字符串对象,this.addModel("mobilePushConfs", mobilePushConfs);方法往该thm页面中添加名为mobilePushConfs的对象,此处为list对象。
Velocity模板引擎的htm页面如下:
<script type="text/javascript" src="$context/res/js/mobile/college/categoryId.js"></script>
<div class="setup_main">
<div class="apply-content mt20">
<form id="insertForm" method="post" class="form-horizontal">
<ul>
<li>
<label class="control-label">类别名称:</label>
<select name="categoryId" id="categoryId" class="fl">
<option value="" selected="true">请选择</option>
#foreach($item in $categorys)
<option value="$item.id" #if($item.id == $searchCategoryId ) selected="selected" #end>$item.description</option>
#end
</select>
<input type="button" class="btn btn-primary ml40" name="register" id="register" value="注册提醒">
</li>
</ul>
</form>
</div>
</div>
===============
说明:
其中categorys为一个集合对象,此处为list集合。可以在页面中遍历该对象的所有属性和值。
循环语句#foreach($var in $arrays ) // 集合包含下面三种Vector, a Hashtable or an Array
#end
#foreach( $product in $allProducts )
<li>$product</li>
#end
#foreach( $key in $allProducts.keySet() )
<li>Key: $key -> Value: $allProducts.get($key)</li>
#end
#foreach( $customer in $customerList )
<tr><td>$velocityCount</td><td>$customer.Name</td></tr>
#end
这篇关于Click和Velocity浅析。的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!