【发布时间】:2014-08-22 00:58:46
【问题描述】:
我正在开发一个 JSF 自定义组件,使用我在以下书籍 Pro JSF and HTML5 by Apress 中找到的信息。
到目前为止,我成功开发了:
- 获取要在组件中渲染的数据的java类
- java 组件类
- java 渲染器类
- 标签库文件
- 呈现标签库的示例页面
一切正常,组件渲染成功。
现在我想为渲染的元素添加javascript事件和行为,更具体地说,我的自定义组件的目的是在网页上渲染一个菜单,我想广告菜单项的下拉效果。我知道如何用 JavaScript 编写整个代码,但我不知道的是:
向自定义组件中呈现的元素添加 javascript 事件和行为的最佳做法是什么?
JS文件应该放在哪里?如何将事件绑定到元素?是在渲染类中完成,还是在网页上完成?
谢谢,如果需要,我愿意提供有关我的代码的更具体信息。
Java 组件类
注意: CosmoMenu 类只是一个 bean。它基本上存储了一个菜单树(一个标签、一个 id 和一组子项,如果有的话)。
package components;
import com.google.gson.Gson;
import com.google.gson.JsonElement;
import com.google.gson.JsonParser;
import domain.CosmoMenu;
import javax.faces.component.FacesComponent;
import javax.faces.component.UIComponentBase;
@FacesComponent(CosmoMenuComponent.COMPONENT_TYPE)
public class CosmoMenuComponent extends UIComponentBase{
/** Component family of {@link CosmoMenuComponent}. */
public static final String COMPONENT_FAMILY = "CosmoMenu";
/** Component type of {@link CosmoMenuComponent}. */
public static final String COMPONENT_TYPE = "CosmoMenu";
@Override
public String getFamily(){
return CosmoMenuComponent.COMPONENT_FAMILY;
}
private CosmoMenu theMenu;
public CosmoMenu getMenu(){
Gson gson = new Gson();
JsonParser jsonParser = new JsonParser();
CosmoMenuAPI myApi = new CosmoMenuAPI();
String strMenu = myApi.getMenu();
JsonElement jEl = jsonParser.parse(strMenu);
theMenu = gson.fromJson(jEl, CosmoMenu.class);
return theMenu;
}
}
【问题讨论】:
-
你能发布你的组件吗? :) 只需要了解如何生成示例。
-
如果您也需要渲染器或其他任何东西,请告诉我。
标签: javascript jsf custom-component taglib