龙盟编程博客 | 无障碍搜索 | 云盘搜索神器
快速搜索
主页 > web编程 > Javascript编程 >

基于AJAX和JSF打造丰富的互联网组件之应用篇(1)(3)

时间:2013-03-06 14:58来源:未知 作者:admin 点击:
分享到:
列表7.HtmlInputSuggestRenderer的encodeEnd()方法 public void encodeEnd( FacesContext context, UIComponent component) throws IOException { String valueString = _getValueAsString(context, compone
列表7.HtmlInputSuggestRenderer的encodeEnd()方法

public void encodeEnd(
FacesContext context,
UIComponent component) throws IOException
{
String valueString = _getValueAsString(context, component);
String clientId = component.getClientId(context);
Map attrs = component.getAttributes();
String title = (String)attrs.get(TITLE_ATTR);
String onchange = (String)attrs.get(ONCHANGE_ATTR);
MethodBinding doSuggest = (MethodBinding)attrs.get(DO_SUGGEST_ATTR);
ResponseWriter out = context.getResponseWriter();
out.startElement("div", component);
if (title != null)
out.writeAttribute("title", title, TITLE_ATTR);
// // value="[converted-value]" onchange="[onchange]" />
out.startElement("input", component);
out.writeAttribute("id", clientId, null);
out.writeAttribute("name", clientId, null);
if (valueString != null)
out.writeAttribute("value", valueString, null);
if (doSuggest != null)
{
//当使用服务器端建议时,禁止浏览器自动完成功能
out.writeAttribute("autocomplete", "off", null);
String expression = doSuggest.getExpressionString();
//从表达式中修整#{}
String bindingRef =
expression.substring(2, expression.length() - 1);
ViewHandler handler =
context.getApplication().getViewHandler();
String doSuggestURL =
handler.getResourceURL(context, "mabon:/" + bindingRef);
out.writeAttribute("onkeypress",
"return projsf.jdj.doKeyPress(event);", null);
out.writeAttribute("onkeyup",
"return projsf.jdj.doKeyUp(event);", null);
out.writeAttribute("onchange",
"projsf.jdj.doChange(event, '" + doSuggestURL
+ "');", null);
out.writeAttribute("onblur",
"return projsf.jdj.doBlur(event);", null);
}
out.endElement("input");
out.startElement("br", null);
out.endElement("br");
out.startElement("div", null);
out.writeAttribute("id", clientId + "$suggest", null);
out.writeAttribute("class", "HtmlInputSuggest", null);
out.endElement("div");
}

列表8.一个使用JSF HttpInputSuggest组件的JSP页面


http://java.sun.com/JSP/Page" version="1.2"
xmlns:jdj="http://projsf.apress.com/jdj"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:h="http://java.sun.com/jsf/html" >




ProJSF : HtmlInputSuggest



title="Input Suggest Component"
value="#{backingBean.value}"
doSuggest="#{backingBean.doSuggest}" />




列表9:支持bean的value属性

package com.apress.projsf.jdj.application;
import java.util.ArrayList;
import java.util.List;
/**
*BackingBean是一个inputSuggest.jspx文档的支持bean。
*/
public class BackingBean{
public void setValue(Object value) { _value = value; }
public Object getValue() { return _value; }

列表10.支持bean的doSuggest()方法

public String[] doSuggest(String initialValue) {
List suggestions = new ArrayList();
for (int i=0; i < _MASTER_LIST.length; i++) {
if (_MASTER_LIST[i].startsWith(initialValue))
suggestions.add(_MASTER_LIST[i]);
}
return suggestions.toArray(new String[0]);
}
private Object _value;
static private final String[] _MASTER_LIST = new String[]
{
"Pro JSF and Ajax",
"Pro Ajax",
"Pro JSP 2",
"Pro Jakarta",
"Pro J2EE 1.4"
};
}

精彩图集

赞助商链接