1 16 package org.apache.myfaces.renderkit.html; 17 18 import org.apache.myfaces.config.MyfacesConfig; 19 import org.apache.myfaces.renderkit.RendererUtils; 20 import org.apache.myfaces.renderkit.html.util.JavascriptUtils; 21 22 import javax.faces.application.ViewHandler; 23 import javax.faces.component.UIComponent; 24 import javax.faces.component.UIForm; 25 import javax.faces.component.html.HtmlForm; 26 import javax.faces.context.FacesContext; 27 import javax.faces.context.ResponseWriter; 28 import java.io.IOException ; 29 import java.util.HashSet ; 30 import java.util.Map ; 31 import java.util.Set ; 32 33 68 public class HtmlFormRendererBase 69 extends HtmlRenderer 70 { 71 73 private static final String HIDDEN_SUBMIT_INPUT_SUFFIX = "_SUBMIT"; 74 private static final String HIDDEN_SUBMIT_INPUT_VALUE = "1"; 75 76 private static final String HIDDEN_COMMAND_INPUTS_SET_ATTR 77 = HtmlFormRendererBase.class.getName() + ".HIDDEN_COMMAND_INPUTS_SET"; 78 79 80 public void encodeBegin(FacesContext facesContext, UIComponent component) 81 throws IOException 82 { 83 RendererUtils.checkParamValidity(facesContext, component, UIForm.class); 84 85 UIForm htmlForm = (HtmlForm)component; 86 87 ResponseWriter writer = facesContext.getResponseWriter(); 88 ViewHandler viewHandler = facesContext.getApplication().getViewHandler(); 89 String viewId = facesContext.getViewRoot().getViewId(); 90 String clientId = htmlForm.getClientId(facesContext); 91 String actionURL = viewHandler.getActionURL(facesContext, viewId); 92 93 writer.startElement(HTML.FORM_ELEM, htmlForm); 94 writer.writeAttribute(HTML.ID_ATTR, clientId, null); 95 writer.writeAttribute(HTML.NAME_ATTR, clientId, null); 96 writer.writeAttribute(HTML.METHOD_ATTR, "post", null); 97 writer.writeURIAttribute(HTML.ACTION_ATTR, 98 facesContext.getExternalContext().encodeActionURL(actionURL), 99 null); 100 101 HtmlRendererUtils.renderHTMLAttributes(writer, htmlForm, HTML.FORM_PASSTHROUGH_ATTRIBUTES); 102 writer.write(""); } 104 105 106 public void encodeEnd(FacesContext facesContext, UIComponent component) 107 throws IOException 108 { 109 ResponseWriter writer = facesContext.getResponseWriter(); 110 111 ViewHandler viewHandler = facesContext.getApplication().getViewHandler(); 113 viewHandler.writeState(facesContext); 114 115 writer.startElement(HTML.INPUT_ELEM, null); 117 writer.writeAttribute(HTML.TYPE_ATTR, "hidden", null); 118 writer.writeAttribute(HTML.NAME_ATTR, component.getClientId(facesContext) + 119 HIDDEN_SUBMIT_INPUT_SUFFIX, null); 120 writer.writeAttribute(HTML.VALUE_ATTR, HIDDEN_SUBMIT_INPUT_VALUE, null); 121 writer.endElement(HTML.INPUT_ELEM); 122 123 if (MyfacesConfig.getCurrentInstance(facesContext.getExternalContext()).isAutoScroll()) 124 { 125 JavascriptUtils.renderAutoScrollHiddenInput(writer); 126 } 127 128 Set set = (Set )component.getAttributes().get(HIDDEN_COMMAND_INPUTS_SET_ATTR); 130 if (set != null && !set.isEmpty()) 131 { 132 HtmlRendererUtils.renderHiddenCommandFormParams(writer, set); 133 134 String target; 135 if (component instanceof HtmlForm) 136 { 137 target = ((HtmlForm)component).getTarget(); 138 } 139 else 140 { 141 target = (String )component.getAttributes().get(HTML.TARGET_ATTR); 142 } 143 HtmlRendererUtils.renderClearHiddenCommandFormParamsFunction(writer, 144 component.getClientId(facesContext), 145 set, 146 target); 147 } 148 149 writer.endElement(HTML.FORM_ELEM); 150 } 151 152 153 public void decode(FacesContext facesContext, UIComponent component) 154 { 155 RendererUtils.checkParamValidity(facesContext, component, UIForm.class); 156 157 163 164 UIForm htmlForm = (UIForm)component; 165 166 Map paramMap = facesContext.getExternalContext().getRequestParameterMap(); 167 String submittedValue = (String )paramMap.get(component.getClientId(facesContext) + 168 HIDDEN_SUBMIT_INPUT_SUFFIX); 169 if (submittedValue != null && submittedValue.equals(HIDDEN_SUBMIT_INPUT_VALUE)) 170 { 171 htmlForm.setSubmitted(true); 172 } 173 else 174 { 175 htmlForm.setSubmitted(false); 176 } 177 } 178 179 180 public static void addHiddenCommandParameter(UIComponent form, String paramName) 181 { 182 Set set = (Set )form.getAttributes().get(HIDDEN_COMMAND_INPUTS_SET_ATTR); 183 if (set == null) 184 { 185 set = new HashSet (); 186 form.getAttributes().put(HIDDEN_COMMAND_INPUTS_SET_ATTR, set); 187 } 188 set.add(paramName); 189 } 190 191 } 192 | Popular Tags |