1 18 package org.apache.beehive.netui.tags.html; 19 20 import org.apache.beehive.netui.util.internal.InternalStringBuilder; 21 22 import org.apache.beehive.netui.core.urls.MutableURI; 23 import org.apache.beehive.netui.core.urls.URIContext; 24 import org.apache.beehive.netui.core.urls.URLType; 25 import org.apache.beehive.netui.core.urls.URLRewriterService; 26 import org.apache.beehive.netui.pageflow.FlowController; 27 import org.apache.beehive.netui.pageflow.PageFlowUtils; 28 import org.apache.beehive.netui.pageflow.handler.Handlers; 29 import org.apache.beehive.netui.pageflow.internal.InternalConstants; 30 import org.apache.beehive.netui.pageflow.internal.InternalUtils; 31 import org.apache.beehive.netui.pageflow.internal.URIContextFactory; 32 import org.apache.beehive.netui.pageflow.scoping.ScopedServletUtils; 33 import org.apache.beehive.netui.script.common.ImplicitObjectUtil; 34 import org.apache.beehive.netui.tags.TagConfig; 35 import org.apache.beehive.netui.tags.internal.PageFlowTagUtils; 36 import org.apache.beehive.netui.tags.javascript.CoreScriptFeature; 37 import org.apache.beehive.netui.tags.javascript.IScriptReporter; 38 import org.apache.beehive.netui.tags.javascript.ScriptRequestState; 39 import org.apache.beehive.netui.tags.rendering.*; 40 import org.apache.beehive.netui.util.Bundle; 41 import org.apache.beehive.netui.util.ParamHelper; 42 import org.apache.beehive.netui.util.logging.Logger; 43 import org.apache.struts.Globals; 44 import org.apache.struts.action.ActionForm; 45 import org.apache.struts.action.ActionMapping; 46 import org.apache.struts.action.ActionServlet; 47 import org.apache.struts.config.FormBeanConfig; 48 import org.apache.struts.config.ModuleConfig; 49 import org.apache.struts.taglib.html.Constants; 50 51 import javax.servlet.ServletContext ; 52 import javax.servlet.ServletRequest ; 53 import javax.servlet.http.HttpServletRequest ; 54 import javax.servlet.http.HttpServletResponse ; 55 import javax.servlet.http.HttpSession ; 56 import javax.servlet.jsp.JspException ; 57 import javax.servlet.jsp.PageContext ; 58 import java.net.URISyntaxException ; 59 import java.util.*; 60 61 204 public class Form extends HtmlBaseTag 205 implements IUrlParams 206 { 207 private static final Logger logger = Logger.getInstance(Form.class); 208 209 private static String FORM_ID = "Netui_Form_"; 211 212 private FormTag.State _state = new FormTag.State(); 214 215 private InputHiddenTag.State _hiddenState = new InputHiddenTag.State(); 217 218 private String _focus = null; private String _location = null; private String _text = null; 222 private String _beanName = null; private boolean _explicitBeanType = false; private String _beanType = null; private String _beanScope = null; 227 private String _realName = null; private boolean _setRealName = false; 230 private String _targetScope; 232 private ActionMapping _mapping = null; private ActionServlet _servlet = null; private HashMap _focusMap; private ModuleConfig _appConfig = null; private FlowController _flowController = null; private boolean _formSubmit = false; 238 private Map _params; 239 private String _actionUrl; private LinkedHashMap _extraHiddenParams = null; 242 245 public String getTagName() 246 { 247 return "Form"; 248 } 249 250 255 protected AbstractHtmlState getState() 256 { 257 return _state; 258 } 259 260 268 public void setAttribute(String name, String value, String facet) 269 throws JspException 270 { 271 if (name != null) { 272 if (name.equals(ACTION) || name.equals(METHOD)) { 273 String s = Bundle.getString("Tags_AttributeMayNotBeSet", new Object []{name}); 274 registerTagError(s, null); 275 } 276 } 277 super.setAttribute(name, value, facet); 278 } 279 280 284 public String getAction() 285 { 286 return _state.action; 287 } 288 289 298 public void setAction(String action) 299 throws JspException 300 { 301 _state.action = setRequiredValueAttribute(action, "action"); 302 } 303 304 313 public void setEnctype(String enctype) 314 { 315 _state.registerAttribute(AbstractHtmlState.ATTR_GENERAL, ENCTYPE, enctype); 316 } 317 318 323 public String getRealFormId() 324 { 325 return _realName; 326 } 327 328 332 public void insureRealId() 333 { 334 _setRealName = true; 335 } 336 337 346 public void setFocus(String focus) 347 { 348 _focus = setNonEmptyValueAttribute(focus); 349 insureRealId(); 350 } 351 352 356 public boolean isFocusSet() { 357 return (_focus != null); 358 } 359 360 369 public void setLocation(String location) 370 { 371 _location = location; 372 } 373 374 378 public String getLocation() { 379 return _location; 380 } 381 382 391 public void setMethod(String method) 392 throws JspException 393 { 394 if (FORM_POST.equals(method) || FORM_GET.equals(method)) { 395 _state.method = method; 396 return; 397 } 398 String s = Bundle.getString("Tags_FormMethodError", new Object []{method}); 399 registerTagError(s, null); 400 } 401 402 413 public void setBeanName(String name) 414 throws JspException 415 { 416 _beanName = setRequiredValueAttribute(name, "beanName"); 418 } 419 420 430 public void setTargetScope(String targetScope) 431 { 432 _targetScope = targetScope; 433 } 434 435 444 public void setOnReset(String onReset) 445 { 446 _state.registerAttribute(AbstractHtmlState.ATTR_JAVASCRIPT, ONRESET, onReset); 447 } 448 449 458 public void setOnSubmit(String onSubmit) 459 { 460 _state.registerAttribute(AbstractHtmlState.ATTR_JAVASCRIPT, ONSUBMIT, onSubmit); 461 } 462 463 479 public void setBeanScope(String scope) 480 throws JspException 481 { 482 _beanScope = setRequiredValueAttribute(scope, "beanScope"); 483 } 484 485 494 public void setTarget(String target) 495 { 496 _state.registerAttribute(AbstractHtmlState.ATTR_GENERAL, TARGET, target); 497 } 498 499 512 public void setBeanType(String type) 513 throws JspException 514 { 515 _beanType = setRequiredValueAttribute(type, "beanType"); 516 _explicitBeanType = true; 517 } 518 519 531 public void setGenJavaScriptFormSubmit(boolean formSubmit) 532 { 533 _formSubmit = formSubmit; 534 _setRealName = true; 535 } 536 537 543 public void addParameter(String name, Object value, String facet) throws JspException 544 { 545 assert(name != null) : "Parameter 'name' must not be null"; 546 547 if (_params == null) { 548 _params = new HashMap(); 549 } 550 ParamHelper.addParam(_params, name, value); 551 } 552 553 558 public void addTagID(String tagID, String name) 559 { 560 if (_focusMap == null) { 561 _focusMap = new HashMap(); 562 } 563 _focusMap.put(tagID, name); 564 } 565 566 571 public int doStartTag() throws JspException 572 { 573 if (hasErrors()) 574 return SKIP_BODY; 575 576 if (getNearestForm() != null) { 578 registerTagError(Bundle.getString("Tags_FormParentForm"), null); 579 } 580 581 HttpServletRequest request = (HttpServletRequest ) pageContext.getRequest(); 583 ServletContext servletContext = pageContext.getServletContext(); 584 585 lookupBeanScopeAndName(request, servletContext); 587 if (hasErrors()) 588 return SKIP_BODY; 589 590 if (_state.id != null) { 593 _realName = getIdForTagId(_state.id); 594 } 595 else { 596 String formId = FORM_ID + getNextId(request); 597 _realName = getIdForTagId(formId); 598 } 599 600 pageContext.setAttribute(Constants.FORM_KEY, this, PageContext.REQUEST_SCOPE); 602 603 int scope = PageContext.SESSION_SCOPE; 605 if ("request".equals(_beanScope)) { 606 scope = PageContext.REQUEST_SCOPE; 607 } 608 609 Object bean = null; 611 if (_beanName != null) 612 bean = pageContext.getAttribute(_beanName, scope); 613 if (bean == null) { 614 if (_explicitBeanType) { 615 try { 617 Handlers handlers = Handlers.get(pageContext.getServletContext()); 618 bean = handlers.getReloadableClassHandler().newInstance(_beanType); 619 if (bean != null) { 620 ((ActionForm) bean).setServlet(_servlet); 621 } 622 } 623 catch (Exception e) { 624 registerTagError(Bundle.getString("Tags_FormNameBadType"), e); 625 } 626 } 627 else { 628 630 if (_flowController != null) { 633 bean = _flowController.getFormBean(_mapping); 634 } 635 636 if (bean == null) { 637 bean = InternalUtils.createActionForm(_mapping, _appConfig, _servlet, servletContext); 638 } 639 } 640 641 if (hasErrors()) 642 return SKIP_BODY; 643 644 if (bean instanceof ActionForm) { 646 ((ActionForm) bean).reset(_mapping, request); 647 } 648 if (bean != null) { 649 pageContext.setAttribute(_beanName, bean, scope); 650 } 651 } 652 if (bean != null) { 653 pageContext.setAttribute(Constants.BEAN_KEY, bean, PageContext.REQUEST_SCOPE); 654 ImplicitObjectUtil.loadActionForm(pageContext, bean); 655 } 656 657 try { 659 HttpServletResponse response = (HttpServletResponse ) pageContext.getResponse(); 660 _extraHiddenParams = new LinkedHashMap(); 661 _actionUrl = rewriteActionURL(servletContext, request, response, _extraHiddenParams); 662 } 663 catch (URISyntaxException e) { 664 logger.error(Bundle.getString("Tags_URISyntaxException")); 666 String s = Bundle.getString("Tags_Form_URLException", 667 new Object []{_state.action, e.getMessage()}); 668 registerTagError(s, e); 669 } 670 671 return EVAL_BODY_BUFFERED; 673 } 674 675 679 public int doAfterBody() throws JspException 680 { 681 if (bodyContent != null) { 682 String value = bodyContent.getString(); 683 bodyContent.clearBody(); 684 if (value.length() > 0) 685 _text = value; 686 } 687 return SKIP_BODY; 688 } 689 690 694 public int doEndTag() throws JspException 695 { 696 if (hasErrors()) 697 return reportAndExit(EVAL_PAGE); 698 699 String idScript = null; 700 701 HttpServletRequest request = (HttpServletRequest ) pageContext.getRequest(); 703 ServletContext servletContext = pageContext.getServletContext(); 704 705 if (_state.id != null || _setRealName) { 708 String id = _state.id; 709 _state.name = _realName; 710 _state.id = _realName; 711 idScript = renderNameAndId(request, id); 712 } 713 714 if (_state.method == null) 715 _state.method = FORM_POST; 716 717 HttpServletResponse response = (HttpServletResponse ) pageContext.getResponse(); 721 722 if (_actionUrl != null) { 723 _state.action = response.encodeURL(_actionUrl); 724 } 725 726 WriteRenderAppender writer = new WriteRenderAppender(pageContext); 727 TagRenderingBase br = TagRenderingBase.Factory.getRendering(TagRenderingBase.FORM_TAG, request); 728 br.doStartTag(writer, _state); 729 730 String token = PageFlowTagUtils.getToken(request, _mapping); 733 734 HttpSession session = pageContext.getSession(); 736 if (session != null) { 737 if (token == null) { 738 token = (String ) session.getAttribute(Globals.TRANSACTION_TOKEN_KEY); 739 } 740 if (token != null) { 741 String name = URLRewriterService.getNamePrefix(servletContext, request, Constants.TOKEN_KEY) 742 + Constants.TOKEN_KEY; 743 writeHiddenParam(name, token, writer, request, true); 744 } 745 } 746 747 if (_params != null) { 749 Iterator paramKeys = _params.keySet().iterator(); 750 while (paramKeys.hasNext()) { 751 Object paramKey = paramKeys.next(); 752 Object paramValue = _params.get(paramKey); 753 if (paramValue instanceof String []) { 754 String [] paramValArray = (String []) paramValue; 755 for (int i = 0; i < paramValArray.length; i++) { 756 String name = paramKey.toString(); 757 String paramName = URLRewriterService.getNamePrefix(servletContext, request, name) + name; 758 writeHiddenParam(paramName, paramValArray[i], writer, request, true); 759 } 760 } 761 else { 762 String name = paramKey.toString(); 763 String paramName = URLRewriterService.getNamePrefix(servletContext, request, name) + name; 764 writeHiddenParam(paramName, paramValue.toString(), writer, request, true); 765 } 766 } 767 } 768 769 if (_extraHiddenParams != null && _extraHiddenParams.size() > 0) { 771 for (Iterator i = _extraHiddenParams.keySet().iterator(); i.hasNext();) { 772 String name = (String ) i.next(); 773 for (Iterator j = ((List)_extraHiddenParams.get(name)).iterator(); j.hasNext();) { 774 String value = (String ) j.next(); 775 writeHiddenParam(name, value, writer, request, true); 776 } 777 } 778 } 779 780 if (_text != null) 782 write(_text); 783 784 pageContext.removeAttribute(Constants.BEAN_KEY, PageContext.REQUEST_SCOPE); 786 pageContext.removeAttribute(Constants.FORM_KEY, PageContext.REQUEST_SCOPE); 787 ImplicitObjectUtil.unloadActionForm(pageContext); 788 789 br.doEndTag(writer); 791 792 if ((_focus != null) && (_focusMap != null)) { 794 String focusName = (String ) _focusMap.get(_focus); 795 796 if (focusName != null) { 797 String formName = _realName; 798 ScriptRequestState srs = ScriptRequestState.getScriptRequestState(request); 799 srs.writeFeature(getScriptReporter(), writer, CoreScriptFeature.SET_FOCUS, false, true, 800 new Object []{formName, focusName}); 801 } 802 } 803 804 if (_formSubmit) { 805 ScriptRequestState srs = ScriptRequestState.getScriptRequestState(request); 806 srs.writeFeature(getScriptReporter(), writer, CoreScriptFeature.ANCHOR_SUBMIT, true, false, null); 807 } 808 809 if (idScript != null) 811 write(idScript); 812 813 localRelease(); 814 return EVAL_PAGE; 815 } 816 817 824 private String renderNameAndId(HttpServletRequest request, String id) 825 { 826 if (id == null) 828 return null; 829 830 String idScript = null; 833 IScriptReporter scriptReporter = getScriptReporter(); 834 ScriptRequestState srs = ScriptRequestState.getScriptRequestState(request); 835 if (TagConfig.isLegacyJavaScript()) { 836 idScript = srs.mapLegacyTagId(scriptReporter, id, _state.id); 837 } 838 839 if (TagConfig.isDefaultJavaScript()) { 841 String script = srs.mapTagId(scriptReporter, id, _state.id, _state.name); 842 843 if (idScript != null) { 845 idScript = idScript + script; 846 } 847 else { 848 idScript = script; 849 } 850 } 851 return idScript; 852 } 853 854 855 862 private void writeHiddenParam(String paramName, String paramValue, AbstractRenderAppender results, 863 ServletRequest req, boolean newLine) 864 { 865 if (newLine) 867 results.append("\n"); 868 869 _hiddenState.clear(); 871 _hiddenState.name = paramName; 872 _hiddenState.value = paramValue; 873 874 TagRenderingBase hiddenTag = TagRenderingBase.Factory.getRendering(TagRenderingBase.INPUT_HIDDEN_TAG, req); 875 hiddenTag.doStartTag(results, _hiddenState); 876 hiddenTag.doEndTag(results); 877 } 878 879 887 private String rewriteActionURL(ServletContext servletContext, HttpServletRequest request, 888 HttpServletResponse response, LinkedHashMap extraHiddenParams) 889 throws URISyntaxException 890 { 891 String qualifiedAction = InternalUtils.qualifyAction(servletContext, _state.action); 892 String actionUrl = InternalUtils.createActionURL(request, qualifiedAction); 893 MutableURI uri = new MutableURI(actionUrl, true); 894 uri.setFragment(_location); 895 uri.setEncoding(response.getCharacterEncoding()); 896 897 boolean needsToBeSecure = PageFlowUtils.needsToBeSecure(servletContext, request, actionUrl, true); 898 URLRewriterService.rewriteURL(servletContext, request, response, uri, URLType.ACTION, needsToBeSecure); 899 900 String targetScope = (_targetScope != null) ? _targetScope : request.getParameter(ScopedServletUtils.SCOPE_ID_PARAM); 902 if (targetScope != null) { 903 if (_params == null) { 904 _params = new HashMap(); 905 } 906 _params.put(ScopedServletUtils.SCOPE_ID_PARAM, targetScope); 907 if (uri != null) { 909 uri.removeParameter(ScopedServletUtils.SCOPE_ID_PARAM); 910 } 911 } 912 913 boolean forXML = false; 917 if (_state.method != null && _state.method.equalsIgnoreCase(FORM_GET) 918 && !URLRewriterService.allowParamsOnFormAction(servletContext, request)) { 919 extraHiddenParams.putAll(uri.getParameters()); 920 } 921 else { 922 forXML = TagRenderingBase.Factory.isXHTML(request); 924 } 925 926 String key = PageFlowUtils.getURLTemplateKey(URLType.ACTION, needsToBeSecure); 927 URIContext uriContext = URIContextFactory.getInstance(forXML); 928 String rewrittenURI = URLRewriterService.getTemplatedURL(request, uri, key, uriContext); 929 return rewrittenURI; 930 } 931 932 935 String getActionUrl() 936 { 937 return _actionUrl; 938 } 939 940 943 protected void localRelease() 944 { 945 super.localRelease(); 946 947 _state.clear(); 948 _hiddenState.clear(); 949 950 _focus = null; 951 _location = null; 952 _text = null; 953 954 _beanName = null; 955 _explicitBeanType = false; 956 _beanType = null; 957 _beanScope = null; 958 959 _realName = null; 960 _setRealName = false; 961 962 _targetScope = null; 963 964 _mapping = null; 965 _servlet = null; 966 _focusMap = null; 967 _appConfig = null; 968 _flowController = null; 969 _formSubmit = false; 970 _params = null; 971 _actionUrl = null; 972 _extraHiddenParams = null; 973 } 974 975 980 private void lookupBeanScopeAndName(HttpServletRequest request, ServletContext servletContext) 981 throws JspException 982 { 983 _appConfig = (ModuleConfig) request.getAttribute(Globals.MODULE_KEY); 985 if (_appConfig == null) { _appConfig = (ModuleConfig) servletContext.getAttribute(Globals.MODULE_KEY); 987 } 988 if (_appConfig == null) { 989 registerTagError(Bundle.getString("Tags_FormNoApplicationConfig"), null); 990 return; 991 } 992 _servlet = (ActionServlet) servletContext.getAttribute(Globals.ACTION_SERVLET_KEY); 993 _flowController = PageFlowUtils.getCurrentPageFlow(request); 994 995 997 PageFlowTagUtils.MappingAndController mac = PageFlowTagUtils.getActionMapping(request, _flowController, _state.action); 998 if (mac == null) { 999 FlowController globalApp = PageFlowUtils.getSharedFlow(InternalConstants.GLOBALAPP_CLASSNAME, request); 1000 mac = PageFlowTagUtils.getActionMapping(request, globalApp, _state.action); 1001 } 1002 if (mac == null) { 1003 registerTagError(Bundle.getString("Tags_BadAction", _state.action), null); 1004 return; 1005 } 1006 1007 _flowController = mac.controller; 1008 _mapping = mac.mapping; 1009 assert (_mapping != null) : "Mapping not found for controller"; 1010 _appConfig = _mapping.getModuleConfig(); 1011 1012 if (_beanName != null) { 1014 if (!_explicitBeanType) { 1015 registerTagError(Bundle.getString("Tags_FormNameNoType"), null); 1016 return; 1017 } 1018 if (_beanScope == null) 1019 _beanScope = "session"; 1020 return; 1021 } 1022 1023 FormBeanConfig formBeanConfig = _appConfig.findFormBeanConfig(_mapping.getName()); 1025 if (formBeanConfig == null) { 1026 _beanType = null; 1028 } 1029 else { 1030 _beanName = _mapping.getAttribute(); 1032 _beanScope = _mapping.getScope(); 1033 _beanType = formBeanConfig.getType(); 1034 } 1035 } 1036} 1037 | Popular Tags |