一个界面控件有时候需要引用额外的脚本或样式等资源,这些资源需要加入到HTML的Head部分。
在toHtml方法中加入
界面框架约定了界面控件通过toHtml方法生成HTML代码片段,而这些代码片段一般包含在HTML的body部分,下面的代码演示了如何在toHtml中添加head部分的内容。
if(actionContext.get("heads") != null){ boolean haveEditor = false; //heads的结构 List<Map<String, String>> heads = (List<Map<String, String>>) actionContext.get("heads"); for(Map<String, String> head : heads){ //自定义一个key if("EpicEditor".equals(head.get("name"))){ haveEditor = true; break; } } if(!haveEditor){ //如果已经添加了则不重复添加,可能出现同一个页面引用两次控件等情况 String html = null; String epicPath = self.getString("epicPath"); if(self.getBoolean("useMinJs")){ html = " <script type=\"text/javascript\" src=\"" + epicPath + "js/epiceditor.min.js\"></script>\n"; }else{ html = " <script type=\"text/javascript\" src=\"" + epicPath + "js/epiceditor.js\"></script>\n"; } //加入d哦heads中 Map<String, String> head = new HashMap<String, String>(); head.put("name", "EpicEditor"); head.put("value", html); heads.add(head); } }
在动作中加入
界面框架也提供了添加资源到heads中的方法,如下图:
Copyright © 2007-2019 XWorker.org 版权所有 沪ICP备08000575号