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.SelectTag.java
/** * Calculate the match values we will actually be using. * * @throws JspException//from w w w .ja v a2 s .c om */ private void calculateMatchValues() throws JspException { if (this.value != null) { this.match = new String[1]; this.match[0] = this.value; } else { Object bean = TagUtils.getInstance().lookup(pageContext, name, null); if (bean == null) { JspException e = new JspException(messages.getMessage("getter.bean", name)); TagUtils.getInstance().saveException(pageContext, e); throw e; } try { this.match = BeanUtils.getArrayProperty(bean, property); if (this.match == null) { this.match = new String[0]; } } catch (IllegalAccessException e) { TagUtils.getInstance().saveException(pageContext, e); throw new JspException(messages.getMessage("getter.access", property, name)); } catch (InvocationTargetException e) { Throwable t = e.getTargetException(); TagUtils.getInstance().saveException(pageContext, t); throw new JspException(messages.getMessage("getter.result", property, t.toString())); } catch (NoSuchMethodException e) { TagUtils.getInstance().saveException(pageContext, e); throw new JspException(messages.getMessage("getter.method", property, name)); } } }
From source file:org.apache.struts.taglib.logic.CompareTagBase.java
/** * Evaluate the condition that is being tested by this particular tag, and * return <code>true</code> if the nested body content of this tag should * be evaluated, or <code>false</code> if it should be skipped. This * method must be implemented by concrete subclasses. * * @param desired1 First desired value for a true result (-1, 0, +1) * @param desired2 Second desired value for a true result (-1, 0, +1) * @throws JspException if a JSP exception occurs *///from w w w .ja v a 2 s. co m protected boolean condition(int desired1, int desired2) throws JspException { // Acquire the value and determine the test type int type = -1; double doubleValue = 0.0; long longValue = 0; if ((type < 0) && (value.length() > 0)) { try { doubleValue = Double.parseDouble(value); type = DOUBLE_COMPARE; } catch (NumberFormatException e) { ; } } if ((type < 0) && (value.length() > 0)) { try { longValue = Long.parseLong(value); type = LONG_COMPARE; } catch (NumberFormatException e) { ; } } if (type < 0) { type = STRING_COMPARE; } // Acquire the unconverted variable value Object variable = null; if (cookie != null) { Cookie[] cookies = ((HttpServletRequest) pageContext.getRequest()).getCookies(); if (cookies == null) { cookies = new Cookie[0]; } for (int i = 0; i < cookies.length; i++) { if (cookie.equals(cookies[i].getName())) { variable = cookies[i].getValue(); break; } } } else if (header != null) { variable = ((HttpServletRequest) pageContext.getRequest()).getHeader(header); } else if (name != null) { Object bean = TagUtils.getInstance().lookup(pageContext, name, scope); if (property != null) { if (bean == null) { JspException e = new JspException(messages.getMessage("logic.bean", name)); TagUtils.getInstance().saveException(pageContext, e); throw e; } try { variable = PropertyUtils.getProperty(bean, property); } catch (InvocationTargetException e) { Throwable t = e.getTargetException(); if (t == null) { t = e; } TagUtils.getInstance().saveException(pageContext, t); throw new JspException(messages.getMessage("logic.property", name, property, t.toString())); } catch (Throwable t) { TagUtils.getInstance().saveException(pageContext, t); throw new JspException(messages.getMessage("logic.property", name, property, t.toString())); } } else { variable = bean; } } else if (parameter != null) { variable = pageContext.getRequest().getParameter(parameter); } else { JspException e = new JspException(messages.getMessage("logic.selector")); TagUtils.getInstance().saveException(pageContext, e); throw e; } if (variable == null) { variable = ""; // Coerce null to a zero-length String } // Perform the appropriate comparison int result = 0; if (type == DOUBLE_COMPARE) { try { double doubleVariable = Double.parseDouble(variable.toString()); if (doubleVariable < doubleValue) { result = -1; } else if (doubleVariable > doubleValue) { result = +1; } } catch (NumberFormatException e) { result = variable.toString().compareTo(value); } } else if (type == LONG_COMPARE) { try { long longVariable = Long.parseLong(variable.toString()); if (longVariable < longValue) { result = -1; } else if (longVariable > longValue) { result = +1; } } catch (NumberFormatException e) { result = variable.toString().compareTo(value); } } else { result = variable.toString().compareTo(value); } // Normalize the result if (result < 0) { result = -1; } else if (result > 0) { result = +1; } // Return true if the result matches either desired value return ((result == desired1) || (result == desired2)); }
From source file:org.apache.struts.taglib.logic.IterateTag.java
/** * Construct an iterator for the specified collection, and begin * looping through the body once per element. * * @exception JspException if a JSP exception has occurred *//*from w ww. j a v a 2 s .co m*/ public int doStartTag() throws JspException { // Acquire the collection we are going to iterate over Object collection = this.collection; if (collection == null) { collection = RequestUtils.lookup(pageContext, name, property, scope); } if (collection == null) { JspException e = new JspException(messages.getMessage("iterate.collection")); RequestUtils.saveException(pageContext, e); throw e; } // Construct an iterator for this collection if (collection.getClass().isArray()) { try { // If we're lucky, it is an array of objects // that we can iterate over with no copying iterator = Arrays.asList((Object[]) collection).iterator(); } catch (ClassCastException e) { // Rats -- it is an array of primitives int length = Array.getLength(collection); ArrayList c = new ArrayList(length); for (int i = 0; i < length; i++) { c.add(Array.get(collection, i)); } iterator = c.iterator(); } } else if (collection instanceof Collection) { iterator = ((Collection) collection).iterator(); } else if (collection instanceof Iterator) { iterator = (Iterator) collection; } else if (collection instanceof Map) { iterator = ((Map) collection).entrySet().iterator(); } else if (collection instanceof Enumeration) { iterator = IteratorUtils.asIterator((Enumeration) collection); } else { JspException e = new JspException(messages.getMessage("iterate.iterator")); RequestUtils.saveException(pageContext, e); throw e; } // Calculate the starting offset if (offset == null) { offsetValue = 0; } else { try { offsetValue = Integer.parseInt(offset); } catch (NumberFormatException e) { Integer offsetObject = (Integer) RequestUtils.lookup(pageContext, offset, null); if (offsetObject == null) { offsetValue = 0; } else { offsetValue = offsetObject.intValue(); } } } if (offsetValue < 0) { offsetValue = 0; } // Calculate the rendering length if (length == null) { lengthValue = 0; } else { try { lengthValue = Integer.parseInt(length); } catch (NumberFormatException e) { Integer lengthObject = (Integer) RequestUtils.lookup(pageContext, length, null); if (lengthObject == null) { lengthValue = 0; } else { lengthValue = lengthObject.intValue(); } } } if (lengthValue < 0) { lengthValue = 0; } lengthCount = 0; // Skip the leading elements up to the starting offset for (int i = 0; i < offsetValue; i++) { if (iterator.hasNext()) { iterator.next(); } } // Store the first value and evaluate, or skip the body if none if (iterator.hasNext()) { Object element = iterator.next(); if (element == null) { pageContext.removeAttribute(id); } else { pageContext.setAttribute(id, element); } lengthCount++; started = true; if (indexId != null) { pageContext.setAttribute(indexId, new Integer(getIndex())); } return (EVAL_BODY_TAG); } else { return (SKIP_BODY); } }
From source file:org.apache.struts.taglib.TagUtils.java
/** * Retrieves the value from request scope and if it isn't already an * <code>ActionMessages</code>, some classes are converted to one. * * @param pageContext The PageContext for the current page * @param paramName Key for parameter value * @return ActionErrors in page context. * @throws JspException//w w w .ja va2 s. c om */ public ActionMessages getActionMessages(PageContext pageContext, String paramName) throws JspException { ActionMessages am = new ActionMessages(); Object value = pageContext.findAttribute(paramName); if (value != null) { try { if (value instanceof String) { am.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage((String) value)); } else if (value instanceof String[]) { String[] keys = (String[]) value; for (int i = 0; i < keys.length; i++) { am.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage(keys[i])); } } else if (value instanceof ActionErrors) { ActionMessages m = (ActionMessages) value; am.add(m); } else if (value instanceof ActionMessages) { am = (ActionMessages) value; } else { throw new JspException( messages.getMessage("actionMessages.errors", value.getClass().getName())); } } catch (JspException e) { throw e; } catch (Exception e) { log.warn("Unable to retieve ActionMessage for paramName : " + paramName, e); } } return am; }
From source file:org.apache.struts.taglib.TagUtils.java
/** * Locate and return the specified property of the specified bean, from an * optionally specified scope, in the specified page context. If an * exception is thrown, it will have already been saved via a call to * <code>saveException()</code>. * * @param pageContext Page context to be searched * @param name Name of the bean to be retrieved * @param property Name of the property to be retrieved, or * <code>null</code> to retrieve the bean itself * @param scope Scope to be searched (page, request, session, * application) or <code>null</code> to use * <code>findAttribute()</code> instead * @return property of specified JavaBean * @throws JspException if an invalid scope name is requested * @throws JspException if the specified bean is not found * @throws JspException if accessing this property causes an * IllegalAccessException, IllegalArgumentException, * InvocationTargetException, or NoSuchMethodException *//*w w w . ja va 2 s. com*/ public Object lookup(PageContext pageContext, String name, String property, String scope) throws JspException { // Look up the requested bean, and return if requested Object bean = lookup(pageContext, name, scope); if (bean == null) { JspException e = null; if (scope == null) { e = new JspException(messages.getMessage("lookup.bean.any", name)); } else { e = new JspException(messages.getMessage("lookup.bean", name, scope)); } saveException(pageContext, e); throw e; } if (property == null) { return bean; } // Locate and return the specified property try { return PropertyUtils.getProperty(bean, property); } catch (IllegalAccessException e) { saveException(pageContext, e); throw new JspException(messages.getMessage("lookup.access", property, name)); } catch (IllegalArgumentException e) { saveException(pageContext, e); throw new JspException(messages.getMessage("lookup.argument", property, name)); } catch (InvocationTargetException e) { Throwable t = e.getTargetException(); if (t == null) { t = e; } saveException(pageContext, t); throw new JspException(messages.getMessage("lookup.target", property, name)); } catch (NoSuchMethodException e) { saveException(pageContext, e); String beanName = name; // Name defaults to Contants.BEAN_KEY if no name is specified by // an input tag. Thus lookup the bean under the key and use // its class name for the exception message. if (Constants.BEAN_KEY.equals(name)) { Object obj = pageContext.findAttribute(Constants.BEAN_KEY); if (obj != null) { beanName = obj.getClass().getName(); } } throw new JspException(messages.getMessage("lookup.method", property, beanName)); } }
From source file:org.apache.struts.taglib.TagUtils.java
/** * Returns the appropriate MessageResources object for the current module * and the given bundle./*from w ww . jav a 2 s . c o m*/ * * @param pageContext Search the context's scopes for the resources. * @param bundle The bundle name to look for. If this is * <code>null</code>, the default bundle name is * used. * @param checkPageScope Whether to check page scope * @return MessageResources The bundle's resources stored in some scope. * @throws JspException if the MessageResources object could not be * found. */ public MessageResources retrieveMessageResources(PageContext pageContext, String bundle, boolean checkPageScope) throws JspException { MessageResources resources = null; if (bundle == null) { bundle = Globals.MESSAGES_KEY; } if (checkPageScope) { resources = (MessageResources) pageContext.getAttribute(bundle, PageContext.PAGE_SCOPE); } if (resources == null) { resources = (MessageResources) pageContext.getAttribute(bundle, PageContext.REQUEST_SCOPE); } if (resources == null) { ModuleConfig moduleConfig = getModuleConfig(pageContext); resources = (MessageResources) pageContext.getAttribute(bundle + moduleConfig.getPrefix(), PageContext.APPLICATION_SCOPE); } if (resources == null) { resources = (MessageResources) pageContext.getAttribute(bundle, PageContext.APPLICATION_SCOPE); } if (resources == null) { JspException e = new JspException(messages.getMessage("message.bundle", bundle)); saveException(pageContext, e); throw e; } return resources; }
From source file:org.apache.struts.taglib.tiles.InsertTag.java
/** * Process nested ≶putList> tag. * Method calls by nested ≶putList> tags. * Nested list is added to sub-component attributes * If role is defined, it is checked immediately. *///from ww w . j a va2 s .co m public void processNestedTag(PutListTag nestedTag) throws JspException { // Check role HttpServletRequest request = (HttpServletRequest) pageContext.getRequest(); String role = nestedTag.getRole(); if (role != null && !request.isUserInRole(role)) { // not allowed : skip attribute return; } // Check if a name is defined if (nestedTag.getName() == null) { throw new JspException( "Error - PutList : attribute name is not defined. It is mandatory as the list is added as attribute of 'insert'."); } // now add attribute to enclosing parent (i.e. : this object). putAttribute(nestedTag.getName(), nestedTag.getList()); }
From source file:org.apache.struts.taglib.tiles.InsertTag.java
/** * Get instantiated Controller.// ww w . j a va 2 s . c om * Return controller denoted by controllerType, or <code>null</code> if controllerType * is null. * @throws JspException If controller can't be created. */ private Controller getController() throws JspException { if (controllerType == null) { return null; } try { return ComponentDefinition.createController(controllerName, controllerType); } catch (InstantiationException ex) { throw new JspException(ex.getMessage()); } }
From source file:org.apache.struts.taglib.tiles.InsertTag.java
/** * Process tag attribute and create corresponding tag handler. */// ww w.j a v a 2 s.c om public TagHandler createTagHandler() throws JspException { // Check each tag attribute. // page Url attribute must be the last checked because it can appears concurrently // with others attributes. if (definitionName != null) { return processDefinitionName(definitionName); } else if (attribute != null) { return processAttribute(attribute); } else if (beanName != null) { return processBean(beanName, beanProperty, beanScope); } else if (name != null) { return processName(name); } else if (page != null) { return processUrl(page); } else { throw new JspException( "Error - Tag Insert : At least one of the following attribute must be defined : template|page|attribute|definition|name|beanName. Check tag syntax"); } }
From source file:org.apache.struts.taglib.tiles.InsertTag.java
/** * Process tag attribute "definition"./* w w w.j ava 2 s . c o m*/ * First, search definition in the factory, then create handler from this definition. * @param name Name of the definition. * @return Appropriate TagHandler. * @throws JspException- NoSuchDefinitionException No Definition found for name. * @throws JspException- FactoryNotFoundException Can't find Definitions factory. * @throws JspException- DefinedComponentFactoryException General error in factory. * @throws JspException InstantiationException Can't create requested controller */ protected TagHandler processDefinitionName(String name) throws JspException { try { ComponentDefinition definition = TilesUtil.getDefinition(name, (HttpServletRequest) pageContext.getRequest(), pageContext.getServletContext()); if (definition == null) { // is it possible ? throw new NoSuchDefinitionException(); } return processDefinition(definition); } catch (NoSuchDefinitionException ex) { throw new JspException("Error - Tag Insert : Can't get definition '" + definitionName + "'. Check if this name exist in definitions factory."); } catch (FactoryNotFoundException ex) { throw new JspException(ex.getMessage()); } catch (DefinitionsFactoryException ex) { if (log.isDebugEnabled()) { ex.printStackTrace(); } // Save exception to be able to show it later pageContext.setAttribute(Globals.EXCEPTION_KEY, ex, PageContext.REQUEST_SCOPE); throw new JspException(ex.getMessage()); } }