1 16 17 package org.apache.struts.faces.renderer; 18 19 20 import java.io.IOException ; 21 import java.util.Map ; 22 23 import javax.faces.component.NamingContainer; 24 import javax.faces.component.UIComponent; 25 import javax.faces.context.FacesContext; 26 import javax.faces.context.ResponseWriter; 27 import javax.servlet.http.HttpSession ; 28 29 import org.apache.commons.logging.Log; 30 import org.apache.commons.logging.LogFactory; 31 import org.apache.struts.Globals; 32 import org.apache.struts.config.ActionConfig; 33 import org.apache.struts.config.ModuleConfig; 34 import org.apache.struts.faces.component.FormComponent; 35 36 37 43 44 public class FormRenderer extends AbstractRenderer { 45 46 47 49 50 53 private static Log log = LogFactory.getLog(FormRenderer.class); 54 55 56 58 59 69 public void decode(FacesContext context, UIComponent component) { 70 71 if ((context == null) || (component == null)) { 72 throw new NullPointerException (); 73 } 74 String clientId = component.getClientId(context); 75 Map map = context.getExternalContext().getRequestParameterMap(); 76 if (log.isDebugEnabled()) { 77 log.debug("decode(" + clientId + ") --> " + 78 map.containsKey(clientId)); 79 } 80 component.getAttributes().put 81 ("submitted", 82 map.containsKey(clientId) ? Boolean.TRUE : Boolean.FALSE); 83 84 } 85 86 87 private static String passThrough[] = 88 { "enctype", "method", "onreset", "onsubmit", "style", "target", }; 89 90 91 102 public void encodeBegin(FacesContext context, UIComponent component) 103 throws IOException { 104 105 if ((context == null) || (component == null)) { 106 throw new NullPointerException (); 107 } 108 109 FormComponent form = (FormComponent) component; 111 String action = form.getAction(); 112 ModuleConfig moduleConfig = form.lookupModuleConfig(context); 113 ActionConfig actionConfig = moduleConfig.findActionConfig(action); 114 String beanName = actionConfig.getAttribute(); 115 if (beanName != null) { 116 form.getAttributes().put("beanName", beanName); 117 } 118 119 String clientId = component.getClientId(context); 121 if (log.isDebugEnabled()) { 122 log.debug("encodeBegin(" + clientId + ")"); 123 } 124 String styleClass = 125 (String ) component.getAttributes().get("styleClass"); 126 127 ResponseWriter writer = context.getResponseWriter(); 129 writer.startElement("form", form); 130 writer.writeAttribute("id", clientId, "clientId"); 131 if (beanName != null) { 132 writer.writeAttribute("name", beanName, null); 133 } 134 writer.writeAttribute("action", action(context, component), "action"); 135 if (styleClass != null) { 136 writer.writeAttribute("class", styleClass, "styleClass"); 137 } 138 if (component.getAttributes().get("method") == null) { 139 writer.writeAttribute("method", "post", null); 140 } 141 renderPassThrough(context, component, writer, passThrough); 142 writer.writeText("\n", null); 143 144 writer.startElement("input", form); 146 writer.writeAttribute("type", "hidden", null); 147 writer.writeAttribute("name", clientId, null); 148 writer.writeAttribute("value", clientId, null); 149 writer.endElement("input"); 150 writer.writeText("\n", null); 151 152 HttpSession session = (HttpSession ) 154 context.getExternalContext().getSession(false); 155 if (session != null) { 156 String token = (String ) 157 session.getAttribute(Globals.TRANSACTION_TOKEN_KEY); 158 if (token != null) { 159 writer.startElement("input", form); 160 writer.writeAttribute("type", "hidden", null); 161 writer.writeAttribute 162 ("name", "org.apache.struts.taglib.html.TOKEN", null); 163 writer.writeAttribute("value", token, null); 164 writer.endElement("input"); 165 writer.writeText("\n", null); 166 } 167 } 168 169 if (component instanceof FormComponent) { 171 ((FormComponent) component).createActionForm(context); 172 } 173 174 } 175 176 177 188 public void encodeEnd(FacesContext context, UIComponent component) 189 throws IOException { 190 191 if ((context == null) || (component == null)) { 192 throw new NullPointerException (); 193 } 194 String clientId = component.getClientId(context); 195 if (log.isDebugEnabled()) { 196 log.debug("encodeEnd(" + clientId + ")"); 197 } 198 ResponseWriter writer = context.getResponseWriter(); 199 200 writer.startElement("input", component); 202 writer.writeAttribute("type", "hidden", null); 203 writer.writeAttribute("name", component.getClientId(context), null); 204 writer.writeAttribute("value", component.getClientId(context), null); 205 writer.endElement("input"); 206 writer.write("\n"); 207 208 context.getApplication().getViewHandler().writeState(context); 210 211 writer.endElement("form"); 213 writer.writeText("\n", null); 214 215 if (!(component instanceof FormComponent)) { 217 return; 218 } 219 String focus = (String ) component.getAttributes().get("focus"); 220 if (focus == null) { 221 return; 222 } 223 String focusIndex = 224 (String ) component.getAttributes().get("focusIndex"); 225 writer.writeText("\n", null); 226 FormComponent form = (FormComponent) component; 227 writer.startElement("script", form); 228 writer.writeAttribute("type", "text/javascript", null); 229 if (!isXhtml(component)) { 230 writer.writeAttribute("language", "JavaScript", null); 231 } 232 writer.writeText("\n", null); 233 if (!isXhtml(component)) { 234 writer.write("<!--\n"); 235 } 236 237 StringBuffer sb = new StringBuffer ("document.forms[\""); 238 sb.append(clientId); 239 sb.append("\"].elements[\""); 240 sb.append(component.getClientId(context)); 241 sb.append(NamingContainer.SEPARATOR_CHAR); 242 sb.append(focus); 243 sb.append("\"]"); 244 String focusControl = sb.toString(); 245 246 writer.write(" var focusControl = "); 247 writer.write(focusControl); 248 writer.write(";\n"); 249 writer.write(" if (focusControl.type != \"hidden\") {\n"); 250 writer.write(" "); 251 writer.write(focusControl); 252 if (focusIndex != null) { 253 writer.write("["); 254 writer.write(focusIndex); 255 writer.write("]"); 256 } 257 writer.write(".focus();\n"); 258 writer.write(" }\n"); 259 260 if (!isXhtml(component)) { 261 writer.write("// -->\n"); 262 } 263 writer.endElement("script"); 264 writer.writeText("\n", null); 265 266 } 267 268 269 271 272 280 protected String action(FacesContext context, UIComponent component) { 281 282 String actionURL = 283 context.getApplication().getViewHandler(). 284 getActionURL(context, context.getViewRoot().getViewId()); 285 if (log.isTraceEnabled()) { 286 log.trace("getActionURL(" + context.getViewRoot().getViewId() + 287 ") --> " + actionURL); 288 } 289 return (context.getExternalContext().encodeActionURL(actionURL)); 290 291 } 292 293 294 299 protected boolean isXhtml(UIComponent component) { 300 301 return (false); 303 } 304 305 306 } 307 | Popular Tags |