📄 htmlrendererutils.java
字号:
} Set lookupSet; boolean useSubmittedValue; if (selectMany) { UISelectMany uiSelectMany = (UISelectMany) uiComponent; lookupSet = RendererUtils.getSubmittedValuesAsSet(facesContext, uiComponent, converter, uiSelectMany); if (lookupSet == null) { useSubmittedValue = false; lookupSet = RendererUtils.getSelectedValuesAsSet(facesContext, uiComponent, converter, uiSelectMany); } else { useSubmittedValue = true; } } else { UISelectOne uiSelectOne = (UISelectOne) uiComponent; Object lookup = uiSelectOne.getSubmittedValue(); if (lookup == null) { useSubmittedValue = false; lookup = uiSelectOne.getValue(); } else { useSubmittedValue = true; } String lookupString = RendererUtils.getConvertedStringValue(facesContext, uiComponent, converter, lookup); lookupSet = Collections.singleton(lookupString); } renderSelectOptions(facesContext, uiComponent, converter, lookupSet, useSubmittedValue, selectItemList); // bug #970747: force separate end tag writer.writeText("", null); writer.endElement(HTML.SELECT_ELEM); } /** * Renders the select options for a <code>UIComponent</code> that is * rendered as an HTML select element. * * @param context * the current <code>FacesContext</code>. * @param component * the <code>UIComponent</code> whose options need to be * rendered. * @param converter * <code>component</code>'s converter * @param lookupSet * the <code>Set</code> to use to look up selected options * @param useSubmittedValue * whether we are using the submittedValue * @param selectItemList * the <code>List</code> of <code>SelectItem</code> s to be * rendered as HTML option elements. * @throws IOException */ private static void renderSelectOptions(FacesContext context, UIComponent component, Converter converter, Set lookupSet, boolean useSubmittedValue, List selectItemList) throws IOException { ResponseWriter writer = context.getResponseWriter(); for (Iterator it = selectItemList.iterator(); it.hasNext();) { SelectItem selectItem = (SelectItem) it.next(); if (selectItem instanceof SelectItemGroup) { writer.startElement(HTML.OPTGROUP_ELEM, null); writer.writeAttribute(HTML.LABEL_ATTR, selectItem.getLabel(), null); SelectItem[] selectItems = ((SelectItemGroup) selectItem) .getSelectItems(); renderSelectOptions(context, component, converter, lookupSet, useSubmittedValue, Arrays.asList(selectItems)); writer.endElement(HTML.OPTGROUP_ELEM); } else { String itemStrValue = RendererUtils.getConvertedStringValue(context, component, converter, selectItem); writer.write("\t\t"); writer.startElement(HTML.OPTION_ELEM, null); if (itemStrValue != null) { writer.writeAttribute(HTML.VALUE_ATTR, itemStrValue, null); } if (lookupSet.contains(itemStrValue)) { //TODO/FIX: we always compare the String vales, better fill lookupSet with Strings only when useSubmittedValue==true, else use the real item value Objects writer.writeAttribute(HTML.SELECTED_ATTR, HTML.SELECTED_ATTR, null); } if (selectItem.isDisabled()) { writer.writeAttribute(HTML.DISABLED_ATTR, HTML.DISABLED_ATTR, null); } writer.writeText(selectItem.getLabel(), null); writer.endElement(HTML.OPTION_ELEM); } } } /* * public static void renderRadio(FacesContext facesContext, UIInput * uiComponent, String value, String label, boolean checked) throws * IOException { String clientId = uiComponent.getClientId(facesContext); * * ResponseWriter writer = facesContext.getResponseWriter(); * * writer.startElement(HTML.INPUT_ELEM, uiComponent); * writer.writeAttribute(HTML.TYPE_ATTR, HTML.INPUT_TYPE_RADIO, null); * writer.writeAttribute(HTML.NAME_ATTR, clientId, null); * writer.writeAttribute(HTML.ID_ATTR, clientId, null); * * if (checked) { writer.writeAttribute(HTML.CHECKED_ATTR, * HTML.CHECKED_ATTR, null); } * * if ((value != null) && (value.length() > 0)) { * writer.writeAttribute(HTML.VALUE_ATTR, value, null); } * * renderHTMLAttributes(writer, uiComponent, * HTML.INPUT_PASSTHROUGH_ATTRIBUTES); renderDisabledOnUserRole(writer, * uiComponent, facesContext); * * if ((label != null) && (label.length() > 0)) { * writer.write(HTML.NBSP_ENTITY); writer.writeText(label, null); } * * writer.endElement(HTML.INPUT_ELEM); } */ public static void writePrettyLineSeparator(FacesContext facesContext) throws IOException { if (MyfacesConfig.getCurrentInstance(facesContext.getExternalContext()) .isPrettyHtml()) { facesContext.getResponseWriter().write(LINE_SEPARATOR); } } public static void writePrettyIndent(FacesContext facesContext) throws IOException { if (MyfacesConfig.getCurrentInstance(facesContext.getExternalContext()) .isPrettyHtml()) { facesContext.getResponseWriter().write('\t'); } } /** * @return true, if the attribute was written * @throws java.io.IOException */ public static boolean renderHTMLAttribute(ResponseWriter writer, String componentProperty, String attrName, Object value) throws IOException { if (!RendererUtils.isDefaultAttributeValue(value)) { // render JSF "styleClass" attribute as "class" String htmlAttrName = attrName.equals(HTML.STYLE_CLASS_ATTR) ? HTML.CLASS_ATTR : attrName; writer.writeAttribute(htmlAttrName, value, componentProperty); return true; } return false; } /** * @return true, if the attribute was written * @throws java.io.IOException */ public static boolean renderHTMLAttribute(ResponseWriter writer, UIComponent component, String componentProperty, String htmlAttrName) throws IOException { Object value = component.getAttributes().get(componentProperty); return renderHTMLAttribute(writer, componentProperty, htmlAttrName, value); } /** * @return true, if an attribute was written * @throws java.io.IOException */ public static boolean renderHTMLAttributes(ResponseWriter writer, UIComponent component, String[] attributes) throws IOException { boolean somethingDone = false; for (int i = 0, len = attributes.length; i < len; i++) { String attrName = attributes[i]; if (renderHTMLAttribute(writer, component, attrName, attrName)) { somethingDone = true; } } return somethingDone; } public static boolean renderHTMLAttributeWithOptionalStartElement( ResponseWriter writer, UIComponent component, String elementName, String attrName, Object value, boolean startElementWritten) throws IOException { if (!RendererUtils.isDefaultAttributeValue(value)) { if (!startElementWritten) { writer.startElement(elementName, component); startElementWritten = true; } renderHTMLAttribute(writer, attrName, attrName, value); } return startElementWritten; } public static boolean renderHTMLAttributesWithOptionalStartElement( ResponseWriter writer, UIComponent component, String elementName, String[] attributes) throws IOException { boolean startElementWritten = false; for (int i = 0, len = attributes.length; i < len; i++) { String attrName = attributes[i]; Object value = component.getAttributes().get(attrName); if (!RendererUtils.isDefaultAttributeValue(value)) { if (!startElementWritten) { writer.startElement(elementName, component); startElementWritten = true; } renderHTMLAttribute(writer, attrName, attrName, value); } } return startElementWritten; } public static boolean renderOptionalEndElement(ResponseWriter writer, UIComponent component, String elementName, String[] attributes) throws IOException { boolean endElementNeeded = false; for (int i = 0, len = attributes.length; i < len; i++) { String attrName = attributes[i]; Object value = component.getAttributes().get(attrName); if (!RendererUtils.isDefaultAttributeValue(value)) { endElementNeeded = true; break; } } if (endElementNeeded) { writer.endElement(elementName); return true; } return false; } public static void writeIdIfNecessary(ResponseWriter writer, UIComponent component, FacesContext facesContext) throws IOException { if(component.getId()!=null && !component.getId().startsWith(UIViewRoot.UNIQUE_ID_PREFIX)) { writer.writeAttribute(HTML.ID_ATTR, component.getClientId(facesContext),null); } } public static class LinkParameter { private String _name; private Object _value; public String getName() { return _name; } public void setName(String name) { _name = name; } public Object getValue() { return _value; } public void setValue(Object value) { _value = value; } } public static void renderHiddenCommandFormParams(ResponseWriter writer, Set dummyFormParams) throws IOException { for (Iterator it = dummyFormParams.iterator(); it.hasNext();) { writer.startElement(HTML.INPUT_ELEM, null); writer.writeAttribute(HTML.TYPE_ATTR, "hidden", null); writer.writeAttribute(HTML.NAME_ATTR, it.next(), null); writer.endElement(HTML.INPUT_ELEM); } } /** * Render the javascript function that is called on a click on a commandLink * to clear the hidden inputs. This is necessary because on a browser back, * each hidden input still has it's old value (browser cache!) and therefore * a new submit would cause the according action once more! * * @param writer * @param formName * @param dummyFormParams * @param formTarget * @throws IOException */ public static void renderClearHiddenCommandFormParamsFunction( ResponseWriter writer, String formName, Set dummyFormParams, String formTarget) throws IOException { //render the clear hidden inputs javascript function String functionName = getClearHiddenCommandFormParamsFunctionName(formName); writer.startElement(HTML.SCRIPT_ELEM, null); writer.writeAttribute(HTML.TYPE_ATTR, "text/javascript", null); writer.write("\n<!--"); writer.write("\nfunction "); writer.write(functionName); writer.write("() {"); if (dummyFormParams != null) { writer.write("\n var f = document.forms['"); writer.write(formName); writer.write("'];"); for (Iterator it = dummyFormParams.iterator(); it.hasNext();) { writer.write("\n f.elements['"); writer.write((String) it.next()); writer.write("'].value=null;"); } } // clear form target writer.write("\n f.target="); if (formTarget == null || formTarget.length() == 0) { //Normally one would think that setting target to null has the //desired effect, but once again IE is different... //Setting target to null causes IE to open a new window! writer.write("'';"); } else { writer.write("'"); writer.write(formTarget); writer.write("';"); } writer.write("\n}"); //Just to be sure we call this clear method on each load. //Otherwise in the case, that someone submits a form by pressing Enter //within a text input, the hidden inputs won't be cleared! writer.write("\n"); writer.write(functionName); writer.write("();"); writer.write("\n//-->\n"); writer.endElement(HTML.SCRIPT_ELEM); } /** * Prefixes the given String with "clear_" and removes special characters * * @param formName * @return */ public static String getClearHiddenCommandFormParamsFunctionName( String formName) { return "clear_" + JavascriptUtils.getValidJavascriptName(formName, false); } public static String getFormName(UIComponent component, FacesContext context) { //Find form UIComponent parent = component.getParent(); while (parent != null && !(parent instanceof UIForm)) { parent = parent.getParent(); } if (parent != null) { //link is nested inside a form return ((UIForm) parent).getClientId(context); } //not nested in form, we must add a dummy form at the end of the // document return DummyFormUtils.DUMMY_FORM_NAME; } public static String getHiddenCommandLinkFieldName(String formName) { return formName + NamingContainer.SEPARATOR_CHAR + HIDDEN_COMMANDLINK_FIELD_NAME; }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -