1 package fr.improve.struts.taglib.layout; 2 3 import javax.servlet.http.HttpServletResponse ; 4 import javax.servlet.jsp.JspException ; 5 import javax.servlet.jsp.PageContext ; 6 7 import org.apache.struts.config.ActionConfig; 8 import org.apache.struts.config.ModuleConfig; 9 import org.apache.struts.taglib.html.Constants; 10 11 import fr.improve.struts.taglib.layout.el.Expression; 12 import fr.improve.struts.taglib.layout.event.EndLayoutEvent; 13 import fr.improve.struts.taglib.layout.event.StartLayoutEvent; 14 import fr.improve.struts.taglib.layout.util.FormUtilsInterface; 15 import fr.improve.struts.taglib.layout.util.LayoutUtils; 16 import fr.improve.struts.taglib.layout.util.TagUtils; 17 18 24 public class FormTag extends PanelTag { 25 protected String acceptCharset = null; 26 27 protected String action = null; 28 protected String enctype = null; 29 30 protected String focus = null; 31 protected String editFocus = null; 32 protected String inspectFocus = null; 33 protected String createFocus = null; 34 protected String jspFocus = null; 35 36 37 protected String method = "POST"; 38 protected String onreset = null; 39 protected String onsubmit = null; 40 protected String onkeypress = null; 41 protected String scope = "session"; 42 protected String style = null; 43 protected String target = null; 44 protected String type = null; 45 protected String reqCode = null; 46 47 protected String styleId; 48 49 54 protected String scheme; 55 56 protected org.apache.struts.taglib.html.FormTag formTag = new SchemeFormTag(); 57 58 protected int mode = FormUtilsInterface.EDIT_MODE; 59 60 63 public static final int CREATE_MODE = FormUtilsInterface.CREATE_MODE; 64 65 68 public static final int EDIT_MODE = FormUtilsInterface.EDIT_MODE; 69 70 73 public static final int INSPECT_MODE = FormUtilsInterface.INSPECT_MODE; 74 75 78 protected boolean layout = true; 79 80 83 private class SchemeFormTag extends org.apache.struts.taglib.html.FormTag { 84 87 protected void renderAction(StringBuffer results) { 88 String schemeNow = Expression.evaluate(scheme, this.pageContext); 89 if (schemeNow==null || schemeNow.length()==0) { 90 super.renderAction(results); 92 } else { 93 HttpServletResponse response = (HttpServletResponse ) this.pageContext.getResponse(); 95 96 String host = this.pageContext.getRequest().getServerName(); 97 String action = TagUtils.getActionMappingURL(this.action, this.pageContext); 98 99 results.append(" action=\""); 100 results.append( 101 response.encodeURL(schemeNow + "://" + host + action)); 102 results.append("\""); 103 } 104 } 105 } 106 107 public FormTag() { 108 super(); 109 } 111 112 public int doEndLayoutTag() throws JspException { 113 if (layout) { 114 StringBuffer buffer = new StringBuffer (); 115 doPrintBlankLine(buffer); 116 doAfterBody(buffer); 117 TagUtils.write(pageContext, buffer.toString()); 118 buffer.setLength(0); 119 doEndPanel(buffer); 120 TagUtils.write(pageContext, buffer.toString()); 121 doEndLayout(); 122 } 123 124 int ret = formTag.doEndTag(); 125 formTag.release(); 126 if(getOnkeypress()!=null && getOnkeypress().length()!=0){ 127 TagUtils.write(pageContext,"<script language=\"javaScript\">document.forms['" + formTag.getBeanName() + "'].onkeypress="+ getOnkeypress() +";</script>"); 128 } 129 return ret; 130 } 131 132 public int doStartLayoutTag() throws JspException { 133 LayoutUtils.copyProperties(formTag,this); 135 136 int ret = formTag.doStartTag(); 138 139 StringBuffer buffer = null; 141 if (layout) { 142 doStartLayout(); 143 buffer = new StringBuffer ("\n"); 144 doStartPanel(buffer); 145 TagUtils.write(pageContext,buffer.toString()); 146 } 147 148 149 mode = getSkin().getFormUtils().getFormDisplayMode(pageContext); 151 computeDisplayMode(); 152 153 if (layout) { 154 buffer.setLength(0); 155 } 156 processRequestCode(buffer); 158 159 processKey(); 161 162 processFocus(); 164 165 if (layout) { 166 doPrintTitle(buffer); 167 doBeforeBody(buffer); 168 doPrintBlankLine(buffer); 169 TagUtils.write(pageContext,buffer.toString()); 170 } 171 return ret; 172 } 173 174 177 protected void processRequestCode(StringBuffer buffer) throws JspException { 178 if (reqCode!=null) { 179 try { 180 String parameter = computeActionParameter(pageContext, action); 181 182 if (parameter!=null) { 184 buffer.append("<input type=\"hidden\" name=\""); 185 buffer.append(parameter); 186 buffer.append("\" value=\""); 187 buffer.append(reqCode); 188 buffer.append("\">\n"); 189 } 190 } catch (Exception e) { 191 JspException jspe = new JspException ("BaseHandlerTag - cannot acces the DispatchAction parameter: " + e.getMessage() + "(" + e.getClass().getName() + ")"); 192 TagUtils.saveException(pageContext, jspe); 193 throw jspe; 194 } 195 } 196 } 197 198 201 public static String computeActionParameter(PageContext in_pageContext, String in_actionName) { 202 String value = computeActionName(in_actionName); 203 ModuleConfig lc_moduleConfig = TagUtils.getModuleConfig(in_pageContext); 204 ActionConfig lc_actionConfig = lc_moduleConfig.findActionConfig(value); 205 String parameter = lc_actionConfig.getParameter(); 206 return parameter; 207 } 208 209 212 public static String computeActionParameter(PageContext in_pageContext) { 213 org.apache.struts.taglib.html.FormTag lc_strutsFormTag = findFormTag(in_pageContext); 214 String action = lc_strutsFormTag==null ? "" : lc_strutsFormTag.getAction(); 215 String parameter = FormTag.computeActionParameter(in_pageContext, action); 216 return parameter; 217 } 218 219 private static org.apache.struts.taglib.html.FormTag findFormTag(PageContext in_pageContext) { 220 org.apache.struts.taglib.html.FormTag lc_strutsFormTag = (org.apache.struts.taglib.html.FormTag) in_pageContext.findAttribute(Constants.FORM_KEY); 221 if (lc_strutsFormTag==null && !LayoutUtils.getNoErrorMode()) { 222 throw new IllegalStateException ("reqCode can not be set if tag is not inside a form tag"); 223 } 224 return lc_strutsFormTag; 225 } 226 227 230 public static String computeFormName(PageContext in_pageContext) { 231 org.apache.struts.taglib.html.FormTag lc_strutsFormTag = findFormTag(in_pageContext); 232 return lc_strutsFormTag.getBeanName(); 233 } 234 235 238 protected static String computeActionName(String in_actionName) { 239 return TagUtils.getActionMappingName(in_actionName); 240 } 241 242 245 protected void processKey() { 246 if (key!=null && LayoutUtils.getUseFormDisplayMode()) { 247 switch (mode) { 248 case FormUtilsInterface.EDIT_MODE: 249 key = key + ".edit"; 250 break; 251 case FormUtilsInterface.CREATE_MODE: 252 key = key + ".create"; 253 break; 254 case FormUtilsInterface.INSPECT_MODE: 255 key = key + ".inspect"; 256 } 257 } 258 } 259 260 263 protected void processFocus() { 264 switch (mode) { 265 case FormUtilsInterface.EDIT_MODE: 266 if (editFocus!=null) { 267 formTag.setFocus(editFocus); 268 } 269 break; 270 case FormUtilsInterface.INSPECT_MODE: 271 if (inspectFocus!=null) { 272 formTag.setFocus(inspectFocus); 273 } 274 break; 275 case FormUtilsInterface.CREATE_MODE: 276 if (createFocus!=null) { 277 formTag.setFocus(createFocus); 278 } 279 break; 280 } 281 } 282 283 public String getAction() { 284 return action; 285 } 286 public int getDisplayMode() { return mode; } 287 public String getEnctype() { 288 return enctype; 289 } 290 public String getFocus() { 291 return focus; 292 } 293 public String getMethod() { 294 return method; 295 } 296 public String getOnreset() { 297 return onreset; 298 } 299 public String getOnsubmit() { 300 return onsubmit; 301 } 302 public String getOnkeypress() { 303 return onkeypress; 304 } 305 public String getScope() { 306 return scope; 307 } 308 public String getReqCode() { 309 return reqCode; 310 } 311 public String getStyle() { 312 return style; 313 } 314 public String getTarget() { 315 return target; 316 } 317 public String getType() { 318 return type; 319 } 320 public void release() { 321 super.release(); 322 acceptCharset = null; 323 action = null; 324 method = null; 325 scope = null; 326 mode = FormUtilsInterface.EDIT_MODE; 328 reqCode = null; 329 onkeypress = null; 330 onreset = null; 331 onsubmit = null; 332 333 focus = null; 334 editFocus = null; 335 createFocus = null; 336 inspectFocus = null; 337 jspFocus = null; 338 styleId = null; 339 scheme = null; 340 341 layout = true; 342 343 formTag.release(); 344 } 345 349 public void setAction(String action) { 350 this.action = action; 351 if (LayoutUtils.getNoErrorMode()) this.action = "/default"; 352 353 } 354 public void setEnctype(String enctype) { 355 this.enctype = enctype; 356 } 357 public void setFocus(String focus) { 358 this.focus = focus; 359 } 360 public void setMethod(String method) { 361 this.method = method; 362 } 363 public void setOnreset(String onreset) { 364 this.onreset = onreset; 365 } 366 public void setOnsubmit(String onsubmit) { 367 this.onsubmit = onsubmit; 368 } 369 public void setOnkeypress(String onkeypress) { 370 this.onkeypress = onkeypress; 371 } 372 public void setReqCode(String reqCode) { 373 this.reqCode = reqCode; 374 } 375 public void setScope(String scope) { 376 this.scope = scope; 377 } 378 public void setStyle(String style) { 379 this.style = style; 380 } 381 public void setTarget(String target) { 382 this.target = target; 383 } 384 public void setType(String type) { 385 this.type = type; 386 } 387 391 public String getCreateFocus() { 392 return createFocus; 393 } 394 395 399 public String getEditFocus() { 400 return editFocus; 401 } 402 403 407 public String getInspectFocus() { 408 return inspectFocus; 409 } 410 411 415 public void setCreateFocus(String createFocus) { 416 this.createFocus = createFocus; 417 } 418 419 423 public void setEditFocus(String editFocus) { 424 this.editFocus = editFocus; 425 } 426 427 431 public void setInspectFocus(String inspectFocus) { 432 this.inspectFocus = inspectFocus; 433 } 434 435 protected void initDynamicValues() { 436 super.initDynamicValues(); 438 439 jspFocus = focus; 441 focus = Expression.evaluate(focus, pageContext); 442 } 443 protected void reset() { 444 focus = jspFocus; 446 jspFocus = null; 447 448 super.reset(); 450 } 451 452 public String getStyleId() { 453 return styleId; 454 } 455 456 public void setStyleId(String styleId) { 457 this.styleId = styleId; 458 } 459 460 public String getAcceptCharset() { 461 return acceptCharset; 462 } 463 464 public void setAcceptCharset(String acceptCharset) { 465 this.acceptCharset = acceptCharset; 466 } 467 468 public String getScheme() { 469 return scheme; 470 } 471 472 public void setScheme(String scheme) { 473 this.scheme = scheme; 474 } 475 476 public boolean isLayout() { 477 return layout; 478 } 479 480 public void setLayout(boolean layout) { 481 this.layout = layout; 482 } 483 484 public Object processEndLayoutEvent(EndLayoutEvent in_event) throws JspException { 485 if (layout) { 486 return super.processEndLayoutEvent(in_event); 487 } else { 488 return Boolean.FALSE; 489 } 490 } 491 492 public Object processStartLayoutEvent(StartLayoutEvent in_event) throws JspException { 493 if (layout) { 494 return super.processStartLayoutEvent(in_event); 495 } else { 496 return Boolean.FALSE; 497 } 498 } 499 } 500 | Popular Tags |