1 18 19 package org.apache.struts.taglib.html; 20 21 import java.io.IOException ; 22 23 import javax.servlet.http.HttpServletRequest ; 24 import javax.servlet.http.HttpServletResponse ; 25 import javax.servlet.http.HttpSession ; 26 import javax.servlet.jsp.JspException ; 27 import javax.servlet.jsp.JspWriter ; 28 import javax.servlet.jsp.PageContext ; 29 import javax.servlet.jsp.tagext.TagSupport ; 30 31 import org.apache.struts.Globals; 32 import org.apache.struts.action.ActionForm; 33 import org.apache.struts.action.ActionMapping; 34 import org.apache.struts.action.ActionServlet; 35 import org.apache.struts.config.FormBeanConfig; 36 import org.apache.struts.config.ModuleConfig; 37 import org.apache.struts.taglib.TagUtils; 38 import org.apache.struts.util.MessageResources; 39 import org.apache.struts.util.RequestUtils; 40 41 47 public class FormTag extends TagSupport { 48 49 51 54 protected String action = null; 55 56 59 protected ModuleConfig moduleConfig = null; 60 61 64 protected String enctype = null; 65 66 69 protected String focus = null; 70 71 78 protected String focusIndex = null; 79 80 83 protected static String lineEnd = System.getProperty("line.separator"); 84 85 88 protected ActionMapping mapping = null; 89 90 93 protected static MessageResources messages = 94 MessageResources.getMessageResources(Constants.Package + ".LocalStrings"); 95 96 99 protected String method = null; 100 101 104 protected String onreset = null; 105 106 109 protected String onsubmit = null; 110 111 116 protected boolean scriptLanguage = true; 117 118 123 protected ActionServlet servlet = null; 124 125 128 protected String style = null; 129 130 133 protected String styleClass = null; 134 135 138 protected String styleId = null; 139 140 143 protected String target = null; 144 145 150 protected String beanName = null; 151 152 157 protected String beanScope = null; 158 159 164 protected String beanType = null; 165 166 170 protected String acceptCharset = null; 171 172 173 private boolean disabled = false; 174 175 176 protected boolean readonly = false; 177 178 180 186 public String getBeanName() { 187 188 return beanName; 189 190 } 191 192 195 public String getAction() { 196 197 return (this.action); 198 199 } 200 201 206 public void setAction(String action) { 207 208 this.action = action; 209 210 } 211 212 215 public String getEnctype() { 216 217 return (this.enctype); 218 219 } 220 221 226 public void setEnctype(String enctype) { 227 228 this.enctype = enctype; 229 230 } 231 232 235 public String getFocus() { 236 237 return (this.focus); 238 239 } 240 241 246 public void setFocus(String focus) { 247 248 this.focus = focus; 249 250 } 251 252 255 public String getMethod() { 256 257 return (this.method); 258 259 } 260 261 266 public void setMethod(String method) { 267 268 this.method = method; 269 270 } 271 272 275 public String getOnreset() { 276 277 return (this.onreset); 278 279 } 280 281 286 public void setOnreset(String onReset) { 287 288 this.onreset = onReset; 289 290 } 291 292 295 public String getOnsubmit() { 296 297 return (this.onsubmit); 298 299 } 300 301 306 public void setOnsubmit(String onSubmit) { 307 308 this.onsubmit = onSubmit; 309 310 } 311 312 315 public String getStyle() { 316 317 return (this.style); 318 319 } 320 321 326 public void setStyle(String style) { 327 328 this.style = style; 329 330 } 331 332 335 public String getStyleClass() { 336 337 return (this.styleClass); 338 339 } 340 341 346 public void setStyleClass(String styleClass) { 347 348 this.styleClass = styleClass; 349 350 } 351 352 355 public String getStyleId() { 356 357 return (this.styleId); 358 359 } 360 361 366 public void setStyleId(String styleId) { 367 368 this.styleId = styleId; 369 370 } 371 372 375 public String getTarget() { 376 377 return (this.target); 378 379 } 380 381 386 public void setTarget(String target) { 387 388 this.target = target; 389 390 } 391 392 395 public String getAcceptCharset() { 396 397 return acceptCharset; 398 399 } 400 401 406 public void setAcceptCharset(String acceptCharset) { 407 408 this.acceptCharset= acceptCharset; 409 410 } 411 412 413 public void setDisabled(boolean disabled) { 414 this.disabled = disabled; 415 } 416 417 418 public boolean isDisabled() { 419 return disabled; 420 } 421 422 423 public void setReadonly(boolean readonly) { 424 this.readonly = readonly; 425 } 426 427 428 public boolean isReadonly() { 429 return readonly; 430 } 431 432 433 435 440 public int doStartTag() throws JspException { 441 442 this.lookup(); 444 445 StringBuffer results = new StringBuffer (); 447 448 results.append(this.renderFormStartElement()); 449 450 results.append(this.renderToken()); 451 452 TagUtils.getInstance().write(pageContext, results.toString()); 453 454 pageContext.setAttribute(Constants.FORM_KEY, this, PageContext.REQUEST_SCOPE); 456 457 this.initFormBean(); 458 459 return (EVAL_BODY_INCLUDE); 460 461 } 462 463 468 protected void initFormBean() throws JspException { 469 int scope = PageContext.SESSION_SCOPE; 470 if ("request".equalsIgnoreCase(beanScope)) { 471 scope = PageContext.REQUEST_SCOPE; 472 } 473 474 Object bean = pageContext.getAttribute(beanName, scope); 475 if (bean == null) { 476 bean = 478 RequestUtils.createActionForm( 479 (HttpServletRequest ) pageContext.getRequest(), 480 mapping, 481 moduleConfig, 482 servlet); 483 if (bean instanceof ActionForm) { 484 ((ActionForm) bean).reset(mapping, (HttpServletRequest ) pageContext.getRequest()); 485 } 486 if (bean == null) { 487 throw new JspException (messages.getMessage("formTag.create", beanType)); 488 } 489 pageContext.setAttribute(beanName, bean, scope); 490 } 491 pageContext.setAttribute(Constants.BEAN_KEY, bean, PageContext.REQUEST_SCOPE); 492 } 493 494 499 protected String renderFormStartElement() { 500 501 StringBuffer results = new StringBuffer ("<form"); 502 503 if (!this.isXhtml()) { 505 renderName(results); 506 } 507 renderAttribute(results, "method", getMethod() == null ? "post" : getMethod()); 508 renderAction(results); 509 renderAttribute(results, "accept-charset", getAcceptCharset()); 510 renderAttribute(results, "class", getStyleClass()); 511 renderAttribute(results, "enctype", getEnctype()); 512 renderAttribute(results, "onreset", getOnreset()); 513 renderAttribute(results, "onsubmit", getOnsubmit()); 514 renderAttribute(results, "style", getStyle()); 515 renderAttribute(results, "id", getStyleId()); 516 renderAttribute(results, "target", getTarget()); 517 518 renderOtherAttributes(results); 520 521 results.append(">"); 522 return results.toString(); 523 } 524 525 528 protected void renderName(StringBuffer results) { 529 results.append(" name=\""); 530 results.append(beanName); 531 results.append("\""); 532 } 533 534 537 protected void renderAction(StringBuffer results) { 538 539 HttpServletResponse response = 540 (HttpServletResponse ) this.pageContext.getResponse(); 541 542 results.append(" action=\""); 543 results.append( 544 response.encodeURL( 545 TagUtils.getInstance().getActionMappingURL( 546 this.action, 547 this.pageContext))); 548 549 results.append("\""); 550 } 551 552 556 protected void renderOtherAttributes(StringBuffer results) { 557 } 558 559 565 protected String renderToken() { 566 StringBuffer results = new StringBuffer (); 567 HttpSession session = pageContext.getSession(); 568 569 if (session != null) { 570 String token = 571 (String ) session.getAttribute(Globals.TRANSACTION_TOKEN_KEY); 572 573 if (token != null) { 574 results.append("<div><input type=\"hidden\" name=\""); 575 results.append(Constants.TOKEN_KEY); 576 results.append("\" value=\""); 577 results.append(token); 578 if (this.isXhtml()) { 579 results.append("\" />"); 580 } else { 581 results.append("\">"); 582 } 583 results.append("</div>"); 584 } 585 } 586 587 return results.toString(); 588 } 589 590 593 protected void renderAttribute(StringBuffer results, String attribute, String value) { 594 if (value != null) { 595 results.append(" "); 596 results.append(attribute); 597 results.append("=\""); 598 results.append(value); 599 results.append("\""); 600 } 601 } 602 603 608 public int doEndTag() throws JspException { 609 610 pageContext.removeAttribute(Constants.BEAN_KEY, PageContext.REQUEST_SCOPE); 612 pageContext.removeAttribute(Constants.FORM_KEY, PageContext.REQUEST_SCOPE); 613 614 StringBuffer results = new StringBuffer ("</form>"); 616 617 if (this.focus != null) { 619 results.append(this.renderFocusJavascript()); 620 } 621 622 JspWriter writer = pageContext.getOut(); 624 try { 625 writer.print(results.toString()); 626 } catch (IOException e) { 627 throw new JspException (messages.getMessage("common.io", e.toString())); 628 } 629 630 return (EVAL_PAGE); 632 633 } 634 635 640 protected String renderFocusJavascript() { 641 StringBuffer results = new StringBuffer (); 642 643 results.append(lineEnd); 644 results.append("<script type=\"text/javascript\""); 645 if (!this.isXhtml() && this.scriptLanguage) { 646 results.append(" language=\"JavaScript\""); 647 } 648 results.append(">"); 649 results.append(lineEnd); 650 651 if (!this.isXhtml()) { 653 results.append(" <!--"); 654 results.append(lineEnd); 655 } 656 657 StringBuffer focusControl = new StringBuffer ("document.forms[\""); 660 focusControl.append(beanName); 661 focusControl.append("\"].elements[\""); 662 focusControl.append(this.focus); 663 focusControl.append("\"]"); 664 665 results.append(" var focusControl = "); 666 results.append(focusControl.toString()); 667 results.append(";"); 668 results.append(lineEnd); 669 results.append(lineEnd); 670 671 results.append(" if (focusControl.type != \"hidden\" && !focusControl.disabled) {"); 672 results.append(lineEnd); 673 674 String index = ""; 676 if (this.focusIndex != null) { 677 StringBuffer sb = new StringBuffer ("["); 678 sb.append(this.focusIndex); 679 sb.append("]"); 680 index = sb.toString(); 681 } 682 results.append(" focusControl"); 683 results.append(index); 684 results.append(".focus();"); 685 results.append(lineEnd); 686 687 results.append(" }"); 688 results.append(lineEnd); 689 690 if (!this.isXhtml()) { 691 results.append(" // -->"); 692 results.append(lineEnd); 693 } 694 695 results.append("</script>"); 696 results.append(lineEnd); 697 return results.toString(); 698 } 699 700 703 public void release() { 704 705 super.release(); 706 action = null; 707 moduleConfig = null; 708 enctype = null; 709 disabled = false; 710 focus = null; 711 focusIndex = null; 712 mapping = null; 713 method = null; 714 onreset = null; 715 onsubmit = null; 716 readonly = false; 717 servlet = null; 718 style = null; 719 styleClass = null; 720 styleId = null; 721 target = null; 722 acceptCharset = null; 723 724 } 725 726 728 729 735 protected void lookup() throws JspException { 736 737 moduleConfig = TagUtils.getInstance().getModuleConfig(pageContext); 739 740 if (moduleConfig == null) { 741 JspException e = new JspException (messages.getMessage("formTag.collections")); 742 pageContext.setAttribute(Globals.EXCEPTION_KEY, e, PageContext.REQUEST_SCOPE); 743 throw e; 744 } 745 servlet = 746 (ActionServlet) pageContext.getServletContext().getAttribute( 747 Globals.ACTION_SERVLET_KEY); 748 749 String mappingName = TagUtils.getInstance().getActionMappingName(action); 751 mapping = (ActionMapping) moduleConfig.findActionConfig(mappingName); 752 if (mapping == null) { 753 JspException e = new JspException (messages.getMessage("formTag.mapping", mappingName)); 754 pageContext.setAttribute(Globals.EXCEPTION_KEY, e, PageContext.REQUEST_SCOPE); 755 throw e; 756 } 757 758 FormBeanConfig formBeanConfig = moduleConfig.findFormBeanConfig(mapping.getName()); 760 if (formBeanConfig == null) { 761 JspException e = 762 new JspException (messages.getMessage("formTag.formBean", mapping.getName(), action)); 763 pageContext.setAttribute(Globals.EXCEPTION_KEY, e, PageContext.REQUEST_SCOPE); 764 throw e; 765 } 766 767 beanName = mapping.getAttribute(); 769 beanScope = mapping.getScope(); 770 beanType = formBeanConfig.getType(); 771 } 772 773 776 private boolean isXhtml() { 777 return TagUtils.getInstance().isXhtml(this.pageContext); 778 } 779 780 784 public String getFocusIndex() { 785 return focusIndex; 786 } 787 788 792 public void setFocusIndex(String focusIndex) { 793 this.focusIndex = focusIndex; 794 } 795 796 802 public boolean getScriptLanguage() { 803 return this.scriptLanguage; 804 } 805 806 811 public void setScriptLanguage(boolean scriptLanguage) { 812 this.scriptLanguage = scriptLanguage; 813 } 814 815 } 816 | Popular Tags |