Jeecms6中后台控制层Action如何将值传入前台视图层模板中的?

转载:https://blog.csdn.net/wsm201005030226/article/details/44343069

Jeecms后台控制层如何传值到前台freemarker的???

最近在研究Jeecms6的时候,发现一个令人费解的问题??举个例子,

 
 
 

点击“内容“选项后,出现上图下面的内容,那么,lz就来分析这个过程的后台处理流程

为了突出问题所在,lz就略去了前面浏览器发送请求到控制层的过程,直接来看控制层。。。。

处理该部分业务逻辑的代买如下:

@RequiresPermissions("content:v_list")
@RequestMapping("/content/v_list.do")
public String list(String queryStatus, Integer queryTypeId,
Boolean queryTopLevel, Boolean queryRecommend,
Integer queryOrderBy, Integer cid, Integer pageNo,
HttpServletRequest request, ModelMap model) {
long time = System.currentTimeMillis();
String queryTitle = RequestUtils.getQueryParam(request, "queryTitle");
queryTitle = StringUtils.trim(queryTitle);
String queryInputUsername = RequestUtils.getQueryParam(request,
"queryInputUsername");
queryInputUsername = StringUtils.trim(queryInputUsername);
if (queryTopLevel == null) {
queryTopLevel = false;
}
if (queryRecommend == null) {
queryRecommend = false;
}
if (queryOrderBy == null) {
queryOrderBy = 4;
}
ContentStatus status;
if (!StringUtils.isBlank(queryStatus)) {
status = ContentStatus.valueOf(queryStatus);
} else {
status = ContentStatus.all;
}
Integer queryInputUserId = null;
if (!StringUtils.isBlank(queryInputUsername)) {
CmsUser u = cmsUserMng.findByUsername(queryInputUsername);
if (u != null) {
queryInputUserId = u.getId();
} else {
// 用户名不存在,清空。
queryInputUsername = null;
}
}
CmsSite site = CmsUtils.getSite(request);
Integer siteId = site.getId();
CmsUser user = CmsUtils.getUser(request);
Integer userId = user.getId();
byte currStep = user.getCheckStep(siteId);
Pagination p = manager.getPageByRight(queryTitle, queryTypeId,user.getId(),
queryInputUserId, queryTopLevel, queryRecommend, status, user
.getCheckStep(siteId), siteId, cid, userId,
queryOrderBy, cpn(pageNo), CookieUtils.getPageSize(request));
List<ContentType> typeList = contentTypeMng.getList(true);
List<CmsModel> models=cmsModelMng.getList(false, true);
if(cid!=null){
Channel c=channelMng.findById(cid);
models=c.getModels(models);
}
model.addAttribute("pagination", p);
model.addAttribute("cid", cid);
model.addAttribute("typeList", typeList);
model.addAttribute("currStep", currStep);
model.addAttribute("site", site);
model.addAttribute("models", models);
addAttibuteForQuery(model, queryTitle, queryInputUsername, queryStatus,
queryTypeId, queryTopLevel, queryRecommend, queryOrderBy,
pageNo);
time = System.currentTimeMillis() - time;
return "content/list";
}

该段代码最后返回是一个字符串,框架会根据配置文件内容转入content目录下list.html(lz不知道这里为什么要使用html而不是ftl),

前台页面上是如何显示的呢?传入部分代码:

......

<@p.table value=pagination;content,i,has_next><#rt/>
<@p.column title="<input type='checkbox' οnclick='Pn.checkbox("ids",this.checked)'/>" width="25" align="center">
<input type='checkbox' name='ids' value='${content.id}' οnclick="Pn.selectCheckBox('${content.id}',this.checked)"/><#t/>
</@p.column><#t/>
<@p.column title="ID" align="center">${content.id}</@p.column><#t/>
<@p.column code="content.title">

......

可以看到这里是通过$(content.*)来取得要显示的值的,那么问题就来了

lz遍寻后台Action文件都找不到该content是在何处声明的,又是在何处赋予查询结果值?????

苦思不得琪姐。。。。。。。。

既然这个系统时能够正常使用的,也就是说,代码是肯定没有问题的,那么肯定就是没有找到content是如何实现的?

经过不懈的努力发现,在list.html中有这么一句话,如下图在326行

在这一行,使用jeecms开发人员定义的<@p.table>标签,那么它如何定义的呢?碾转反侧(查找标签ftl过程略),找到table.ftl标签文件,如下:

我们可以看到前面4行,后面不重要,不理解也没关系,第一个值value就指的是用于显示列表数据,

OK,哈哈哈,那么我们就可以理解

<@p.table value=pagination;content,i,has_next><#rt/>

这句话的意思

听我细细解释:首先后台pagination是后台Action定义,有疑问的同学可以去jeecms6后台代码查看,在这里将pagination赋值给table的value,table就是一个集合对象

需要在前台展示集合对象的数据,就使用循环来展示,这里在<@p.table>标签文件table.ftl中自定义了循环格式(有兴趣的同学可以去研究一下该标签文件),如果硬要把

分号后面的内容”content,i,has_next“的做个java翻译的话,我像应该是这样:

List list=pagination;

ListIterator iterator=list.listIterator();

while(iterator.hasnext()){

Content content=list.get(i);

i++;

iterator.next();

}

这样理解是不是就容易多了。。。。。。

那么i就是指的这个集合遍历的过程,从集合开始到最后,

content是集合每一轮遍历后得到的Content对象

has_next查看是否遍历都集合末尾

那么经过这样的遍历,前台模板文件中使用诸如content.*形式的值也不难理解了

本次通过这么一个小小例子来解读了Jeecms6后台向前台传值的过程,希望对有需要的同学帮助理解

原文地址:https://www.cnblogs.com/Jeely/p/11214434.html