List of usage examples for javax.servlet.jsp JspException JspException
public JspException(Throwable cause)
JspException
with the specified cause. From source file:org.apache.struts.taglib.html.BaseHandlerTag.java
/** * Searches all scopes for the bean and calls BeanUtils.getProperty() with * the given arguments and converts any exceptions into JspException. * * @param beanName The name of the object to get the property from. * @param property The name of the property to get. * @return The value of the property./*from w w w . j a va 2 s .co m*/ * @throws JspException * @since Struts 1.1 */ protected String lookupProperty(String beanName, String property) throws JspException { Object bean = TagUtils.getInstance().lookup(this.pageContext, beanName, null); if (bean == null) { throw new JspException(messages.getMessage("getter.bean", beanName)); } try { return BeanUtils.getProperty(bean, property); } catch (IllegalAccessException e) { throw new JspException(messages.getMessage("getter.access", property, beanName)); } catch (InvocationTargetException e) { Throwable t = e.getTargetException(); throw new JspException(messages.getMessage("getter.result", property, t.toString())); } catch (NoSuchMethodException e) { throw new JspException(messages.getMessage("getter.method", property, beanName)); } }
From source file:org.apache.struts.taglib.html.JavascriptValidatorTag.java
/** * Render the JavaScript for to perform validations based on the form * name.//from w ww . j av a2 s.c o m * * @throws JspException if a JSP exception has occurred */ public int doStartTag() throws JspException { JspWriter writer = pageContext.getOut(); try { writer.print(this.renderJavascript()); } catch (IOException e) { throw new JspException(e.getMessage()); } return EVAL_BODY_TAG; }
From source file:org.apache.struts.taglib.html.JavascriptValidatorTag.java
/** * Returns fully rendered JavaScript.//from w w w. ja v a 2 s .c o m * * @since Struts 1.2 */ protected String renderJavascript() throws JspException { StringBuffer results = new StringBuffer(); ModuleConfig config = TagUtils.getInstance().getModuleConfig(pageContext); ValidatorResources resources = (ValidatorResources) pageContext .getAttribute(ValidatorPlugIn.VALIDATOR_KEY + config.getPrefix(), PageContext.APPLICATION_SCOPE); if (resources == null) { throw new JspException("ValidatorResources not found in application scope under key \"" + ValidatorPlugIn.VALIDATOR_KEY + config.getPrefix() + "\""); } Locale locale = TagUtils.getInstance().getUserLocale(this.pageContext, null); Form form = null; if ("true".equalsIgnoreCase(dynamicJavascript)) { form = resources.getForm(locale, formName); if (form == null) { throw new JspException("No form found under '" + formName + "' in locale '" + locale + "'. A form must be defined in the " + "Commons Validator configuration when " + "dynamicJavascript=\"true\" is set."); } } if (form != null) { if ("true".equalsIgnoreCase(dynamicJavascript)) { results.append(this.createDynamicJavascript(config, resources, locale, form)); } else if ("true".equalsIgnoreCase(staticJavascript)) { results.append(this.renderStartElement()); if ("true".equalsIgnoreCase(htmlComment)) { results.append(HTML_BEGIN_COMMENT); } } } if ("true".equalsIgnoreCase(staticJavascript)) { results.append(getJavascriptStaticMethods(resources)); } if ((form != null) && ("true".equalsIgnoreCase(dynamicJavascript) || "true".equalsIgnoreCase(staticJavascript))) { results.append(getJavascriptEnd()); } return results.toString(); }
From source file:org.apache.struts.taglib.html.JavascriptValidatorTag.java
/** * Generates the dynamic JavaScript for the form. * * @param config/*w w w . j a v a 2s. co m*/ * @param resources * @param locale * @param form */ private String createDynamicJavascript(ModuleConfig config, ValidatorResources resources, Locale locale, Form form) throws JspException { StringBuffer results = new StringBuffer(); MessageResources messages = TagUtils.getInstance().retrieveMessageResources(pageContext, bundle, true); HttpServletRequest request = (HttpServletRequest) pageContext.getRequest(); ServletContext application = pageContext.getServletContext(); List actions = this.createActionList(resources, form); final String methods = this.createMethods(actions, this.stopOnError(config)); String formName = form.getName(); jsFormName = formName; if (jsFormName.charAt(0) == '/') { String mappingName = TagUtils.getInstance().getActionMappingName(jsFormName); ActionMapping mapping = (ActionMapping) config.findActionConfig(mappingName); if (mapping == null) { JspException e = new JspException(messages.getMessage("formTag.mapping", mappingName)); pageContext.setAttribute(Globals.EXCEPTION_KEY, e, PageContext.REQUEST_SCOPE); throw e; } jsFormName = mapping.getAttribute(); } results.append(this.getJavascriptBegin(methods)); for (Iterator i = actions.iterator(); i.hasNext();) { ValidatorAction va = (ValidatorAction) i.next(); int jscriptVar = 0; String functionName = null; if ((va.getJsFunctionName() != null) && (va.getJsFunctionName().length() > 0)) { functionName = va.getJsFunctionName(); } else { functionName = va.getName(); } results.append(" function " + jsFormName + "_" + functionName + " () { \n"); for (Iterator x = form.getFields().iterator(); x.hasNext();) { Field field = (Field) x.next(); // Skip indexed fields for now until there is a good way to // handle error messages (and the length of the list (could // retrieve from scope?)) if (field.isIndexed() || (field.getPage() != page) || !field.isDependency(va.getName())) { continue; } String message = Resources.getMessage(application, request, messages, locale, va, field); message = (message != null) ? message : ""; // prefix variable with 'a' to make it a legal identifier results.append(" this.a" + jscriptVar++ + " = new Array(\"" + field.getKey() + "\", \"" + escapeQuotes(message) + "\", "); results.append("new Function (\"varName\", \""); Map vars = field.getVars(); // Loop through the field's variables. Iterator varsIterator = vars.keySet().iterator(); while (varsIterator.hasNext()) { String varName = (String) varsIterator.next(); Var var = (Var) vars.get(varName); String varValue = Resources.getVarValue(var, application, request, false); String jsType = var.getJsType(); // skip requiredif variables field, fieldIndexed, fieldTest, // fieldValue if (varName.startsWith("field")) { continue; } String varValueEscaped = escapeJavascript(varValue); if (Var.JSTYPE_INT.equalsIgnoreCase(jsType)) { results.append("this." + varName + "=" + varValueEscaped + "; "); } else if (Var.JSTYPE_REGEXP.equalsIgnoreCase(jsType)) { results.append("this." + varName + "=/" + varValueEscaped + "/; "); } else if (Var.JSTYPE_STRING.equalsIgnoreCase(jsType)) { results.append("this." + varName + "='" + varValueEscaped + "'; "); // So everyone using the latest format doesn't need to // change their xml files immediately. } else if ("mask".equalsIgnoreCase(varName)) { results.append("this." + varName + "=/" + varValueEscaped + "/; "); } else { results.append("this." + varName + "='" + varValueEscaped + "'; "); } } results.append(" return this[varName];\"));\n"); } results.append(" } \n\n"); } return results.toString(); }
From source file:org.apache.struts.taglib.html.MultiboxTag.java
/** * Render the value element/*from ww w .ja v a 2 s.co m*/ * * @param results The StringBuffer that output will be appended to. */ protected String prepareValue(StringBuffer results) throws JspException { String value = (this.value == null) ? this.constant : this.value; if (value == null) { JspException e = new JspException(messages.getMessage("multiboxTag.value")); pageContext.setAttribute(Globals.EXCEPTION_KEY, e, PageContext.REQUEST_SCOPE); throw e; } prepareAttribute(results, "value", TagUtils.getInstance().filter(value)); return value; }
From source file:org.apache.struts.taglib.html.MultiboxTag.java
/** * Render the checked element/* w ww. ja v a 2 s .com*/ * * @param results The StringBuffer that output will be appended to. */ protected void prepareChecked(StringBuffer results, String value) throws JspException { Object bean = TagUtils.getInstance().lookup(pageContext, name, null); String[] values = null; if (bean == null) { throw new JspException(messages.getMessage("getter.bean", name)); } try { values = BeanUtils.getArrayProperty(bean, property); if (values == null) { values = new String[0]; } } catch (IllegalAccessException e) { throw new JspException(messages.getMessage("getter.access", property, name)); } catch (InvocationTargetException e) { Throwable t = e.getTargetException(); throw new JspException(messages.getMessage("getter.result", property, t.toString())); } catch (NoSuchMethodException e) { throw new JspException(messages.getMessage("getter.method", property, name)); } for (int i = 0; i < values.length; i++) { if (value.equals(values[i])) { results.append(" checked=\"checked\""); break; } } }
From source file:org.apache.struts.taglib.html.OptionsCollectionTag.java
/** * Process the start of this tag./*w w w .j a va2 s. c o m*/ * * @throws JspException if a JSP exception has occurred */ public int doStartTag() throws JspException { // Acquire the select tag we are associated with SelectTag selectTag = (SelectTag) pageContext.getAttribute(Constants.SELECT_KEY); if (selectTag == null) { JspException e = new JspException(messages.getMessage("optionsCollectionTag.select")); TagUtils.getInstance().saveException(pageContext, e); throw e; } // Acquire the collection containing our options Object collection = TagUtils.getInstance().lookup(pageContext, name, property, null); if (collection == null) { JspException e = new JspException(messages.getMessage("optionsCollectionTag.collection")); TagUtils.getInstance().saveException(pageContext, e); throw e; } // Acquire an iterator over the options collection Iterator iter = getIterator(collection); StringBuffer sb = new StringBuffer(); // Render the options while (iter.hasNext()) { Object bean = iter.next(); Object beanLabel = null; Object beanValue = null; // Get the label for this option try { beanLabel = PropertyUtils.getProperty(bean, label); if (beanLabel == null) { beanLabel = ""; } } catch (IllegalAccessException e) { JspException jspe = new JspException(messages.getMessage("getter.access", label, bean)); TagUtils.getInstance().saveException(pageContext, jspe); throw jspe; } catch (InvocationTargetException e) { Throwable t = e.getTargetException(); JspException jspe = new JspException(messages.getMessage("getter.result", label, t.toString())); TagUtils.getInstance().saveException(pageContext, jspe); throw jspe; } catch (NoSuchMethodException e) { JspException jspe = new JspException(messages.getMessage("getter.method", label, bean)); TagUtils.getInstance().saveException(pageContext, jspe); throw jspe; } // Get the value for this option try { beanValue = PropertyUtils.getProperty(bean, value); if (beanValue == null) { beanValue = ""; } } catch (IllegalAccessException e) { JspException jspe = new JspException(messages.getMessage("getter.access", value, bean)); TagUtils.getInstance().saveException(pageContext, jspe); throw jspe; } catch (InvocationTargetException e) { Throwable t = e.getTargetException(); JspException jspe = new JspException(messages.getMessage("getter.result", value, t.toString())); TagUtils.getInstance().saveException(pageContext, jspe); throw jspe; } catch (NoSuchMethodException e) { JspException jspe = new JspException(messages.getMessage("getter.method", value, bean)); TagUtils.getInstance().saveException(pageContext, jspe); throw jspe; } String stringLabel = beanLabel.toString(); String stringValue = beanValue.toString(); // Render this option addOption(sb, stringLabel, stringValue, selectTag.isMatched(stringValue)); } TagUtils.getInstance().write(pageContext, sb.toString()); return SKIP_BODY; }
From source file:org.apache.struts.taglib.html.OptionsCollectionTag.java
/** * Return an iterator for the options collection. * * @param collection Collection to be iterated over * @throws JspException if an error occurs *//*from ww w. jav a2 s.c om*/ protected Iterator getIterator(Object collection) throws JspException { if (collection.getClass().isArray()) { collection = Arrays.asList((Object[]) collection); } if (collection instanceof Collection) { return (((Collection) collection).iterator()); } else if (collection instanceof Iterator) { return ((Iterator) collection); } else if (collection instanceof Map) { return (((Map) collection).entrySet().iterator()); } else if (collection instanceof Enumeration) { return new IteratorAdapter((Enumeration) collection); } else { throw new JspException(messages.getMessage("optionsCollectionTag.iterator", collection.toString())); } }
From source file:org.apache.struts.taglib.html.OptionsTag.java
/** * Process the end of this tag./* w ww .j av a2s .c o m*/ * * @throws JspException if a JSP exception has occurred */ public int doEndTag() throws JspException { // Acquire the select tag we are associated with SelectTag selectTag = (SelectTag) pageContext.getAttribute(Constants.SELECT_KEY); if (selectTag == null) { throw new JspException(messages.getMessage("optionsTag.select")); } StringBuffer sb = new StringBuffer(); // If a collection was specified, use that mode to render options if (collection != null) { Iterator collIterator = getIterator(collection, null); while (collIterator.hasNext()) { Object bean = collIterator.next(); Object value = null; Object label = null; try { value = PropertyUtils.getProperty(bean, property); if (value == null) { value = ""; } } catch (IllegalAccessException e) { throw new JspException(messages.getMessage("getter.access", property, collection)); } catch (InvocationTargetException e) { Throwable t = e.getTargetException(); throw new JspException(messages.getMessage("getter.result", property, t.toString())); } catch (NoSuchMethodException e) { throw new JspException(messages.getMessage("getter.method", property, collection)); } try { if (labelProperty != null) { label = PropertyUtils.getProperty(bean, labelProperty); } else { label = value; } if (label == null) { label = ""; } } catch (IllegalAccessException e) { throw new JspException(messages.getMessage("getter.access", labelProperty, collection)); } catch (InvocationTargetException e) { Throwable t = e.getTargetException(); throw new JspException(messages.getMessage("getter.result", labelProperty, t.toString())); } catch (NoSuchMethodException e) { throw new JspException(messages.getMessage("getter.method", labelProperty, collection)); } String stringValue = value.toString(); addOption(sb, stringValue, label.toString(), selectTag.isMatched(stringValue)); } } // Otherwise, use the separate iterators mode to render options else { // Construct iterators for the values and labels collections Iterator valuesIterator = getIterator(name, property); Iterator labelsIterator = null; if ((labelName != null) || (labelProperty != null)) { labelsIterator = getIterator(labelName, labelProperty); } // Render the options tags for each element of the values coll. while (valuesIterator.hasNext()) { Object valueObject = valuesIterator.next(); if (valueObject == null) { valueObject = ""; } String value = valueObject.toString(); String label = value; if ((labelsIterator != null) && labelsIterator.hasNext()) { Object labelObject = labelsIterator.next(); if (labelObject == null) { labelObject = ""; } label = labelObject.toString(); } addOption(sb, value, label, selectTag.isMatched(value)); } } TagUtils.getInstance().write(pageContext, sb.toString()); return EVAL_PAGE; }
From source file:org.apache.struts.taglib.html.OptionsTag.java
/** * Return an iterator for the option labels or values, based on our * configured properties.//from w w w. j a v a 2s. c om * * @param name Name of the bean attribute (if any) * @param property Name of the bean property (if any) * @throws JspException if an error occurs */ protected Iterator getIterator(String name, String property) throws JspException { // Identify the bean containing our collection String beanName = name; if (beanName == null) { beanName = Constants.BEAN_KEY; } Object bean = TagUtils.getInstance().lookup(pageContext, beanName, null); if (bean == null) { throw new JspException(messages.getMessage("getter.bean", beanName)); } // Identify the collection itself Object collection = bean; if (property != null) { try { collection = PropertyUtils.getProperty(bean, property); if (collection == null) { throw new JspException(messages.getMessage("getter.property", property)); } } catch (IllegalAccessException e) { throw new JspException(messages.getMessage("getter.access", property, name)); } catch (InvocationTargetException e) { Throwable t = e.getTargetException(); throw new JspException(messages.getMessage("getter.result", property, t.toString())); } catch (NoSuchMethodException e) { throw new JspException(messages.getMessage("getter.method", property, name)); } } // Construct and return an appropriate iterator if (collection.getClass().isArray()) { collection = Arrays.asList((Object[]) collection); } if (collection instanceof Collection) { return (((Collection) collection).iterator()); } else if (collection instanceof Iterator) { return ((Iterator) collection); } else if (collection instanceof Map) { return (((Map) collection).entrySet().iterator()); } else if (collection instanceof Enumeration) { return new IteratorAdapter((Enumeration) collection); } else { throw new JspException(messages.getMessage("optionsTag.iterator", collection.toString())); } }